I have Form and Class like that :
namespace ALTER_Control
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ALTER A = new ALTER();
A.ALTER();
}
}
public class ALTER
{
public Form1 F;
public void ALTER()
{
F.TextBox1.Text="I Altered That";
}
}
}
So i try to call ALTER() to change the textbox1 value in Form1 but i get that error :
object reference not set to an instance of an object
That happens only if i am accessing or modifying the Form1 Controls.
And by the way i set textbox1 modifier to public
So , finally i`d like to change the control value without getting that error.
You need to assign the reference to the form. Like this:
private void button1_Click(object sender, EventArgs e)
{
ALTER A = new ALTER();
A.F = this;
A.ALTER();
}
Why does your ALTER class (which isn't a great class name either) have to know about your form?
private void button1_Click(object sender, EventArgs e)
{
ALTER A = new ALTER();
this.TextBox1.Text = A.ALTER();
}
}
[...]
public class ALTER
{
public String ALTER()
{
// Do your thing
return "I Altered That";
}
}
Use these lines of code:
ALTER A = new ALTER();
A.F = this ;
A.ALTER();
Related
Hello I hope you can help me, I've been trying like more than 10 days to solve this problem.
I have a Form Application with 1 usercontrol and 1 Class and need use my class instance created in Form1 inside the UserControl1. (With Form1 to Form2 it's works fine)
Class CMensaje:
namespace WindowsFormsAppInstanciarClaseEnControl
{
public class CMensajes
{
private string mensaje;
public CMensajes()
{
}
public string Mensaje { get => mensaje; set => mensaje = value; }
}
}
UserControl1:
namespace WindowsFormsAppInstanciarClaseEnControl
{
public partial class UserControl1 : UserControl
{
CMensajes mensajito;
public UserControl1(CMensajes mensa)
{
InitializeComponent();
mensajito = mensa;
}
private void UserControl1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = mensajito.Mensaje;
}
}
}
Form1
namespace WindowsFormsAppInstanciarClaseEnControl
{
public partial class Form1 : Form
{
CMensajes mensajito = new CMensajes();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
mensajito.Mensaje = textBox1.Text;
UserControl1 usercontrol1 = new UserControl1(mensajito);
}
}
}
The problem is that it's works but intermediately I start to get
The variable mensajito is either undeclared or was never assigned. When I open Form1 Design. When I do the same code with Form1 to Form2 everything perfect!!
Really I need to pass a instance of my class serialport but is the same Here I write only a test code to understand what could I do?
Thanks.
Error Screen when I try to open Form1.designer.cs
For me you should do this inside the User Control constructor:
public UserControl1(CMensajes mensa)
{
InitializeComponent();
mensajito = new CMensajes();
mensajito = mensa;
}
Haven't got a machine in front of me to test this but at the risk of sounding silly, try adding a default constructor to your user control.
public UserControl1()
{
InitializeComponent();
}
Or try moving the instantiation of your class inside the Form constructor.
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";
I'm beginner in C# and having great difficulty to figure our some issues. So I hope my terminology does not matter. Here is my question. Let's say I have the following code:
namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//code starts
//...
//if(...) {
//...
//string parameter = abc.ToString();
//}
//code ends
}//Form1 ends
private void button1_Click(object sender, EventArgs e)
{
//code here
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = parameter;
button1.Perform();
}
}
}
I have difficulties here.
How can I use the string declared in Form1 called parameter inside button2_Click?
textBox1.Text = parameter; doesn't work.
Use a member variable.
public partial class Form1 : Form
{
private string parameter = null;
public Form1()
{
InitializeComponent();
// ...
parameter = abc.ToString();
}
There is another question that is very similar to mine however after reading it i still cannot get it to work.
I have two forms , MainForm and SecondForm and a few other classes, i need an instance of my AVLtree and be able to access it through my other forms.
This is what ive done so far
MainForm
public partial class MainForm : Form
{
AddArtist secondForm = new AddArtist();
public static AVLTree<Artist> treeAVL { get; set; }
public MainForm()
{
InitializeComponent();
}
private void butAdd_Click(object sender, EventArgs e)
{
secondForm.Show();
}
private void MainForm_Load(object sender, EventArgs e)
{
}
}
}
SecondForm
public partial class AddArtist : Form
{
String Name1 = "No Name";
int Members = 0;
public AVLTreetreeAVL = new AVLTree();
public AddArtist()
{
InitializeComponent();
treeAVL = MainForm.treeAVL;
}
private void MainForm_Load(object sender, EventArgs e)
{
}
private void butAdd_Click(object sender, EventArgs e)
{
Name1 = tBName.Text;
Members = (Convert.ToInt32(tBMem.Text));
Artist newArtist = new Artist(Name1,Members);
try
{
treeAVL.InsertItem(newArtist);
}
catch (Exception )
{
MessageBox.Show("No Data Entered", "Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
}
tBName.Text = "";
tBMem.Text = " ";
}
}
}
Any help would be greatly appreciated pointing out where im going wrong or how to solve it.
It now compiles however it gives an error of Object reference not set to an instance of an object. i hope ive gone about coding this is the right way.
What is the access modifier of AVLTree class? Check if it is private or internal, since your code needs it to be public.
Set public on your parametrized type
public class Artist
{
..
}
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)