I want to refresh an already opened form (form1) from another opened form's (form2) button_click(). In form1 I display the data saved by form2 and when form1 is already opened I want it to refresh if new data is saved on form2 in order to display it.
The problem is that I've tried iterating through `Application.Openforms`, but it turns out that it is read-only and I cannot access the form once found and I don't know how to access *form1* from *form2*, since I can't simply find it.
How can I access *form1* from *form2*?
Edit:
Form1 is actually opened from Form2.
The problem with Application.Openforms is that , as I've stated, a read-only list of forms already opened and I cant actually access the forms through it. They simply don't have the methods for it, I sugest you try using Application.OpenForms and look it up if you don't know how it works.
Also it's pointless to show what I've already tried because it includes Application.OpenForms, but for the sake of information:
FormCollection of = Application.OpenForms;
foreach (var f in of)
{
if (f.GetType().ToString() == "Kontrl_Doc.Visualizar")
{
f.Refresh();
}
}
When I click the button (button_click()) in Form2 it checks if Form1 is open or not. If Form1 isn't open it opens one and if it is than I'd like it to refresh it. Simultaneously, it closes Form2 and opens Form2 again, in order to reset is fields.
What I wan to do is, if the form1 is already opened , it form2 should tell it to refresh the already opened window with the form 1.
"Form1 is actually opened from Form2" - If this is the case, then just call Refresh using the form variable that you have in Form2. If necessary, make that a private field in the Form2 class or store it in an array for later use.
For example:
(Somewhere in Form2)
Form1 form1 = new Form1();
form1.Show();
(Inside the button click in Form2)
form1.Refresh();
you can use events. In form2 you place this code
public event Action ReloadForm1;
//on the place where you will reload form1
ReloadForm1();
and in form1 if you have opening form2:
form2.ReloadForm1 += Reload;
//outside method
void Reload()
{
this.Reload();
}
Create a void method in form1 and add the components u want to refresh maybe you want to reload a dropdown from db
public void Refresh()
{
...
}
then open a dialog of form2
catch the dialog result
Related
I have a program with two forms, but Form1 is opening up over Form2, which isn't good since you need to use Form2 first. How can you force focus to Form2 when the application runs?
Use ShowDialog for Form2 instead of Show.
You can also use Application.OpenForms property to get the already opened form Form2 and then call its Focus method like:
if(Application.OpenForms["Form2"] != null)
Application.OpenForms["Form2"].Focus();
I have Main form with list of data inside listBox. On button click I'm opening new form to create new data object (Main form is inactive in background), when new data is submitted listobox inside main form should be populated with that new object.
I was thinking following:
When Form2 is submitted I was thinking to find MainForm instance and kill that instance and after that it should be easy, load again list of data from the db and display in the listbox.
Question is:
If Form1 is created and on some event Form2 is instantiated with showDialog so Form1 is inactive until data is submitted how to find Form1 instance before Form2 is closed?
So again, how to find instance of Form1 class from Form2 class?
Thanks
You can get a reference to any of the application's currently open forms by using the Application.OpenForms property. Forms in this FormCollection can be accessed by index like so:
var form1 = Application.OpenForms[0];
or by the form's Name property like so:
Form form1 = Application.OpenForms["Form1"];
Hope this helps.
After getting the instance of an open form, I needed to call a method from that form, so this worked for me:
if (System.Windows.Forms.Application.OpenForms["Form1"] != null)
{
Form1 form1 = Application.OpenForms["Form1"] as Form1;
form1.yourMethodCall();
}
if you call
Form1.ShowDialog(this)
then you'll be able to get a reference to the calling form with
this.Owner.Name
in the second form (Form2 in your case)
see http://msdn.microsoft.com/en-us/library/system.windows.forms.form.showdialog.aspx
I have a Main form.I want to launch another form from it and launch another from the launched form.I want to ensure that the Main form is not editable when sub forms are displayed so i use showdialog()
Mainform>(Showdialog)>form1>(showDialog+dispose)>form2(dispose)>Mainform
From Mainform i call form2.ShowDialog() then from form2 i use the following code to launch another form
this.visible=false;
form3.showdialog();
this.dispose();
But there are some problems in this.Is there a better way to achieve what im looking for?
edit:more description
I have a Main form,User clicks a Button on Mainform>Form1 is lauched>User clicks a Button in Form1>Form 2 is lauched(diposing/hiding form1) after form2 is closed Mainform should be brought to front and made editable,until then all other forms should be on top of Mainform and Mainform should be un-editable
The problem is that you have to specify the MainForm as the parent for (both) form2 and form3. When you use the overload of ShowDialog that has no parameters, WinForms uses the active form as the parent, so form3's parent becomes form2 automatically. You are then trying to close/dispose form2 causing form3 to become orphaned.
There are several options for getting the reference to MainForm, but the simplest is to use:
form2/3.ShowDialog(Application.OpenForms["MainForm"]);
Assuming that you have set the Name property on MainForm to "MainForm".
In your code, this.dispose() is executed only after form3 is closed. What i think you want is to close form2 after form3 was closed, so you can call this.Close() instead of this.Dispose().
this.visible=false;
form3.showdialog();
this.Close();
Or maybe, after form3 is shown up you dont need form2 any more. That meas:
this.visible=false;
//show instead of showdialog so it wont wait until form3 is closed
form3.show();
this.Close();
It looks like you are trying to implement something like a wizard. The best solution would be to launch all the child forms sequentially, in the main form.
If you need to pass data along the sequence, you should pass it from each dialog to the main form, which then passes it to the next dialog.
MainForm:
Form1 f = new Form1();
if (f.ShowDialog(this) == DialogResult.OK) {
Form2 f2 = new Form2();
f2.ShowDialog(this);
}
Form1 (button click which will open the form 2):
button1_click(object sender, EvengArgs e) {
this.DialogResult = DialogResult.OK;
Close();
}
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();
I have a form Form1 from which I display Form2 as a modal form. From Form2 I do all sort of editing and deleting of different set of values which should be reflected in Form1 after closing Form2. So what I do is RePopulateControls_in_Form1() after closing Form2. Since RePopulateControls_in_Form1()is a long process, I want to execute that method only if some modification (edit,add, delete) happens in Form2 and not when Form2 is just opened and closed.
So this is what I try to do in Form1:
Form2 f = new Form2();
if (f.ShowDialog(this) == DialogResult.Something)
RePopulateControls_in_Form1()
And then in Form2 I do,
private void bntEdit()
{
//If Edit?
this.DialogResult = DialogResult.Something;
}
private void bntAdd()
{
//If Add?
this.DialogResult = DialogResult.Something;
}
private void bntDelete()
{
//If Delete?
this.DialogResult = DialogResult.Something;
}
But my problem is .Something. If it is anything other than .None, Form2 simply gets closed. I do not want Form2 to get simply closed by its own unless the user closes it.
If I do this:
//in Form1
private void Form1_Click()
{
Form2 f = new Form2();
if (f.ShowDialog(this) == DialogResult.None)
RePopulateControls_in_Form1()
}
//in Form2
private void Form2_SomeModification()
{
//If Modified?
this.DialogResult = DialogResult.None;
}
RePopulateControls_in_Form1() is not hit!
In short, in my program how can I tell the compiler to call RePopulateControls_in_Form1() only if values are modified in Form2?
Note: Repopulating is certainly required since the controls are dynamically created and a bit complex (actually what is created in Form2 is GUI controls and its labels etc).
Setting DialogResult on Form hides the form and returns from ShowDialog. If you want to delay that until the user performs some other action (such as closing the form) you should store that state as a member field and set DialogResult in a handler for Form.Closing.
Also, if you do want to dismiss the modal form on a button press, you can use the Button.DialogResult property instead of making a Button.Click handler.
A simple way might be not to use DialogResult at all but a dedicated property not interfering with the Form's behavior. - Then you should be able to program any logic you want.
I would use an event in Form 2. Fire that event when your Form2 is closing. Handling that event in Form1 would allow you to carry out any processing you want to. Further if needs be you could pass back some information from Form2 to Form1 in parameters to the event.
An alternative would be to set up a Global static variable - maybe just a bool. Then Form2 can set it to true or false depending on whether there are changes made. Form1 can read this when Form2 returns and if true carry out processing and set it back to false.