find form instance from other class - c#

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

Related

Refresh an open form from another open form

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

How to create Form On Top and cannot access other forms

Currently I have two forms(Form1 (base form) and Form2), from form1 I open form2, I want form1 to be inaccessible (unable to click buttons and access any other objects on that form) while form 2 is still open, and once form2 is closed I can access form1 again, I don't know what this behavior is called
I know how to put the form always on top via newForm.TopMost = true and how to check if a form is open via Application.OpenForms.OfType<Alert_Form>().Any(), Anyone know the sniplet I need to achieve what I want for form1 and possible (any other form)
Thanks and cheers
You simply call ShowDialog on your Form2 instance rather than Show. That will display a modal dialogue, which is the behaviour that you describe.

Give focus to form2

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 want to disable a button from clicking

Bassicly im creating a program that allows the user to enter values and if the values exceed a certain amount then disable a button that was on a different form. But im unsure how to access its buttons control. I thought it would be something like this? Thanx
if(value>120)
{
Form3 form3 = new Form3();
Button.Disable();
this.close();
}
Your request is to disable a button that was on another form - from reading that I assume the form already exists. By creating a new instance:
Form3 form3 = new Form3();
You're creating a new instance of Form3 so you'll never disable a button on a form that was already visible.
You'll have to make the current form aware of the instance of Form3 to be able to change anything there. Here are a few ways to make them interact:
Store these values in a separate object and make your forms react on changes through events for example
Provide a reference to the instance of Form3 upon creating or Show()ing "this" form
Keep a reference to each created form in a static class
Also keep in mind having multiple related forms active at the same time may confuse your end-user.
You need a reference to an instance of Form3. You are creating a new instance of Form3 which is probably not what you wanted. Then your Form3 needs to expose the button you are interested in as a public property so that you can access it from outside of the class. Then you should be able to set the Disabled property to true.
You can disable the button like this:
otherForm.Button.Enabled = false;
To be able to disable this button from another context (form), you need to declare it as public. You can do this as follows:
selecting the button in design view
in the properties window set Modifiers to Public
Then you can show the form with the disabled button, like so:
var newForm = new Form3();
newForm.Button.Enabled = false;
newForm.Show();
I guess you have to do something like this.
update
if(value>120)
{
Form3 form3 = new Form3();
form3.Button.Enabled = false;
this.close();
}
update

c# gui - navigate between forms

I'm programming c# GUI and I have 2 forms.
Form1 is my main form, and it has a button to open form2.
When the button in form1 is being clicked, I hide form1, create a new object of form2 and show form2.
I have a back button in form2. I want the behavior of this button to close form2, and show again the hidden form1.
How can I do it?
Have your form1 subscribe to the VisibleChange event of form2 and act accordingly. It will have to "remember" whether form2 is visible or hidden (or query it directly).
The alternatives are:
Your form2 will need a reference to form1.
This can be done in a number of ways - passing it in a constructor parameter, adding a property and assigning form1 to it.
Either of these ways will tightly couple these forms to each other (bad thing).
Have you tried
Form1.Visible = true;
Form1.Activate();
And then in the Form1 VisibleChanged Event you simply write
Form2.Close();

Categories

Resources