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!
Related
In C# I have two forms "Form1" and "Form2". Form1 creates images that are stored in a folder. Form2 displays the number of images in that folder.
Say I have made 2 images with Form1 and then I open Form2. Form2 now says there are 2 images. Now while keeping both forms open I want to be able to add a new image and Form2 updates. At the moment if I use Form1 to add more images while Form2 is open Form2 continues to display the number of images that were in the folder when Form2 opened.
I have found solutions that involve Form2 closing and reopening but I don't want this. It's jarring for the user having windows opening and closing every time they press a button. I just want Form2 to update live as I make changes with Form1.
Any help would be greatly appreciated!! Thanks!
You can do this with public method in Form2
In Form1 you need to save Form2 object in property
FORM 1
public partial class Form1 : Form
{
public Form2 MyProperty { get; set; }
public Form1()
{
InitializeComponent();
}
// for opening form 2
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
MyProperty = f2;
}
// adding new image
private void button_ADD_Click(object sender, EventArgs e)
{
MyProperty.updateCounter();
}
}
When you add new image, then you can call the metoh from Form2 to update counter.
In FORM 2 you need crate PUBLIC method to update counter
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
// default value
label1.Text = "0";
}
// update counter
public void updateCounter()
{
label1.Text = (int.Parse(label1.Text) + 1).ToString();
}
}
You may want to take a look at events and delegates. Basically, whenever you do something to Form1 that can affect other forms, you want to trigger an event. In Form2, you have a method which you call whenever that event triggers. This way you can have multiple forms open, and as long as you set them to "listen" to a given event, they can all react.
You can read more about them here, in the Microsoft Documentation.
I'm trying to pass a value set in my parent form to a second form. I created a property with a get part in the parent form.
I don't want to do something like:
Form2 secondForm = new Form2(value);
It is an already exiting form and I don't want to keep creating a new form every time I want to pass a value.
See this example.
1-Create a window form application,Declare a public string global variable in Form1 , using this variable we can pass value from Form1 to Form2.
2-Now in form2 ,Create a object for Form1 and Get the value using this object.
See image
You have some possibilities here:
Give a Reference from your first Form as value
Form2 secondForm = new Form2(yourForm1);
So you can access via the getter in your first Form. yourForm1.MyValue;
This seems to be a bit ugly. Better is you create a Interface, which hold your Property and is implemented from you first Form.
public interface IValueHolder
{
public int MyValue {get;}
}
public class FirstForm : Form, IValueHolder
{
public int MyValue{get;}
//Do your form stuff
Form2 form = new Form2(this);
}
So your Form2 just take the Interface and stays independent from Form1. Further you can create a Property on Form2 which you access from Form1. For example if your Property in Form1 changes you set the value from Form2 as well.
public class Form2 : Form
{
public int MyValue{get;set;}
}
public class Form1 : Form
{
private int _myValue;
public int MyValue
{
set
{
if (_myValue != value)
{
form2.MyValue = value;
}
}
}
}
At least you can use a Event maybe. Further you can create a Property on Form2 which holds an Form1 Reference or a IValueHolder as described above.
Hope this helps.
I'm not sure about how you are going to use the Form2 in the parent(let it be frmParent). anyway you can follow any of the steps below:
Define the property in the child form as static so that you can access that by using Form2.frmProperty.
Or define the property as public and then access through the class instance, so that you can access the variable through that instance as long as the instance existed. something like the following:
Form2 secondFormInstance = new Form2();
secondFormInstance.frmProperty = 10;
// at some later points
int childValue = secondFormInstance.frmProperty; // take value from that variable
secondFormInstance.frmProperty++; // update the value
Or you can use like what you specified in the question.
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 a program with two forms.
The second form, Form2 in which I want a few labels initialized with values from the main form.
The code:
public Form2()
{
InitializeComponent();
Form1 mainForm = (Form1)this.Owner;
lblName.Text = mainForm.gvRow.Cells[2].Value.ToString();
lblItemType.Text = mainForm.gvRow.Cells[1].Value.ToString();
lblLocation.Text = mainForm.gvRow.Cells[3].Value.ToString();
}
For some reason this does not work in the Form2() section, this.Owner is null. But if I was to place the code in an event method it works just fine.
How can I fix that?
The second form shouldn't need to even know about your main form in the first place. Even if it did, it's an extremely bad idea to be reading into its internal controls.
Instead your second form should have public properties through which it can accept the data that your main form wants to provide to it, without exposing any of its internal controls, and the main form can set those properties using the data from its controls. You could also potentially use parameters to the constructor instead, if you have just a bit of data, and that is the only time you need to provide it.
public class Form2
{
public string Name
{
get { return lblName.Text; }
set { lblName.Text = value; }
}
}
public class MainForm
{
public void Foo()
{
Form2 child = new Form2();
child.Name = mainForm.gvRow.Cells[2].Value.ToString();
child.Show();
}
}
This code is executed when the Form2 form is created. The Owner isn't set yet (and, presumably, the data isn't present yet). If you put it in the VisibleChanged event handler - it will be executed when the Owner and data are (presumably) present.
Use the Load Event. The Owner is only initialized after you Show the form, which then in return raises the Load Event.
Owner isn't set until the form is shown - i.e. in ShowDialog, not during the constructor. You should pass the parent as a parameter in the constructor:
public Form2(Form1 mainForm)
{
InitializeComponent();
lblName.Text = mainForm.gvRow.Cells[2].Value.ToString();
lblItemType.Text = mainForm.gvRow.Cells[1].Value.ToString();
lblLocation.Text = mainForm.gvRow.Cells[3].Value.ToString();
}
That's because the Owner is not initialized yet in the Form2 constructor, set your code in your Form2_Load event
Use the Form.Show(IWin32Window) overload to pass the owner to the child form.
http://msdn.microsoft.com/en-us/library/szcefbbd(v=vs.110).aspx
You need to set the Owner property yourself
As an alternative you could pass a reference to Form1 to the Form2 constructor. In the code that opens Form2 you probably have something like this:
var form2 = new Form2();
form2.Show();
You could replace that with:
var form2 = new Form2(this);
form2.Show();
In Form2 you'd add a constructor overload:
public Form2(Form1 owningForm)
{
InitializeComponent();
Form1 mainForm = owningForm;
lblName.Text = mainForm.gvRow.Cells[2].Value.ToString();
lblItemType.Text = mainForm.gvRow.Cells[1].Value.ToString();
lblLocation.Text = mainForm.gvRow.Cells[3].Value.ToString();
}
If different "owning forms" are possible you may need to define an interface instead of passing Form2.
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);
}
}