i have Form1 and Form2, in Form1 i have some textboxes like username,password and more... and a textbox "region". when user hits "region"(Form1.hide()), then Form2 opens witch has 5 labels with names of regions on it.
so how can i make that when user clicks on a name of region in Form2, Form1 will have the region on it? and keep all the data that the user entered before the region click.
something like this(in form 2):
private void center_Click(object sender, EventArgs e)
{
this.Hide();
Form1.region = "center";
Form1.show();
}
Try creating an instance of Form2 and calling ShowDialog() method to show it
Form2 form2= new Form2();
form2.ShowDialog();
When creating Form2, just pass Form1 as a parameter and edit the textbox value in your click event.
On form1:
private void click_on_region(object sender, EventArgs e)
{
this.Hide();
Form2 frm2 = new Form2(this);
Form2.Show();
}
on form2:
Form1 _frm1;
public Form_Main(Form1 frm)
{
InitializeComponent();
_frm1 = frm;
}
private void center_Click(object sender, EventArgs e)
{
this.Hide();
_frm1.textBox_region.Text = whateverobject.Text;
_frm1.Show();
}
This might not be the prettiest but It'll do for starters.
Form 1 Code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 objForm2 = new Form2();
objForm2.ShowDialog();
textBox_Region.Text = objForm2.RegionName;
}
}
And Form 2 Code
public partial class Form2 : Form
{
public string RegionName
{
get
{
return textBox_Form2_Region.Text.ToString();
}
set { }
}
public Form2()
{
InitializeComponent();
}
}
On Form 2
private void center_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.textbox_region = whateverobject.text;
this.hide();
frm1.show();
}
this will bring up a form1 with the region text on it.
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, 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();
}
}
How can I change title of Form 1 from my From 2? Here is my code:
Form1:
public void setTitle(string title)
{
this.Text = title;
}
Form2:
private void buttonOk_Click(object sender, EventArgs e)
{
Form1 f1= new Form1();
f1.setTitle(textBoxTitle.Text);
this.Hide();
}
What am I doing wrong?
You should pass Form1 as parameter in Form2's constructor.
Form1 Form_one;
public Form2(Form1 form1):this()
{
Form_one = form1;
}
private void buttonOk_Click(object sender, EventArgs e)
{
Form_one.setTitle(textBoxTitle.Text);
this.Hide();
}
In the method you want to show Form2 you should call like that;
Form2(this).Show();
You should have the actual instance of Form1 which is currently displayed.
While displaying Form1, keep an instance to it in Form2. (I am assuming that you are displaying Form1 from Form2. If not, you should provide that Form1 instance to Form2 while creating an instance of Form2)
public class Form2 : Form
{
private Form1 form1;
private void OpenForm1()
{
form1 = new Form1();
form.Show()
}
}
Then, invoke the setTitle() on that instance:
private void buttonOk_Click(object sender, EventArgs e)
{
form1.setTitle(textBoxTitle.Text);
this.Hide();
}
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();
}
("Click on owner-form" doesn't count. :))
I have:
public partial class Form1 : Form
{
Form2 frm2 = new Form2();
public Form1()
{
InitializeComponent();
frm2.Owner = this;
}
private void button1_Click(object sender, EventArgs e)
{
frm2.ShowDialog();
}
}
Just change the owner setting from
frm2.Owner = this;
to
frm2.ShowDialog(this);
(Credit goes to gerald-p-wright)