How to carry a value in a class through multiple forms - c#

I have a value in my first form of a game which I would like to access on the next 2 forms. This value is saved in a private class. How would I go about doing this?
All help is greatly appreciated.

Create a new constructor in your target Winform class that takes the value as a parameter. Do the same for the subsequent form.
private MyType _value;
public MyForm(MyType myValue) : this()
{
_value = myValue;
}
To show the new form from your original form:
var form = new MyForm(someValue);
form.Show(); // or ShowDialog()

Use get/set to define your value:
Class Form1
int _x; // value you want to access from other class/form
public int YourNumber;
{
get
{
return this._x;
}
set
{
this._x= value;
}
}
Now to access/modify from other class for example Form2:
Class Form2
Form1 form = new Form1();
form.YourNumber = 5; // and it's changed in Form1 also
int y = form.YourNumber; // and you get it from Form1

Use Global Variable concept so that you can access across multiple forms. You can find details from
How to make global variables?
Example:
http://dotnetmirror.com/Articles/wpf/116/global-variables-in-wpf-winforms-mvvm

Related

Check values in other forms

I am trying to pass the value of an int variable created in Form1 to another form. I tried by making the variable public and make this public partial class extended by Form1, but it copies all elements Form1 to the other form. Is there another way? Thanks
Write a constructor by overloading
//called form code
public Form5(string Work_Order)
{
InitializeComponent();
string wo=Work_Order;
}
// calling form code
Form5 f5 = new Form5(Work_Order1);
f5.Show(); //or f5.ShowDialog();
Pass the value through a parameterized constructor
if you're in Form1 and you need to pass a value to other form use this instead.In Form1:
int a = 7;
form2 f = new form2(a);
f.show();
and in form2 make a constructor
int a;
public form2(int num){
this.a = num;
}
and use this variable 'a' throughout the form2.

in my c# application the other form are set in form.default.show but the other new forms are not setting

forms are set as 'form1.Default.Show()' but the new form are not set. tell me how to do that to show form by setting the new form as form2.default.show()
It appears you want to have some kind of static access to the Form by using the Default Property?
Inside your Form code behind add this (note: this is not thread safe)
private static Form1 instance;
public static Form1 Default
{
get
{
if (instance == null) {
instance = new Form1();
instance.FormClosed += delegate { instance = null; };
}
return instance;
}
}
Now you can call the Form like this:
Form1.Default.Show();
First pic showing error:
2nd pic i right coding:

Pass value from parent form to child form?

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.

getting textbox value from another form

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);
}
}

Passing values between two windows forms

The case is this, I have two different forms from the same solution/project. What I need to do is to extract the value of a label in Form A and load it into Form B. As much as possible, I am staying away from using this code since it will only conflict my whole program:
FormB myForm = new FromB(label.Text);
myForm.ShowDialog();
What I am trying right now is a class with a property of get and set for the value I wanted to pass. However, whenever I access the get method from FormB, it returns a blank value.
I hope somebody can help me with this. Any other ways to do this is extremely appreciated. :)
public class Miscellaneous
{
string my_id;
public void SetID(string id)
{
my_id = id;
}
public string GetID()
{
return my_id;
}
}
You could do something like this:
Child form
public string YourText { get; set; }
public TestForm()
{
InitializeComponent();
}
public void UpdateValues()
{
someLabel.Text = YourText;
}
Initiate it
var child = new TestForm {YourText = someTextBox.Text};
child.UpdateValues();
child.ShowDialog();
With this approach you don't have to change the Constructor, you could also add another constructor.
The reason for them being empty is that the properties are set after the constructor, you could Also do someting like this to add a bit of logic to your getters and setters, However, I would consider not affecting UI on properties!
private string _yourText = string.Empty;
public string YourText
{
get
{
return _yourText;
}
set
{
_yourText = value;
UpdateValues();
}
}
In this case, the UI will be updated automaticly when you set the property.
You can use a static variable/method to hold/pass the value of a control (when it gets changed).
You can use form reference or control reference to get and pass values directly.
You can use custom event for that (notifying the code that subscribed).
btw. FormB myForm = new FromB(label.Text); did not work because you are passing by value and the value was empty at the moment of creation of FormB.
FormB myForm = new FromB(label); would have worked.
Well one approach to take is to create a singleton class in your application. When you form b loads or the label changes you update the singleton with the value. Then when form a needs the value it can just get the instance of the singleton within your application and it will have that value.
There are probably cleaner ways to do it but just thinking of an easy way to pass information back and forth and store any information needed for both forms.
EDIT: Here is an example of a singleton that I pulled from here:
http://www.yoda.arachsys.com/csharp/singleton.html
public sealed class Singleton
{
static readonly Singleton instance=new Singleton();
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Singleton()
{
}
Singleton()
{
}
public static Singleton Instance
{
get
{
return instance;
}
}
}
Now all you need to do is put this class in a namespace that is accessible to both forms and then you can call the Instance property of this class and then reference your values. You can add properties to it as well for whatever you want to share. When you want to retrieve those values you would call it like this:
Singleton.Instance.YourProperty
((Form2)Application.OpenForms["Form2"]).textBox1.Text = "My Message";
declare public property varible in second form
Public property somevariable as sometype
and access it in first form using instance
Dim obj as New form2()
obj .somevariable ="value"

Categories