Accessing form1 variables from another class how? - c#

how do I access form1 string variable from a different class?
public partial class Form1: Form
{
public Form1()
{
InitializeComponent();
}
public string deva = "123";
//button
private void button8_Click(object sender, EventArgs e)
{
deva = "456";
}
private void button9_Click(object sender, EventArgs e)
{
Other ks = new Other();
ks.test_me();
}
}
public class Other: Form1
{
//trying to access Form1 variable.
public void test_me()
{
Form1 fm = new Form1();
MessageBox.Show(fm.deva);
//deva is 123 but not 456.
//I clicked on button and values changes it form1 however from here it assigns just default value
}
//
//Does creating a new form1 will reset its values?
//Somebody please help me. how to solve this issue.
}

public partial class Form1: Form {
public Form1()
{
InitializeComponent();
}
public string deva = "123";
//button
private void button8_Click(object sender, EventArgs e)
{
deva = "456";
}
private void button9_Click(object sender, EventArgs e)
{
Other ks = new Other(this);
ks.test_me();
}
}
no need to inherit from form1, please pass the object via constructor
public class Other {
Form1 obj = null;
public Other(Form1 object)
{
this obj = object;
}
public void test_me()
{
MessageBox.Show(obj.deva);
}
}

Make your variable deva Static. Access it with Class directly not object.
public static string deva = "123";
public void test_me()
{
//Form1 fm = new Form1();
MessageBox.Show(Form1.deva);
}

Answer on the title question.
Read Jon Skeet's comment for explanation of reason why your approach not workiing.
If you want have access to the variables of another instance, then you need in someway have reference to that instance
One way pass it in the constructor of Other
public class Other: Form1
{
private readonly Form1 _Form1;
public Other(Form1 form1)
{
_Form1 = form1;
}
public void test_me()
{
MessageBox.Show(_Form1.deva);
}
}
Then where you create new instance of Other pass instance of your Form1 ti the constructor of Other
public class Form1
{
private void button9_Click(object sender, EventArgs e)
{
Other ks = new Other(this);
ks.test_me();
}
}

default value is set a every new instance
if you want to keep last value you make a static property
public static string deva = "123";

Related

C# Use instance of a class made in Form 1 in Form 2

I want to Use an instance of a class made in Form 1 in Form 2 (i changed it to a list for simplicity of example code:
Not only that, I want Form 2 to be able to modify it (Clear it at some point).
The advice I got was this, although I was not told how due to "no spoonfeeding allowed"
namespace automationControls.FileTime
{
public class Form_Main : Form
{
public List<string> folderList; //<---- i want to access this.....
private void button_showForm2_Click(object sender, EventArgs e)
{
Form_Log ConfirmBoxForm = new Form_Log(this);
ConfirmBoxForm.Show();
}
}
//form_Main opens form_Log
namespace automationControls.FileTime
{
public partial class Form_Log : Form
{
public Form_Log(Form_Main _f1)
{
InitializeComponent();
}
private void Form1_FormClosing(Object sender, FormClosingEventArgs e)
{
How.Do.I.AccessForm_Main.folderList.Clear();//<---- ............. in this function
}
}
}
Answered:In the constructor of Form_Log, store the reference to _f1 somewhere you can access it from elsewhere in Form_Log
Why don't you use the constructor that you have already added your form?
private Form_Main _mainForm;
public Form_Log(Form_Main _f1)
{
InitializeComponent();
_mainForm = _f1;
}
private void Form1_FormClosing(Object sender, FormClosingEventArgs e)
{
var myList = _mainForm.folderList;
}
Try This,
public class Form_Main : Form
{
public List<string> folderList; //<---- i want to access this.....
private void button_showForm2_Click(object sender, EventArgs e)
{
Form_Log ConfirmBoxForm = new Form_Log(this);
ConfirmBoxForm.Show();
}
}
Form log :
public partial class Form_Log : Form
{
private Form_Main _mainForm;
public Form_Log(Form_Main _f1)
{
InitializeComponent();
_mainForm = _f1;
}
private void Form1_FormClosing(Object sender, FormClosingEventArgs e)
{
_mainForm.folderList.Clear();
}
}
I don't know how advanced is your project but in this situation i would use delegates. Here is how i would do it:
public delegate void ModifyCollectionHandler(string parameter);
public delegate void ClearCollectionHandler();
public partial class Form1 : Form
{
public List<string> folderList;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form = new Form2()
form.ClearItem+=form_ClearItem;
form.AddItem+=form_AddItem;
form.DeleteItem+=form_DeleteItem;
}
void form_DeleteItem(string parameter)
{
if (folderList == null)
return;
folderList.Remove(parameter);
}
void form_AddItem(string parameter)
{
if (folderList == null)
folderList = new List<string>();
folderList.Add(parameter);
}
void form_ClearItem()
{
if (folderList != null)
folderList.Clear();
}
}
public partial class Form2 : Form
{
public event ModifyCollectionHandler AddItem;
public event ModifyCollectionHandler DeleteItem;
public event ClearCollectionHandler ClearItem;
public Form2()
{
InitializeComponent();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
if (ClearItem != null)
ClearItem();
}
}
I hope I helped you :)
Best regards
in the Form1 put this :
public static List<string> folderList;
you can simply call it from any form ex:Form2 like this :
From1.folderList.Clear();

how to call non-static method on form1 from form2

can sameone provide example code on how to call non-static method on form1 from form2.
form1
public Form1()
{
InitializeComponent();
}
public void prikazi()
{
MessageBox.Show("ok");
}
private void openf2_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
}
form2
public Form2()
{
InitializeComponent();
}
private void callMethod_Click(object sender, EventArgs e)
{
// this don't work. If I change to public static void on form1 then it work's but I need non-static example
Form1.prikazi();
}
thanks
It doesn't matter if it's a form class, if you want to access a non-static method, there is no other alternative then to create an instance of the class.
But - It doesn't make sense.. so don't do it
Find other alternatives, like creating the method you need static in a common place, or consider adding this method (or a variation of it) to the form
form1
public Form1()
{
InitializeComponent();
}
public void prikazi()
{
MessageBox.Show("ok");
}
private void openf2_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.Show();
}
form2
private Form1 parentForm;
public Form2(Form1 parentForm)
{
this.parentForm = parentForm;
InitializeComponent();
}
private void callMethod_Click(object sender, EventArgs e)
{
parentForm.prikazi();
}
But better learn to bundle reusable code in to a separate class, rather than passing around form instances.
You need to have an instance of the form to call the method.
There are a few ways that you can make this work
1) Pass an action through to the new form
public Form2()
{
InitializeComponent();
}
public Action yourAction {get; set;}
private void callMethod_Click(object sender, EventArgs e)
{
Action instance = yourAction;
if(instance != null)
instance();
}
then in Form 1 you can say
private void openf2_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.yourAction = prikazi;
frm.Show();
}
2) You can pass an instance of Form1 into Form 2
So in Form 2 you have:
public Form1 ParentForm {get; set;}
private void callMethod_Click(object sender, EventArgs e)
{
if (ParentForm != null)
ParentForm.prikazi();
}
And Form1 you assign a value to the ParentForm variable
private void openf2_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.ParentForm= this;
frm.Show();
}
public partial class Form1 : Form
{
internal static Form1 ViewForm1; // make other form run Public void
public form1()
{
InitializeComponent();
ViewForm1 = this; //Add this
}
public void DoSomething()
{
//Code...
}
}
......................
public partial class Form1 : Form
{
public form2()
{
InitializeComponent();
Form1.ViewForm1.ShowData(); // call public void from form1
}

No errors, label seems like it should change to the indicated text but its not

Can anybody tell me why this is doing nothing? pcNameLabel.Text is supposed to be changing to bob when StatTransfer() is called by FighterButtonClick. According to the debugger everything is working right.
I've taken out some extra variables and stuff unrelated to the problem at hand.
public partial class MainForm : Form
{
public static string VariableLabel1;
public static string Variable2;
Random _r = new Random();
public MainForm()
{
InitializeComponent();
}
void CLoop()
{
while(true)
{
SetInfo();
}
}
public void SetInfo()
{
this.pcNameLabel.Text = VariableLabel1;
}
void ChClassButtClick(object sender, EventArgs e)
{
CharStats form = new CharStats();
form.Show();
}
}
This is a seperate windows form window.
public partial class CharStats : Form
{
public CharStats()
{
InitializeComponent();
}
void StatTransfer()
{
MainForm Mform = new MainForm();
MainForm.VariableLabel1 = "Bob";
Mform.SetInfo();
}
void FighterButtonClick(object sender, EventArgs e)
{
Fighter();
StatTransfer();
}
}
In these lines
void StatTransfer()
{
// This is a new instance of MainForm, not the original one
MainForm Mform = new MainForm();
MainForm.VariableLabel1 = "Bob";
Mform.SetInfo();
}
you create a new instance of MainForm and this instance is never displayed. This hidden instance contains the label that you are trying to change, but you cant't see it.
The simplest workaround to the problem is to pass the calling instance of MainForm to CharStats form when you initialize it
void ChClassButtClick(object sender, EventArgs e)
{
CharStats form = new CharStats(this);
form.Show();
}
Now you should change the constructor of CharStats to receive the passed instance and save it in a global variable inside the CharStats class
public partial class CharStats : Form
{
private MainForm _callingForm;
public CharStats(MainForm callingForm)
{
InitializeComponent();
_callingForm = callingForm;
}
.....
And use this saved instance where you need it
void StatTransfer()
{
_callingForm.VariableLabel1 = "Bob";
callingForm.SetInfo();
}
}
EDIT By the way, you dnn't need to use static variable for this to work. Simply change the method MainForm.SetInfo to receive a string and pass Bob when you call it
public void SetInfo(string newText)
{
this.pcNameLabel.Text = newText;
}
From CharStats
void StatTransfer()
{
callingForm.SetInfo("Bob");
}
MainForm is not set to display anywhere. I believe you want to add it to your CharStats form like so:
void StatTransfer()
{
MainForm Mform = new MainForm();
MainForm.VariableLabel1 = "Bob";
Mform.SetInfo();
this.Controls.Add(Mform);
}

Accessing Form's Control from another class C#

I'm a newbie in c# and visual studio, but not programming in general.
I searched for answer to my question for 3 days and I found plenty of them, but for some weird reason (I'm sure I'm missing something very obvious) I cannot get it to work.
I think it's the most basic question newbies like me ask.
I have a form (Form3) with a text box and a button (I set it up is just for testing purposes).
I want to populate and read this text box from another class. I understand the most proper way to do this is to create a property in Form3.cs with GET and SET accessors. I did that but I cannot get it to work. I'm not getting any error messages, but I'm not able to set the value of the text box either. It just remains blank.
Here's my sample code:
namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
public string setCodes
{
get { return test1.Text; }
set { test1.Text = value; }
}
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{ }
private void button1_Click(object sender, EventArgs e)
{
a.b();
}
}
public class a
{
public static void b()
{
Form3 v = new Form3();
v.setCodes = "abc123";
}
}
}
Can someone lend me a hand solving this?
The problem is you are setting the value to a new instance of the form. Try something like this:
public partial class Form3 : Form {
public string setCodes
{
get { return test1.Text; }
set { test1.Text = value; }
}
private A a;
public Form3()
{
InitializeComponent();
a = new A(this);
}
private void button1_Click(object sender, EventArgs e)
{
a.b();
}
private void Form3_Load(object sender, EventArgs e)
{
}
}
public class A
{
private Form3 v;
public a(Form3 v)
{
this.v = v;
}
public void b()
{
v.setCodes = "abc123";
}
}
You're creating a brand new Form3() instance.
This does not affect the existing form.
You need to pass the form as a parameter to the method.
Try this:
public partial class Form3 : Form
{
/* Code from question unchanged until `button1_Click` */
private void button1_Click(object sender, EventArgs e)
{
a.b(this);
}
}
public class a
{
public static void b(Form3 form3)
{
form3.setCodes = "abc123";
}
}
This passes the current instance of the form to the other class so that it can update the setCodes property. Previously you were creating a new form instance rather than updating the current form.
Sending form instance to other other class
Form1 objForm1=new Form1();
obj.Validate(objForm1);
Easy way to access controls in another class by modifying Controls Private to Public in the Form(Designer.cs)

Creating a class object in C#

I am trying to create an object of a class, but it doesn't seem to work, I can't help but think I am looking at this from a JAVA perspective:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
PortChecks PortCheckObject = new PortChecks();
}
private void testCheck_Click(object sender, EventArgs e)
{
PortCheckObject.MyMethod();
}
I can error when using the PortCheckObject to call my method MyMethod
(PortChecks is the class name)
It's because it's outside of the scope of testCheck_Click
public partial class Form1 : Form
{
PortChecks PortCheckObject = new PortChecks();
public Form1()
{
InitializeComponent();
}
private void testCheck_Click(object sender, EventArgs e)
{
PortCheckObject.MyMethod();
}
}
PortChecks PortCheckObject in Form1 constructor is a local variable.
Put its declaration as a private field in Form1 class.
public partial class Form1 : Form
{
private PortChecks PortCheckObject = new PortChecks();
public Form1()
{
InitializeComponent();
}
private void testCheck_Click(object sender, EventArgs e)
{
PortCheckObject.MyMethod();
}
}
#James,
You need a class property with the name 'PortCheckObject' and can be possible to access in other parts of the class.
public partial class Form1 : Form
{
private PortChecks PortCheckObject;
public Form1()
{
InitializeComponent();
PortCheckObject = new PortChecks();
}
private void testCheck_Click(object sender, EventArgs e)
{
PortCheckObject.MyMethod();
}
}
This is a general scope issue, not a Java v.s. C# issue (as your code wouldn't work in Java either). PortCheckObject is in Form1()'s scope, not testCheck_Click's scope. Try the following:
public partial class Form1 : Form
{
private PortChecks PortCheckObject;
public Form1()
{
InitializeComponent();
PortCheckObject = new PortChecks();
}
private void testCheck_Click(object sender, EventArgs e)
{
PortCheckObject.MyMethod();
}
This is an instance of a scope problem. You do not have scope in your testCheck_Click method. Make the following change and it should work:
public partial class Form1 : Form
{
private PortChecks MyPortCheck {get; set;}
public Form1()
{
InitializeComponent();
MyPortCheck = new PortChecks();
}
private void testCheck_Click(object sender, EventArgs e)
{
MyPortCheck .MyMethod();
}
...
}

Categories

Resources