I know how to go to another form in modal mode just like what I did below:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Form2 myNewForm = new Form2();
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
myNewForm.ShowDialog();
}
}
This is my second form, how do I go back to the previous form?
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
// what should i put here to show form1 again
}
}
When you call ShowDialog on a form, it runs until the form is closed, the form's DialogResult property is set to something other than None, or a child button with a DialogResult property other than None is clicked. So you could do something like
public partial class Form1
{
...
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
newform.ShowDialog();
// We get here when newform's DialogResult gets set
this.Show();
}
}
public partial class Form2
{
...
private void button1_Click(object sender, EventArgs e)
{
// This hides the form, and causes ShowDialog() to return in your Form1
this.DialogResult = DialogResult.OK;
}
}
Although if you're not doing anything but returning from the form when you click the button, you could just set the DialogResult property on Form2.button1 in the form designer, and you wouldn't need an event handler in Form2 at all.
I use a static Form value Current in all my multiple form apps.
public static Form1 Current;
public Form1()
{
Current = this;
// ... rest of constructor
}
Then in Form2
public static Form2 Current;
public Form2()
{
Current = this;
// ... rest of constructor
}
Then you can from your button click do,
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
// what should i put here to show form1 again
Form1.Current.ShowDialog(); // <-- this
}
Related
I am new to C# and I am using windows forms.
I have form1 and form2, I show and hide form2 from form1 as following:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Form2 frm2 = new Form2();
private void button_showForm2_Click(object sender, EventArgs e)
{
frm2.Show();
//I want to show the following message once form2 hides:
MessageBox.Show("Form2 is hidden. Continue processing next line of code");
}
}
In form2:
private void button_HideForm2_Click(object sender, EventArgs e)
{
Hide();
}
When I run the above code and show form2, form2 shows up with the messageBox at same time. I know it is because when using Show() method it does not hold the program flow and continue executing next lines of code while using ShowDialog() holds the program flow till you close the child form.
What I want to do is ( I do not want to use ShowDialog()) : I want to show form2 and when you finish using it and hide it I want to display the above message (in form1) when form2 hides.
Anyone knows how can I do that? Thank you alot.
I would implement it in this way
Form1:
private void button_showForm2_Click(object sender, EventArgs e)
{
this.frm2 = new Form2(this);
this.frm2.Show();
}
public void ShowMessage()
{
MessageBox.Show("Form2 is hidden. Continue processing next line of code");
}
Form2:
public Form1 _Form1 { get; private set; }
public Form2(Form1 _Form1) { this._Form1 = _Form1; }
private void button_HideForm2_Click(object sender, EventArgs e)
{
Hide();
this._Form1.ShowMessage();
}
Hook the Form's VisibleChanged Event, your code will then get fired when the Forms visibility changes.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Form2 frm2 = new Form2();
private void button_showForm2_Click(object sender, EventArgs e)
{
frm2.VisibleChanged += new EventHandler(this.FormVisibilityChanged);
frm2.Show();
}
private void FormVisibilityChanged(object sender, EventArgs e)
{
frm2.VisibleChanged -= new EventHandler(this.FormVisibilityChanged);
MessageBox.Show("Form2 is hidden. Continue processing next line of code");
}
}
I have two forms, form1 and form2.
In form1, there is are two buttons, button1 and button2.
In form2, there is a listview, ListView1.
button1 should hold a string value called "Vanilla".
When button2 is pressed it opens form2.
On form2, in listview1 it should show "Vanilla" in the first column.
Form1
public partial class form1 : Form
{
public static string buttonValue = "";
public form1()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
buttonValue = "Vanilla";
}
public void button2_Click(object sender, EventArgs e)
{
form2 form2 = new form2();
form2.Show();
this.Hide();
}
Form2
public partial class form2 : Form
{
public form2()
{
InitializeComponent();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
You can design the second form as bellow:
public partial class form2 : Form
{
public form2()
{
InitializeComponent();
}
private string _passedValue = "";
public form2(string passedValue)
{
InitializeComponent();
_passedValue = passedValue;
listView1.Items.Add(_passedValue);
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
You can pass the value stored in the first button using the bellow code.
public void button2_Click(object sender, EventArgs e)
{
form2 form2 = new form2(buttonValue);
form2.Show();
this.Hide();
}
I have two forms Form1 and Form2, Form 1 with a dataGridView and a button where Form2 has one button.
When i click the button1 in Form1, it will open Form2 (overlapping).
Now i need to clear the values of the datagridview in Form1 when i press the
button in Form2 also the same button click event should close the
Form2.
Any idea how to do it?
Any help is appreciated
This is Form 2:
private DataGridView _dgv;
public Form2(DataGridView dgv)
{
_dgv = dgv;
InitializeComponent();
}
private void buttonClearRows_Click(object sender, EventArgs e)
{
_dgv.Rows.Clear();
Close();
}
This is Form1:
private void buttonOpenForm2_Click(object sender, EventArgs e)
{
new frm2(dataGridView1).ShowDialog();
}
You need to pass a reference to Form1 into Form2, either by consctructor or by a property. Sample uses constructor. Change the names of the controls to the ones you have. Consider this as a pseudo code sample.
Form1 (some logic removed for brewity)
public class Form1
{
...
public void Clear()
{
DataGridView1.Rows.Clear();
}
public void btnOpenForm2_Click(object sender, EventArgs e)
{
var form2 = new Form2(this); // create a new form2, and pass a reference to form1
form2.Show(); // show the form.
}
...
}
Form2 (some logic removed for brewity)
public class Form2
{
private Form1 _parent; // this will hold the parent until Form2 is disposed.
...
public void Form2(Form1 parent)
{
_parent = parent; // assign Form1 instance to a field.
}
public void btnClearGrid(object sender, EventArgs e)
{
_parent.Clear(); // clear the rows in the datagridview instance within form1.
}
...
}
If I understand what you're asking, I strongly suggest to hide access to DataGridView in Form1 from outside the class, to avoid unexpected behavior in future.
You could instead add a function in Form1 and manage button1 click event in this way:
public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
Form2 dialog = new Form2();
dialog.ShowDialog(this);
}
public void ClearRows() { dataGridView1.Rows.Clear(); }
}
Then in Form2 you can easily handle click on button in this way:
public partial class Form2 : Form
{
private void button1_Click(object sender, EventArgs e)
{
((Form1)this.Owner).ClearRows();
this.Close();
}
}
It's very simply, in the form1 you can create an instance of Form2 and after the show you can set the reference of dataGridView in child form2.
Fundamental is who you set the dataGridProperty modifier=public(visual property F4) in the form1
This is Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.dataGridViewPassed = this.dataGridView1;
f.ShowDialog();
}
}
This is Form 2:
public partial class Form2 : Form
{
public DataGridView dataGridViewPassed = null;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.dataGridViewPassed.Rows.Clear();
}
}
I have two forms, Form1 and Form2. I use ShowDialog() on Form2 from Form1. How can I run Form1Load() from Form2? Specifically, I want to refresh Form1 from Form2.
Firstly, make sure you assigning the Owner property on Form2 before showing it. This allows you to access the current instance of Form1.
class Form1 : Form
{
public void Method()
{
var form2 = new Form2();
form2.Owner = this;
form2.ShowDialog();
}
}
From Form2 you can this use this.Owner to access the instance of Form1 and call any public methods or access any public properties. (Make sure the load event handler is public)
class Form2 : Form
{
public void Method()
{
this.Owner.form1_Load(null,null); //assuming you don't use these params.
}
}
In your form1_Load() I would recommend putting in a this.Refresh() to "refresh" the form. The refresh doesn't update some things that have data stored, it only repaints the form, so in the load event you will have to manually "refresh" things.
Create an instance of Form1 and then use the Refresh method or your Form1_Load method with that instance.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void ShowMessage(string message)
{
MessageLabel.Text = message;
}
private void ShowForm2(object sender, EventArgs e)
{
Form2 Form2Copy = new Form2(this);
Form2Copy.ShowDialog();
}
}
and
public partial class Form2 : Form
{
Form1 Form1Copy;
public Form2(Form1 Parent)
{
InitializeComponent();
Form1Copy = Parent;
}
public void Button_Click(Object sender, EventArgs e)
{
Form1Copy.ShowMessage("Hello from Form2!");
}
}
Pass in Form1 to the ShowDialog() method of your Form2 instance:
private void Form1_Load(object sender, EventArgs e)
{
this.LoadEventCode();
}
public void LoadEventCode()
{
this.Text = DateTime.Now.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog(this); // <-- pass in Form1
}
Now over in Form2, cast the .Owner property to Form1 and do what you need:
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = (Form1)this.Owner;
f1.LoadEventCode();
}
Basically; Form1 has 2 buttons, Form2 has 1 button.
When you click Form2's button it checks which button on Form1 you clicked, opening Form3 or Form4 depending on which button you clicked (on Form1).
So I've utilized Mark Halls first method of passing variables between forms. Now for the second half of my closed refinement.
Form1
private void btnLogin_Click(object sender, EventArgs e)
{
// Call function while storing variable info.
Account("login");
}
private void btnRegister_Click(object sender, EventArgs e)
{
// Call function while storing variable info.
Account("register");
}
// Function used to pass Variable info to Account form while opening it as instance.
private void Account(string formtype)
{
// Generate/Name new instant of form.
frontend_account frmAcc = new frontend_account();
// Pass variable to instance.
frmAcc.CheckButtonClick = formtype;
// Show form instance.
frmAcc.Show(this);
// Hide this instance.
this.Hide();
}
Form2
// String Variable to store value from Login.
public string CheckButtonClick { get; set; }
private void btnContinue_Click(object sender, EventArgs e)
{
// If statement to open either Main form or Registration form, based on Login variable.
if (CheckButtonClick == "login")
{
// Generate/Name new instant of form.
frontend_main frmMain = new frontend_main();
// Show form instant.
frmMain.Show();
// Close this instant.
this.Close();
}
else if (CheckButtonClick == "register")
{
// Generate/Name new instant of form.
frontend_register frmReg = new frontend_register();
// Show form instant.
frmReg.Show();
// Close this instant.
this.Close();
}
}
On Form2 there are TWO radio buttons, can I adept that code to set the focus of a tab control when a form is opened? ie. if radClient is checked set focus on tabcontrol after opening winform, else if radStudent is checked set focus on tabcontrol (other page) after opening winform... and i guess don't open a winform if no radio is checked.
I believe this will set the focus;
// Sets focus to first tab.
tabRegister.SelectedTab = tabRegister.TabPages[0];
// Sets focus to second tab.
tabRegister.SelectedTab = tabRegister.TabPages[1];
In your example the first problem I see is you are closing your parent form which closes your Form1 and disposes of Your Form2, What I would do is Hide Form1 instead of Closing it, I would then create a public property on Form2 to pass in the Button that was selected. But anytime you are opening and closing multiple Forms it can get messy, what I would do would be to create UserControls for your additional Forms and swap them out in a Panel. The first example is how to do it the way that you asked.
Form
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
ShowForm2("login");
}
private void btnRegister_Click(object sender, EventArgs e)
{
ShowForm2("register");
}
private void ShowForm2(string formtype)
{
Form2 f2 = new Form2(); // Instantiate a Form2 object.
f2.CheckButtonClick = formtype;
f2.Show(this); // Show Form2 and
this.Hide(); // closes the Form1 instance.
}
}
Form2
ublic partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public string CheckButtonClick { get; set; }
private void button1_Click(object sender, EventArgs e)
{
if (CheckButtonClick == "login")
{
Form3 f3 = new Form3(); // Instantiate a Form3 object.
f3.Show(); // Show Form3 and
this.Close(); // closes the Form2 instance.
}
else if (CheckButtonClick == "register")
{
Form4 f4 = new Form4(); // Instantiate a Form4 object.
f4.Show(); // Show Form4 and
this.Close(); // closes the Form2 instance.
}
}
}
Form3 and Form4 note since Form1 is long forgotten to these forms I search for it to Open back up
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void Form3_FormClosed(object sender, FormClosedEventArgs e)
{
FormCollection frms = Application.OpenForms;
foreach (Form f in frms)
{
if (f.Name == "Form1")
{
f.Show();
break;
}
}
}
}
The second Option with UserControls has one Form with a Panel on it. It uses events to signal the Form to Change Controls plus a public property on UserControl2
public partial class Form1 : Form
{
string logonType;
public Form1()
{
InitializeComponent();
}
private void userControl1_LoginOrRegisterEvent(object sender, LoginOrRegisterArgs e)
{
logonType = e.Value;
userControl2.BringToFront();
}
private void userControl2_ControlFinshedEvent(object sender, EventArgs e)
{
if (logonType == "logon")
userControl3.BringToFront();
else if (logonType == "register")
userControl4.BringToFront();
}
private void userControl3_ControlFinshedEvent(object sender, EventArgs e)
{
userControl1.BringToFront();
}
private void userControl4_ControlFinshedEvent(object sender, EventArgs e)
{
userControl1.BringToFront();
}
}
UserControl1
public partial class UserControl1 : UserControl
{
public delegate void LoginOrRegisterHandler(object sender, LoginOrRegisterArgs e);
public event LoginOrRegisterHandler LoginOrRegisterEvent;
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
LoginOrRegisterArgs ea = new LoginOrRegisterArgs("logon");
LoginOrRegisterEvent(sender, ea);
}
private void button2_Click(object sender, EventArgs e)
{
LoginOrRegisterArgs ea = new LoginOrRegisterArgs("register");
LoginOrRegisterEvent(sender, ea);
}
}
public class LoginOrRegisterArgs
{
public LoginOrRegisterArgs(string s) {Value = s;}
public string Value {get; private set;}
}
UserControl2
public partial class UserControl2 : UserControl
{
public delegate void ControlFinishedHandler(object sender, EventArgs e);
public event ControlFinishedHandler ControlFinshedEvent;
public UserControl2()
{
InitializeComponent();
}
public string SetLogonType { get; set; }
private void button1_Click(object sender, EventArgs e)
{
ControlFinshedEvent(sender, new EventArgs());
}
}
UserControl3 & UserControl4 exactly the same except for different Class Name
public partial class UserControl3 : UserControl
{
public delegate void ControlFinishedHandler(object sender, EventArgs e);
public event ControlFinishedHandler ControlFinshedEvent;
public UserControl3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ControlFinshedEvent(sender, new EventArgs());
}
}
As I suggested in my comment, one of the best way I know to pass data between forms is to use events.
Basically, in the "child" forms, you declare an event that will be handled, or listened to, by the "main" form.
See the referenced answer from my comment, and if you have specific questions on how to adapt it, ask away.
Cheers