How can the lines mark with * in Form2 be referred back to Form1?
I mean when Form1 is instantiated, it can only be referenced by
the name Form1. But in the lines with *, Form1 is used as a type
not a object. However, you can use the m_parent as reference for
Form1 in the form called Form2
(I hope someone may get what I was trying to ask.)
public partial class Form2 : Form
{
* private Form1 m_parent;
* public Form2(Form1 frm1)
{
InitializeComponent();
* m_parent = frm1;
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
What you have there is a constructor for Form2 which takes a reference to a Form1 instance. On Form1 there may be code which looks something like:
var form = new Form2(this);
form.Show();
What is happening is you create Form2 with a reference to the current form and show it. It now has access to public members of Form1
Both Form1 and Form2 are normal classes that can be instantiated.
In the Form2 class there is a constructor wich takes an instance of Form1 object as parameter. In the constructor's body the parameter (the reference to the Form1 object) is then storen in m_parent variable.
Related
On Form1 I have a label with my total points. In Form2 I made a store were you can purchase things with those points. However I am able to pass the points value to Form2 but I can't figure out how I can send the points value back to Form1.
The code I use to send the value from Form1 to Form2.
Inventory inventory = new Inventory();
inventory.points = points.
I used the search function but since I just started writing code I find most of the given answers too confusing.
Code a constructor for form2 class as below:
public Form2(string strTextBox)
{
InitializeComponent();
label1.Text=strTextBox;
}
On Form1 where you want to call Form2 and pass a value
Form2 frm=new Form2(textBox1.Text);
frm.Show();
Add a property in Form1 to retrieve value from textbox:
public string _textBox1
{
get{return textBox1.Text;}
}
On Form2:
public string _textBox
{
set{label1.Text=value;}
}
Pass the Form1 instance to Form2 when setting the points.
This requires a Form1 public variable in Form2.
e.g
Inventory inventory = new Inventory();
inventory.points = points;
inventory.form1 = this;
and when sending back in Form2
this.form1.points = this.points;
I hope this helps!
I am running two forms simultaneously and I am trying to resize Form1 by calling a Form1 method with an event in Form2. With the following code the proper size values are displayed in the console, but the size of Form1 does not change. I have tried a number of approaches but I don't see why this does not work.
In Form1:
public void ResizeForm()
{
Console.WriteLine(this.Size.ToString());
this.Size = new System.Drawing.Size(600, 300);
}
In Form2:
private void ResizeCheckbox_CheckedChanged(object sender, EventArgs e)
{
Form1 form = new Form1();
form.ResizeForm();
}
You should pass the instance of the current Form1 to the second form. Add an instance in Form2 and then get it from Form1
Form2
Form1 _form1;
public Form2(Form1 form1)
{
InitializeComponent();
_form1 = form1;
}
private void ResizeCheckbox_CheckedChanged(object sender, EventArgs e)
{
_form1.ResizeForm();
}
Then open Form2 in the main form like this.
Form2 form2 = new Form2();
form2.Show((Form1)this); //I'm not sure if you need to cast "this" to From1
Form1 form = new Form1(); creates a new form, resizes it, and then forgets it. So, this is completely pointless. The ResizeForm() method does get invoked, but on the wrong instance of Form1. From your description, you should have some other instance of Form1 somewhere, the instance that you are actually displaying to the user. You need to access that instance from within Form2. If you do not have access to the correct instance of Form1 from within Form2, you must somehow pass it, so that Form2 has it. Creating a new instance of Form1 is not going to resize the original instance of Form1.
This is a common question that i've been finding hard to understand
I have a form2 and a form1
on form2 i created a public variable this way, on class form2: form
public partial class form2 : Form
{
public string xmlDialogconstante { get; private set; }
}
It's supposed to be available on form2 and form1
i'm trying to call her on form1 using the following
form2.xmlDialogconstante
It gets me an error everytime, saying a reference is required, what does it means?
That is an instance property you need to create an instance of Form2 to access xmlDialogconstante or make it static.
You can create an instance and access your property like this, but if you don't initialize it before accessing you will get null.
form2 f = new form2();
string value = f.xmlDialogconstante;
I have two forms and a datagridview which is in the form1.Im trying to add a new column by clicking in a button from form2.Like that:
Form2
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.dataGridView1.Columns.Add("test" , "test");
}
How can I do that?
Form1 form1 = new Form1();
This will not work as your real form1 is already there, I pressume. Instead create a reference to it in form2 and load it in form2's constructor!
Here are the steps:
the local reference to form1 in form2's variables: Form1 form1
When opening form2 pass a reference to form1 in the constructor:
form2 = new Form2(this);
Store it in the local refence in the constructor on form2:
public Form2(Form1 form1_)
{
InitializeComponent();
form1 = form1_;
}
Now you are all set to use Form1 and its public properties and controls. To use form1.dataGridView1 you must make it public first, though. (Or create a public reference to it..)
I have an TextBox named pass in Form1 that I need to get the value of in form2. I tried this:
public partial class Form1 : Form {
public string GetPass() {
return pass.Text;
}
}
public partial class form2 : Form {
//...
MessageBox.Show(new Form1().GetPass());
}
The above code returns an empty string, why?
You are not showing your actual code as evidenced by the syntax errors etc. - the only logical explanation for your problem is that you are not passing the reference to Form1 correctly to Form2, but create a new form instead - that new form would have the empty textbox.
To further help you, please show how you pass the reference to your Form1 in your actual code.
Edit:
Is see your edit now and above is exactly the problem. You have to pass a Form1 instance to form2 instead of creating a new one, i.e.:
public partial class form2 : Form
{
private Form1 form1;
public form2(Form1 otherForm)
{
form1 = otherForm;
}
public void Foo()
{
MessageBox.Show(form1.GetPass());
}
}
Define one string variable as Public in declaration section
for ex. we have a form with name "frmOne"
public string strVar = string.Empty;
Now, assign the value of TextBox of "frmOne" to that variable from where you are getting the value of Textbox.
for ex.
strVar = Textbox1.Text.ToString();
Now in another form say "frmTwo", you will get access the value of that textbox of "frmOne" something like that (where you want to get the value) :
frmOne frm = new frmOne();
string strValue = frm.strVar;
So, finally strValue local variable of frmTwo contains the value of Textbox of frmOne.
You are creating a NEW form1 where the textbox is likely to be blank, and calling GetPass() on that empty form. You need an instance of the already-opened form1 where the textbox might have a value.
Because you are creating a new instance of Form1 each time you call GetPass().
You need to get the instance of the opened form1 one way or another, and call GetPass on it:
form1.GetPass();
If there is no specifics on the order of creation of form1 and form2, you can use the following to get the instance of form1:
foreach (Form openedForm in Application.OpenForms) {
if (openedForm.GetType() == Form1) {
MessageBox.Show(openedForm.GetPass());
}
}
It's returning empty because you're creating a new instance of the form. Assuming that Form1 is already open somewhere, you need to retrieve the existing instance of Form1 and pull the value from there.
hi you can write this :
public partial class Form1: Form
{
public Form1()
{
InitializeComponent();
}
internal Form2 F2=new form2();
private void CommandBarButton1_Click(object sender, EventArgs e)
{
MessageBox.Show(f2.TextBox1.Text);
}
}