I have a textbox7 created in form1, but I want to access it in another form, form 2 so I changed its modifiers to public
When I get on form2 i want it to perform the following
Form1.textBox7.ReadOnly = true;
but I get the following error
Error 1 An object reference is required for the non-static field, method, or property 'WindowsFormsApplication1.Form1.textBox7' C:\Users\Admin\Google Drive\AdminhomeGD\ztese\zprograma\WindowsFormsApplication1\WindowsFormsApplication1\formOpConfig.cs 73 46 WindowsFormsApplication1
i have no idea what's wrong
Edit:
I'm trying to use the following, on form1 (named Form1):
public Boolean gammamanual
{
get { return textBox7.ReadOnly; }
set { textBox7.ReadOnly = value; }
}
while on form2 (named formOpConfig):
namespace WindowsFormsApplication1
{
public partial class formOpConfig : Form
{
public formOpConfig(Form1 opener)
{
// this is a constructor
this.Opener = opener;
}
private Form1 Opener { get; set; }
public formOpConfig() { // initialization and stuff }
private void buttonExit_Click (object sender, EventArgs e)
{
if (checkBox3.Checked == true) { this.textBox7.gammamanual = false; }
else { this.textBox7.gammamanual = true; }
this.Hide();
}
I want it so that when the checkbox3 is checked and the form2 exited, the textBox7 will be editable manualy.
Edit:2 the problem was that form2 was started from form1, also, i had 2 different constructors in form2 (formOpConfig). the solution was adapting the main constructor using
private Form1 Opener { get; set; }
public formOpConfig(Form1 opener)
{
this.Opener = opener;
InitializeComponent(); //Stuff
}
meanwhile i was able to start formOpConfig from the Form1 using
formOpConfig painelconfig = new formOpConfig(this);
painelconfig.ShowDialog();
You cannot access an instance variable via class name, that works only if it's static. So you need an instance of Form1
to access a property/field of it. You could for example pass it via constructor from Form1
to Form2
(if Form1
instantiates Form2
) or use a property/method.
But instead of exposing the complete control i would only provide what is necessary, that's called Encapsulation. You could expose a public property with a meaningful name that describes the purpose of textBox7
(change also this name). Presuming it's purpose is to show a username the property could be UserName
:
public partial class Form1 : Form
{
public string UserName
{
get{return TxtUserName.Text;}
set{ TxtUserName.Text = value;}
}
// ...
}
Since you want to get/set the ReadOnly
property of the TextBox
you could provide another property:
public bool UserNameEditable
{
get{ return !TxtUserName.ReadOnly; }
set{ TxtUserName.ReadOnly = !value; }
}
Now you are able to get/set it from another Form
if you have an instance of Form1
:
public partial class Form2 : Form
{
public Form2(Form1 opener)
{
// this is a constructor
this.Opener = opener;
}
private Form1 Opener { get; set; }
private void SomeMethod()
{
this.Opener.UserName = "Tim";
this.Opener.UserNameEditable = false;
}
// ...
}
You need to pass the Form1
instance to the constructor of Form2
when you instantiate it. Another approach is to make the property Opener
public and use that which suffers the disadvantage that you cannot be sure that it was set.
Another approach is using LINQ and Application.OpenForms
which is elegant but error-prone:
var form1 = Application.OpenForms.OfType<Form1>().FirstOrDefault();
form1.UserName = "Tim";
Side-note: Form1
and Form2
are also crying for more meaningful names.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments