This question already has answers here:
Send values from one form to another form
(20 answers)
Closed 7 years ago.
I has 2 form which is Form1 and Form2. In Form1, I has a button which will open Form2 in button click. In Form2, it use to do some setting for the label text in Form1. When close the form2, the label text in Form1 will update base on the setting, but I can make the label text update. Below is the code for update the text for label in Form1. I'm hardcode the text to simulate the situation.
Form1
public void languageChange()
{
labelControl5.Text = "AAAAAA";
labelControl5.Invalidate();
labelControl5.Update();
labelControl5.Refresh();
Application.DoEvents();
}
In form2, I has below code to fire languageChange function in Form1.
private void innoLanguage_FormClosed(object sender, FormClosedEventArgs e)
{
Main_new main = new Main_new();
main.languageChange();
}
It has call the function in the Form1 when form2 is close but it not update the label text.
I feel I make a mistake but I can't figure out. How I can make this work, please help.
First thing's first you don't need to invalidate a Label when you change its Text variable, it will automatically redraw the control.
Secondly when the Form2 closes you created a new Main_new, called languageChange() on it, but then didn't actually show or display the form. I'm not sure what the situation is but if there's already a Main_new form open you don't need to make a new one, simply get the parent of the Form2 (which will be the Main_new already open), cast it as a Main_new, then call languageChange() on that.
Example
private void innoLanguage_FormClosed(object sender, FormClosedEventArgs e)
{
((Main_new)this.Parent).languageChange();
}
If Main_new isn't the name of the Form that opens Form2 change it to whatever does.
Related
it's just a simple stuff that i wanna test
i have two forms (Form1 and Form2)
In Form1 have:
textbox and button
In Form2 have only TextBox that use for displayData when I click button in Form1
the problems is when i clicked buttton the data send to Form2 but it's created a new form everytime I clicked it. Is there any way to updateData in Form2 without create a new one.
Here the code:
In Form1:
private void sendBt1_Click(object sender, EventArgs e) //sendBt1 is button in Form1
{
Form2 f2 = new Form2(typeBox1.Text);//typeBox1 is TextBox in Form1 use to type
f2.Show();
}
In Form2:
public Form2(string data)
{
InitializeComponent();`enter code here`
this.ds2.Text = data; //ds2 is textBox in Form2 that i wanna display
}
Can anyone help? Im kinda stuck rn :)
As commented already, there are numerous ways to get this done. The current code, as you describe, is creating multiple Form2 objects since the code is creating a new Form2 each time the button is clicked on Form1.
Form2 f2 = new Form2(typeBox1.Text);
In addition, you want the “TEXT” in typeBox1 TextBox to be passed to Form2's TextBox ds2. The current code does this however, if the text in typeBox1 TextBox in Form1 changes, the TextBox in Form2 (ds2) is NOT updated. Again, there are numerous ways to fix this, however, I suggest to create a DataBinding for Form2's TextBox such that IF the typeBox1 TextBox changes in Form1, then the TextBox ds2 in Form2 will automatically be updated at the same time and the user does not have to do anything.
The example below uses this DataBinding approach and works as described….
I may be missing something here, however, in my tests, one approach is to simply “check” to see if Form2 is already open. If Form2 IS OPEN… then, we do not have to do anything since our code sets up the data binding for the text box in Form2’s constructor. Since Form2 IS OPEN it may be minimized, therefore, if the user clicks the button on Form1 and Form2 IS OPEN then we will set the forms state back to normal which will basically restore the form. Also the code will call the forms Show() method in case the Form has somehow been hidden.
IF Form2 IS NOT OPEN, then obviously we need to create it as your current code does, however, instead of passing in JUST the TEXT from the TextBox, I suggest you send over the WHOLE TextBox from From1 to Form2. This way, we can set up the DataBinding for Form2’s TextBox and Bind Form2’s TextBox to the passed in TextBox from Form1. This will automatically update Form2’s TextBox ds2 when Form1’s TextBox changes.
Therefore I suggest you change the code in Form1’s sendBt1_Click event to something like…
private void sendBt1_Click_Click(object sender, EventArgs e) {
var form = Application.OpenForms["Form2"];
if (form == null) {
form = new Form2(typeBox1);
}
form.Show(); // <- in case the form is somehow hidden
form.WindowState = FormWindowState.Normal; // <- in case the form is minimized
}
NOTE: in the above code we are passing the WHOLE TextBox to From2, not just the text in the TextBox.
Then Form2’s constructor may look something like….
public Form2(TextBox f1TextBox) {
InitializeComponent();
ds2.DataBindings.Add("Text", f1TextBox, "Text");
}
This should be all you need. When the text is changed in Form1’s text box, the text box in Form2 is automatically updated.
I have 2 Forms. The first one contains a Button and the other contains a PictureBox.
Now I want to display the PictureBox when the Button is pressed.
I have tried with this code:
private void button3_Click(object sender, EventArgs e)
{
// in order to hide the first form
this.Hide();
Form2 f1 = new Form2();
f1.ShowDialog();
// to display the pic the second form
Form2.pictureBox1.Visible = true;
}
But the problem was that it was inaccessible due to protection level.
What can I do?
One Way is to Pass the Image Path (string) as argument to Form2. i.e. Create a constructor for Form2 taking a string param. Do the Image Assignment & Visibility thing in Form2 Constructor (obvisouly after InitializeComponents()).
Hack Fix way is to make the PictureBox in Form2 (designer) as Public.
I'm working on a c# program and I want a panel to appear on a form when a button is clicked in another. So when the add button is clicked on form2 the panel requesting the details for this to be possible will be displayed on form 1.
I currently have a static method set up in form1 which can be accessed from form2 - however due to panel.Show() being non static it won't allow me to use this in the function.
In Form1 I have:
public static void showPanel()
{
panel.Show()
}
In my second form I have the following:
private void btn_add_Click(object sender, EventArgs e)
{
form1.showPanel();
this.Hide();
}
I have tested with just having the static function show a message box which works. Is it possible to do it the way I want or do I need to take a few steps back and try a different technique?
Are we talking about a new instance of the second form if so you can try to instantiate the new form using:
Form newForm = new YourFormName(potential parameters);
newForm.showPanel();
newForm.Show();
If you want to execute the form on an open form you can give the first form a reference(field with instance) of the second form. Or you can try using: Application.OpenForms. if you give it [1] it'll give you the second open form probably. You can also use .OfType to get the correct form in case your form order isn't always the same.
PROBLEM SOLVED
SHORT STORY
I want to detect "FormClosing()" event through different forms, ie, when form1 is closed that is instantiated within form2, can form2 detect when user presses exit in form1?
LONG STORY
My team and I are working on a windows form application. Project has two forms: one is the main form page and the other is accessed via this main form. Main form looks like this:
And the second one looks like this:
If you press "Ekle/Sil" buttons within the main form, you are directed to form 2 where you can edit database entries. When you press "Sayfayı Yenile" button in the main form, the content of the text areas are refreshed by re-fetching entries from the database.
My problem is, I want to automatically refresh the main form when the user closes the second form. My research suggests I should use an "FormClosing()" event to detect a closing form. However, I want to detect this from the main form. Instantiating main form in second form's source code doesn't seem to be a reliable solution. Anyone can tell me how to do this?
EDIT
I solved the problem:
1) Created a public method within the main form that refreshes the page.
2) Send "this" property from the main form when creating the second form.
3) Added an "FormClosed()" handler within the second form that invokes this public method.
Still, I'm looking for a better solution.
EDIT 2
Better solution InBetween's answer
Simply use the Form.Closed event of the new child windows form. Everything is handled from the main form:
void EkleSil_Clicked(object sender, EventArgs e) //or whatever method is called when button is clicked
{
var newChildForm = new ChildForm();
newChildForm.Closed += childFormClosed;
newChildForm.Show();
}
void childFormClosed(object sender, EventArgs e)
{
((Form)sender).Closed -=childFormClosed;
updateShownData();
}
You can create a event in the second form and raise it when the form is closing .
Handle the event in the main form and refresh the main form when the event is raised
Another option would be to pass Form1 as an argument to Form2. Then use the Form.Closing event in Form2 and use the Form1 reference to trigger something.
Form1 form1Ref;
public Form2(Form1 mainform)
{
form1Ref = mainform;
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
form1Ref.SomeMethod();
}
how do I check if a form is closed?
I want to reload a listview's items when I close another form.
What I'm doing is:
Form1 = form with ListView
Form2 = form with TextBoxes
Opening a new form.
Creating a new folder via a textbox.
When I press "Add" in Form2 I want to reload the "ListView" on Form1.
So how am I supposed to do this?
And sorry for my bad English :(.
The simplest answer is to make a public method on Form1, let's call it RefreshList() and in your button click event on Form2, you simply call Form1.RefreshList. Here's a quick sample:
Form 2:
public Form1 ParentForm { get; set; }
private void Button_Click(object Sender, EventArgs args) {
{
// After the rest of your handler
if(ParentForm != null)
ParentForm.RefreshList();
Close(); // Close Form2 here, we're done!
}
Form 1:
private void ShowForm2()
{
Form2 form2 = new Form2();
form2.ParentForm = this;
form2.Show();
}
public void RefreshList()
{
// do your refresh here
}
Initially you mention that you want to do this when Form2 closes, but then later you mention that you want to do it "When I press "Add" in Form2 I want to reload the "ListView" on Form1." As others have mentioned, you can use the Closed event, so I took this approach to address your second case.
In Form1, you can subscribe to From2's FormClosed event.
You can handle the form Closing event as the Closed Event is obsolete if you are using anything above dot net 1.1
You can handle the FormClosed event if you simply want to react to when a particular form is closed. If you want to take it a step further, there is a FormClosing event that you could also handle and even prevent the form from closing if need to.
Having Form1 add an event handler to Form2's FormClosing event is an excellent option. However, there is an even simpler solution if Form2 is a dialog window. In other words, if it's OK for Form1 to be effectively locked and disabled while Form2 is displayed, you can simply show Form2 as a dialog window. Dialog windows show synchronously, so you can simply update the list immediately after showing Form2 and it won't hit that list-updating code until after Form2 is closed. For instance:
form2.ShowDialog(this);
updateList();