I have 2 Forms. Form1 creates Form2 like this:
public partial class Form1 : Form
{
private void button3_Click(object sender, EventArgs e)
{
Form2 AcqForm = new Form2();
AcqForm.Show();
string[] ret = AcqForm.fulldate;
MessageBox.Show(ret[27]);
}
}
public partial class Form2 : Form
{
public string[] fulldate; //Created in form 2
close(); //Need to get this string back on or before close event
}
How should I go about doing this?
You need to handle the Form2 instance's FormClosed event in the first form and access the public properties.
Using this snippet, you can get a little knowledge on it.
in Form1:
using(Form2 form2 = new Form2())
{
if(form2.ShowDialog() == DialogResult.OK)
{
MessagBox.Show(form2.fulldate);
}
}
In Form2:
public partial class Form2 : Form
{
public string[] fulldate {get; set;} // Create a Property
void CloseForm()
{
fulldate = "valueToReturn";
DialogResult = DialogResult.OK;
}
}
Assuming you want button3_Click to wait until AcqForm is closed before accessing fulldate, you'll first have to change AcqForm.Show(); to AcqForm. ShowDialog();. (Show() doesn't wait for it to close.)
As for the fulldate field – it will be accessible even after the form closes because the form still exist. SLaks' remark about it being a 'field' means that we have different names for different types of 'variables'. What you have declared in the class (without get and set) is called a field. A variable in a method is called a 'variable'.
If you still want to do something when AcqForm closes, do this:
In Form1's constructor, before the AcqForm. ShowDialog();:
AcqForm.FormClosing += AcqForm_FormClosing;
And in Form1's class:
void AcqForm_FormClosing(object sender, FormClosingEventArgs e)
{
//Whatever will be here will be done when the form is closing.
//Use 'FormClosed' for doing things AFTER the form has closed.
}
Related
I try to update Form1 values from Form2, but nothing happens. No errors also, strange...
Form1:
public static Form2 f2{ get; set; } = new Form2();
private void addButton1_Click(object sender, EventArgs e)
{
f2.Show();
}
Form2:
public void button1_Click(object sender, EventArgs e)
{
Form f1 = new Form();
f1.label2.ForeColor = Color.Red;
}
Nothing happens. I can get data to my form2 from form1, but I cannot send, I mean I can, but nothing happens... Why? Thank you
The other solutions mentioned here would work for your specific case, but I encourage you to look at the big picture, and design a generalized solution that will work for most cases.
Your problem essentially boils down to doing something on one form based on the events of another form.
The best approach to do this is, in my opinion:
Let Form1 do all its own actions.
Let Form2 do all its own actions.
If you need to do something on Form1 based on an event that occurred on Form2, let Form2 notify Form1 that something happened there, so go do your own thing.
If necessary, pass data from Form2 to Form1.
So I would make use of delegates for this purpose.
Imagine you have a Button and a Label on your Form1. Clicking the button opens up Form2, on which you have another Button. Clicking this button on Form2 should change the background color of the label on Form1. So our setup would look like this. You haven't mentioned if it's Winforms or WPF, so I'm using WPF for my convenience but the idea is the same in either.
Form1
Form2
In my Form1 I'd declare a public delegate with a signature like this:
public delegate void NotifyEvent();
That is, this delegate represents a method that takes in no parameters, and has void return type. The idea is to let Form2 'call' a method in Form1, and that method essentially notifies the button was clicked on Form2. So, if there was a way for us to call a method that resides in the Form1 from Form2, we can then notify Form1 of an event happening on Form2 With me so far?
Now, if I write a method like this in the Form1, and let it be called from Form2, that should accomplish our goal. Here, lblDisp is the Label in Form1.
public void ButtonClickedOnForm2()
{
lblDisp.Background = new SolidColorBrush(Colors.LawnGreen);
}
To accomplish this, I would define a delegate of type NotifyEvent in Form1 like below, and register the ButtonClickedOnForm2() method to it. Your Form1 code behind should look like this
public delegate void NotifyEvent();
public partial class Form1 : Window
{
public NotifyEvent notifyDelegate;
Form2 form2 = null;
public Form1()
{
InitializeComponent();
// This is 'registering' the ButtonClickedOnForm2 method to the delegate.
// So, when the delegate is invoked (called), this method gets executed.
notifyDelegate += new NotifyEvent(ButtonClickedOnForm2);
}
public void ButtonClickedOnForm2()
{
lblDisp.Background = new SolidColorBrush(Colors.LawnGreen);
}
private void BtnOpen_Click(object sender, RoutedEventArgs e)
{
// Passing the delegate to `Form2`
form2 = new Form2(notifyDelegate);
form2.Show();
}
}
Accordingly, now we need to modify our Form2. We need to tell us which delegate to invoke when the button click happens. So to do that, I'd pass the delegate in the constructor of Form2 like so:
public partial class Form2 : Window
{
NotifyEvent notifyDel;
public Form2(NotifyEvent notify)
{
InitializeComponent();
notifyDel = notify;
}
private void BtnOK_Click(object sender, RoutedEventArgs e)
{
// This invokes the delegate, which in turn calls the ButtonClickedOnForm2 method in Form1.
notifyDel.Invoke();
}
}
Now, when the button is clicked on Form2, it invokes the delegate. And on our Form1, we've told it that in case the delegate is invoked, it should go ahead and execute the ButtonClickedOnForm2 method. In that method, we've written code to change the background color of the label. And that should solve your problem.
Passing Data
Additionally, if you want to pass data from Form2 to Form1, you can simply add parameters to the delegate definition. Say you want to pass a string from Form2 to Form1. Then, you'd change your delegate to look like this:
public delegate void NotifyEvent(string data);
And the method ButtonClickedOnForm2 like so:
public void ButtonClickedOnForm2(string data)
{
lblDisp.Content = data;
lblDisp.Background = new SolidColorBrush(Colors.LawnGreen);
}
Then on Form2, invoke the delegate by passing a string like so:
private void BtnOK_Click(object sender, RoutedEventArgs e)
{
// This invokes the delegate, which in turn calls the ButtonClickedOnForm2 method in Form1.
notifyDel.Invoke("I am from Form2");
}
Now clicking the button on Form2 should change text and background color of label on Form1 like this:
With Form f1 = new Form(); you are creating a new form instead of accessing one which is already open and you don't call f1.Show(); therefore it will never be displayed. You need a reference to the one which is open. E.g. pass form 1 as constructor parameter to form 2.
In form 2:
public partial class Form2 : Form
{
private readonly Form1 _form1;
public Form2 (Form1 from1)
{
_form1 = form1;
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
_form1.label2.ForeColor = Color.Red;
}
}
In form 1:
// Cannot be static as we need a reference to `this`
private Form2 _f2;
public Form2 f2 {
get {
if (_f2 == null) {
_f2 = new Form2(this); // Pass form 1 as parameter to form 2.
}
return _f2;
}
}
private void addButton1_Click(object sender, EventArgs e)
{
f2.Show();
}
Thank you for constructor idea. Anyway (this) does not work for me in global scope. So I needed to simplify it to this:
Form 2 with constructor:
public Form1 _form1 { get; set; }
public Form2(Form1 form1)
{
_form1 = form1;
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
_form1.label2.ForeColor = Color.Red;
}
}
Form 1 creating new object with parameter everytime call needed(because of 'this'):
private void addButton1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
f2.Show();
}
Works fine so far...
try this code
Form1:
private void addbutton1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
f2.Show();
}
public void ColorChange()
{
label2.ForeColor = Color.Red;
}
Form2:
public Form1 f1;
public Form2(Form1 m)
{
this.f1 = m;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
f1.Show();
f1.ColorChange();
}
In my application I have two forms (see image below). Clicking on "Add" button shows the second "Add wireless network" form.
After filling the form and clicking on "OK", second form adds the new profile and updates the wifi network profiles in the first form. There is a RefreshProfiles function in the first form and I call it in the second form with this:
((MainForm)this.Owner).RefreshWiFiProfiles();
and the "Add" button's code is this:
private void AddButton_Click(object sender, EventArgs e)
{
NewNetworkForm newNetworkForm = new NewNetworkForm();
newNetworkForm.Owner = this;
newNetworkForm.ShowDialog();
}
This setup is working fine, but since the number of lines increased as I added new things, I wanted to divide the code. So I created a class to contain some functions.
namespace WirelessNetworkManager
{
public class Tools
{
public static void RefreshWiFiProfiles(ListView ListViewControl)
{
// clear old list
ListViewControl.Items.Clear();
// update it
}
}
}
I call the method in the first form with this:
Tools.RefreshWiFiProfiles(ProfilesListView);
and it's working fine. The problem is, since I need to update the profiles list from the second form too, I need to call this in NewNetworkForm. I can access and pass the ProfilesListView in MainForm because it's in there. How can I pass a control that is in MainForm to a method in NewNetworkForm and modify it? Or is there a better approach to do this?
File structure
MainForm.cs (WirelessNetworkManager.MainForm)
NewNetworkForm.cs (WirelessNetworkManager.NewNetworkForm)
Tools.cs (WirelessNetworkManager.Tools)
You can set the Modifiers property of your ProfilesListView in Form2 to Public or Internal (It's Private by default) then you can access ProfilesListView of Form2 like this : Form2.ProfilesListView
For example:
ProfilesList.Refresh(Form2.ProfilesListView);
Your mistake is : You are creating a new Form when you are using RefreshWiFiProfiles() method.
You should access to existing Form2, for example if Form2 is Owner of Form1, this code works :
Form ownerForm = (Form)this.Owner;
Tools.RefreshWiFiProfiles(ownerForm.ProfilesListView);
Here is the complete example:
Form1 declaration:
Add a ListView to Form1, Set it's Modifiers to Public and add some items in it.
Add a Button to show Form2
Add a Button to clear Form2 ListView
Form1 Code:
public partial class Form1 : Form
{
Form2 form2;
public Form1()
{
InitializeComponent();
}
private void btnShowForm2_Click(object sender, EventArgs e)
{
form2 = new Form2();
form2.Owner = this;
form2.Show();
}
private void btnClearForm2List_Click(object sender, EventArgs e)
{
Tools.RefreshWiFiProfiles(form2.lstViewOfForm2);
}
}
Form2 declaration:
Add a ListView to Form2, Set it's Modifiers to Public and add some items in it.
Add a Button to clear Form1 ListView
Form2 Code:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnClearForm1List_Click(object sender, EventArgs e)
{
Form1 form1 = (Form1)this.Owner;
Tools.RefreshWiFiProfiles(form1.lstViewOfForm1);
}
}
Declaration of Tools Class:
public static class Tools
{
public static void RefreshWiFiProfiles(ListView listView)
{
listView.Clear();
}
}
I tried implementing this. __curious_geek's answer and it worked fine.
MainForm.cs
namespace WirelessNetworkManager
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void AddButton_Click(object sender, EventArgs e)
{
// passing "this" (MainForm) to the second form
NewNetworkForm newNetworkForm = new NewNetworkForm(this);
newNetworkForm.ShowDialog();
}
}
}
NewNetworkForm.cs
namespace WirelessNetworkManager
{
public partial class NewNetworkForm : Form
{
public NewNetworkForm()
{
InitializeComponent();
}
// a local variable to reference the first form (MainForm)
private MainForm mainForm = null;
// a second overloaded constructor accepting a form
public NewNetworkForm(Form callingForm)
{
// mainForm now refers to the first form (MainForm)
mainForm = callingForm as MainForm;
InitializeComponent();
}
private void OKButton_Click(object sender, EventArgs e)
{
// create wifi profile with user input
// accessing the ListView using this.mainForm
Tools.RefreshWiFiProfiles(this.mainForm.ProfilesListView);
this.Close();
}
}
}
Tools.cs
namespace WirelessNetworkManager
{
public class Tools
{
public static void RefreshWiFiProfiles(ListView ListViewControl)
{
ListViewControl.Items.Clear();
// iterate through wifi profiles and populate ListViewControl
}
}
}
Also the control element's (ListViewControl) Modifiers should be set to Public or Internal. Otherwise you'll get an error.
'WirelessNetworkManager.MainForm.ProfilesListView' is inaccessible due to its protection level
In my project I have a Settings form and a Main form.
I'm trying to call the Main form's MasterReset function from the Setting form, but nothing happens.
The Master form's Masterreset function looks like this.
public void MasterReset()
{
DialogResult dialogResult = MessageBox.Show("Are you sure you want to perform master reset? All settings will be set to default.", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (dialogResult == DialogResult.Yes)
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string phonebook_path = path + "\\Phonebook\\Contacts.xml";
XmlDocument xDoc = new XmlDocument();
xDoc.Load(phonebook_path);
XmlNode xNode = xDoc.SelectSingleNode("People");
xNode.InnerXml = "";
xDoc.Save(phonebook_path);
listView1.Clear();
people.Clear();
}
else if (dialogResult == DialogResult.No)
{
return;
}
}
And I'm accessing it from the Settings form like this
private void btn_MasterReset_Click(object sender, EventArgs e)
{
Main f1 = new Main();
f1.MasterReset();
}
Why am I not seeing any results?
Do you know what composition over inheritance is?
In the form where you have MasterReset you should do something like this:
Llet's suppose that in your second form you have something like this, and let's suppose your "mainform" will be called "MasterForm".
public partial class Form1 : Form
{
private MasterForm _masterForm;
public Form1(MasterForm masterForm )
{
InitializeComponent();
_masterForm = masterForm;
}
}
Here's the code in your masterForm Class:
private void button2_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1(this);
}
Here's in your form1:
private void btn_MasterReset_Click(object sender, EventArgs e)
{
_masterForm.MasterReset();
}
Hope this helps!
This worked for me: In your Program class, declare a static instance of Main (The class, that is) called Form. Then, at the beginning of the Main method, use Form = new Main(); So now, when starting your app, use
Application.Run(Form);
public static Main Form;
static void Main() {
Form = new Main();
Application.Run(Form)
}
Now, calling a function from another form is simple.
Program.Form.MasterReset(); //Make sure MasterReset is a public void
namespace F1
{
// Method defined in this class
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//This method I would like to call in other form
public void function()
{
MessageBox.Show("Invoked");
}
// opening the new form using button click
private void OpenNewForm_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
}
}
// This is second form
public partial class Form2: Form
{
public Form2()
{
InitializeComponent();
}
// on button click Form1 method will be called
private void button1_Click(object sender, EventArgs e)
{
var mainForm = Application.OpenForms.OfType<Form1>().Single();
mainForm.function();
}
}
}
There are multiple solutions possible. But the problem itself arise from the bad design. If you need something to be accessed by many, then why should it belong to someone? If, however, you want to inform something about anything, then use events.
Your mistake is what you are creating another instance of form1, thus MasterReset is operating with form, which is not even shown.
What you can do:
Make (as Ravshanjon suggest) a separate class to handle that MasterReset (and maybe something else). But also add to it an event. form1 and form2 can both subscribe to it and whenever either of them call MasterReset - both will react.
Create form dependency (as BRAHIM Kamel suggested): when you create form2, then pass to it form1 instance (as constructor parameter or by setting public property), to be able to call public non-static methods of form1.
As a quick, but relatively legimate solution, make this method static:
private static Form1 _instance;
public Form1()
{
InitializeComponents();
_instance = this;
}
public static void MasterReset()
{
// alot of code
_instance.listView1.Clear();
// alot of code
}
this way you can call MasterReset from any other form like this Form1.MasterReset(). Disadvantage of this method is what you can not have more than one instance of form2 (which is more likely anyway).
I understand your problem, you can declare your function as public static void(also listView1 and people should be static too). Then when you want to call to like this:
private void btn_MasterReset_Click(object sender, EventArgs e)
{
Main.MasterReset();
}
I am working on a game that utilizes Windows Forms in C#. I want to be able to use the first form to call a second form. I have this working. Then I would like for the second form to send data back to the first form rather than creating a new instance of the first form. Can this be done? I know I need to have my properties set up so that I can set the variables from one form to the other. I am just not sure how to go about calling the first form without creating a new instance of it.
Is there a way that this can be done?
For example if I have Form A create an instance of Form B, can I have Form B do some work and send the data back to the original Form A without creating a new instance of Form A?
If you don't use the Data sent back Form A right away then you could use the Form_Closing event handler Form B and then a public property in Form B also.
In your Form A it could look like this:
public partial class FormA : Form
{
FormB frmB = new FormB(); // Instantiate FormB so that you could create an event handler in the constructor
public FormA()
{
InitializeComponent();
// Event Handler for Form Closing
frmB.FormClosing += new FormClosingEventHandler(frmB_FormClosing);
}
void frmB_FormClosing(object sender, FormClosingEventArgs e)
{
String fromFormB = frm2.FormBData; // Get Data from Form B when form is about to close
}
private void button1_Click(object sender, EventArgs e)
{
frmB.ShowDialog(); // Showing Form B
}
}
And in your Form B it could look like this:
private void button1_Click(object sender, EventArgs e)
{
// Let just say that the data is sent back once you click a button
FormBData = "Hello World!";
Close();
}
public String FormBData { get; set; }
It's hard to say without knowing your full requirements. But generally I go like this (Somewhat psuedo code).
Form2 dialogForm = new Form2();
if(dialogForm.ShowDialog() == DialogResult.OK)
{
this.PropertyOnForm1 = dialogForm.PropertyOnForm2
}
This ofcourse relies that your second form is a dialog. You will need to set the dialogresult buttons on Form2, and have a public property that will be accessed from Form1 once the dialog has been completed.
Let me know if this doesn't work and I'll write up a different answer.
Since you are creating Form2 in Form1, you can create a custom event in Form2 and subscribe to it in Form1 at the time that you create Form2, if you are returning information from Form2 when you are closing it then Edper's or MindingData's answers will work.
Here is a quick and dirty example using EventHandler<TEventArgs>
Form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.myCustomEvent += frm2_myCustomEvent;
frm2.Show();
}
void frm2_myCustomEvent(object sender, string e)
{
this.Text = e;
}
}
Form2
public partial class Form2 : Form
{
public event EventHandler<string> myCustomEvent;
int count;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
count +=1;
myCustomEvent(sender, count.ToString());
}
}
I have two forms. First, Form1 has a group box, some labels and a listbox. I press a button and new Form2 is opened and contains some text. I want to transfer the text in Form2 to the listbox in the Form1.
So far, what I have done is make modifier of listbox to public and then put this code in the button of Form2
Form1 frm = new Form1();
frm.ListBox.items.Add(textBox.Text);
But amazingly, this does not add any value. I thought I was mistaken with the insertion so I made the same procedure. This time, I made a label public and added textbox value to its Text property but it failed.
Any ideas?
Try adding a parameter to the constructor of the second form (in your example, Form1) and passing the value that way. Once InitializeComponent() is called you can then add the parameter to the listbox as a choice.
public Form1(String customItem)
{
InitializeComponent();
this.myListBox.Items.Add(customItem);
}
// In the original form's code:
Form1 frm = new Form1(this.textBox.Text);
Let's assume Form1 calls Form2. Please look at the code:
Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
frm.VisibleChanged += formVisibleChanged;
}
private void formVisibleChanged(object sender, EventArgs e)
{
Form2 frm = (Form2)sender;
if (!frm.Visible)
{
this.listBox1.Items.Add(frm.ReturnText);
frm.Dispose();
}
}
}
Form2:
public partial class Form2 : Form
{
public string ReturnText { get; set; }
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.ReturnText = this.textBox1.Text;
this.Visible = false;
}
}
The answer is to declare public property on Form2 and when form gets hidden. Access the same instance and retrieve the value.
Below code working perfect on my machine.
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.listBox1.Items.Add(textBox1.Text );//ListBox1 : Modifier property made public
f1.ShowDialog();
}
Ok, If you are Calling Sequence is like, Form1->Form2 and Form2 updates the value of Form1 then you have to use ParentForm() or Delegate to update the previous form.
Form1 frm = new Form1();
frm is now a new instance of class Form1.
frm does not refer to the original instance of Form1 that was displayed to the user.
One solution is, when creating the instance of Form2, pass it a reference to your current instance of Form1.
Please avoid the concept of making any public members like you said
>>i have done is make modifier of listbox to public and then in form2 in button code<<
this is not a good practice,on the other hand the good one is in Brad Christie's Post,I hope you got it.
This code will be inside the form containing myListBox probably inside a button click handler.
Form2 frm2 = new Form2();
frm2.ShowDialog();
this.myListBox.Items.Add(frm2.myTextBox.Text);
frm2.Dispose();