I (currently) have two forms, one that needs to call the other. In other words, by clicking a certain button in Form1, Form2 needs to "pop up", like a dialog box. I know I have to get a reference in both forms, but I'm not entirely sure how to do this. What is a decent tutorial/beginner site to learn about GUIs in C#?
You can try this:
protected void Button1_Click(object sender, EventArgs e)
{
Form2 myForm = new Form2();
myForm.ShowDialog();
}
ShowDialog forces the user to close that window before s/he can access the rest of the application.
Related
I have a form and through this I press a button and a new form opens. If I click minimize this new form, the form I call this form from is minimized as well. How could I make sure the form from which I call the new form is not minimized?
Maybe I have to enable some property in the form or something.
Of course, I tried with the following code and with the form propety singleFixed but the two forms are minimized:
private void bminimize_Click(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
}
Maybe have I to create this new form as subform or something like this?
EDIT: How I call this new form:
private void Button_Click(object sender, EventArgs e)
{
DateTime rnow = DateTime.Now;
Chronometer chrono = new chronometer();
var resultchrono = chrono.ShowDialog();
if (resultchrono == DialogResult.OK)
{
...
}
You're using ShowDialog(), which is documented thus, emphasis mine:
This version of the ShowDialog method does not specify a form or control as its owner. When this version is called, the currently active window is made the owner of the dialog box. If you want to specify a specific owner, use the other version of this method.
If you need the dialog result, you'll need to use the ShowDialog(owner) form of the call with say, the desktop window handle.
Its because of: chrono.ShowDialog();. Show the dialog with chrono.Show(); . But you will have to handle returned values differently.
If you need the DialogResult in the same method in which you open the window, then use AKX´s answer.
Problem: I have created a program that has two forms. When a button is pressed on Form1, the program "hides" Form1 and "shows" Form2. The issue is, when I close Form2, the program continues to run in the background, I believe this is because the program needs Form1 to be closed, not hidden in order for the program to end.
My Question is how do I override the Second Form to close the program and not hide?
private void btnCreate_Click(object sender, EventArgs e)
{
Form2.Show();
Form1.Hide();
}
I know I could show Form1 again from Form2, and then close, but I would really like to avoid that.
You have few options to do that, according to how your application is.
First and intuitive solution is to call Close() instead of Hide() for Form1. This works only if Form1 is not the main form of your application (the one inside main message loop started with Application.Run()).
Second solution is to add an event handler for the FormClosed event of Form2 and to close first form when second one is closed:
private void btnCreate_Click(object sender, EventArgs e)
{
Form2.Show();
Form2.FormClosed += new FormClosedEventHandler(delegate { Close(); });
Form1.Hide();
}
You can directly close Form1 or call Application.Exit() (in case you did open multiple forms you want to close all together).
Last solution is to make second form the owner of first one:
private void btnCreate_Click(object sender, EventArgs e)
{
Form2.Show();
Form1.Owner = Form2;
Form1.Hide();
}
This will close automatically the owned form when owner is closed. This is the solution I prefer but it works only for one form (an owner can own only one form).
You can use the FormClosed event:
private void btnCreate_Click(object sender, EventArgs e)
{
Form2.Show();
Form2.FormClosed += new FormClosedEventHandler(form2_FormClosed);
Form1.Hide();
}
void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
Form1.Close();
}
In application development especially for winforms for the sake of your users, always have a main form (whether an mdiparent or a single parent) that will always be the main form that all other forms show from.
Giving an answer to the question will be encouraging bad programming practice.
Let the mainform close and the application will end. Hiding form1 will not end the application.
So in the project I'm planning, the user will be switching between different screens frequently. These different screens will have different controls with different functions, and really nothing in common.
Should I create a form for each screen and just form.close and form.show(?) to switch screen? Or is it better to keep hiding and showing certain controls, so if a user wants to go to form B from form A, all of form A's controls disappear, and form Bs controls appear? I don't need to know how to do it, I just need to know which is the recommended or proper way, since I haven't really seen it anywhere.
You shouldn't use
form.close();
as this will close the current form (and if its your main form, exit the application) use
form.hide();
instead. An example is given below
from Form1:
private void btnModify_Click(object sender, EventArgs e)
{
Form mod = new modifyForm();
mod.Owner = this;
mod.Show();
this.Hide();
}//end btnModify_Click
from modifyForm:
private void btnCancel_Click(object sender, EventArgs e)
{
this.Owner.Show();
this.Close();
}
You can also try using the TabControl. You can group your different screens into the different tab items. All you have to do is drag it to the form and edit its content in the Properties window.
I'm currently trying to create a Properties Window which is opened after a Button on the Outlook Toolbar is pressed, i now have:
1) the Button on the Toolbar (currently if pressed nothing occurs)
2) i know how to create the method which would hold the action after the Button is Pressed
-but, I am a beginner and i don't know how to create a window which would open after the button is pressed, the Window should be fairly big, and for now have nothing but a checkbox(which i later would like to apply some method to.
if you ever created a window which opens after a button is pressed, i would be really pleased to get your help.
All help is appreciated, thank you
Here's the recommended way of opening a dialog window when the user clicks a button:
Add a new form to your project (e.g. MyForm) and then you can use the following code in your button's click event handler:
private void OnMyButtonClicked(object sender, EventArgs e)
{
MyForm myForm = new MyForm();
if (myForm.ShowDialog() == DialogResult.OK)
{
// The code that should be executed when the dialog was closed
// with an OK dialog result
}
}
In case you do not want the new window to be modal (i.e. you want to allow the user use other parts of the application while the window is opened), the code gets even more simple:
private void OnMyButtonClicked(object sender, EventArgs e)
{
MyForm myForm = new MyForm();
myForm.Show();
}
You can also create your form on the fly without adding one to your project, which is a bit more complicated, but advanced developers prefer this approach instead of messing with the designer ;)
private void OnMyButtonClicked(object sender, EventArgs e)
{
Form myForm = new Form();
myForm.Text = "My Form Title";
// Add a checkbox
CheckBox checkBox = new CheckBox();
checkBox.Text = "Check me";
checkBox.Location = new Point(10, 10);
myForm.Controls.Add(checkBox);
// Show the form
myForm.Show();
}
Here is a small tutorial for you to follow..
http://msdn.microsoft.com/en-us/library/ws1btzy8%28v=vs.90%29.aspx
EDIT: I would also recommend you remember the msdn website because it will prove invaluable for other programming issues you come across..
you have to add a new form to your project. Then you call the constructor where you want to pop up the window.
like this
Form2 form2 = new Form2();
form2.showDialog();
Edit:
where form2 is not the "main" Form of you program.
This'll set your main window to the background as long as the newly popped up window is closed.
I have two forms form1 is main form and form two is model form I want to set the forms as below:
Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show(this);
}
The above would set the form1 owner of form2 and form2 would be shown but the problem is that this will break the order of forms on press of Alt+Tab keys hence I have tried it with another way as below.
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog(this);
}
This would be works but the problem is that the dialogue forms will not allow me to maximise/minimise and close
My form2 is borderless form and it is set to show on specific location as to fit with main form1. My aim to do not shows the form2 in Alt+Tab list and as I close the form2 then form1 will show immediately without break order of form.
When I press Alt+Tab keys on first condition and try to close form2 then the other application shown instead of form1 which is I do not want.
Is there any solution of this problem?.
It really sounds like you could do the second form as a custom control.
See Microsoft's documentation and this set of examples.
Think of it as a standard control, like a Button, DataGridView, TextBox, or the like, except that you have total control over it. You can show or hide it, you don't have to worry about where it is positioned, it won't take focus away from the parent form, and so on. And you can put whatever other controls you want in it, encapsulate all their logic, etc.
A possible hack is to keep your parent form active after opening child form as a modal form, so that you could do maximize/minimize your parent as well. An extension method:
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool EnableWindow(IntPtr hWnd, bool enable);
public static DialogResult ShowDialogSpecial(this Form formToBeShown, Form parent)
{
parent.BeginInvoke(new Action(() => EnableWindow(parent.Handle, true)));
formToBeShown.ShowDialog(parent);
return formToBeShown.DialogResult;
}
You can call:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
//additionally do f2.ShowInTaskbar = false to make sense.
f2.ShowDialogSpecial(this);
}
This wont let child form truly act as non-modal form, since child form can cover over parent form.