Can't access public textBox7 created in form1, while in form2

César Amorim

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();
Tim Schmelter

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can't use int array from Form1 in Form2

From Dev

Calling Form2's function from Form1 with Form1's public variables

From Dev

Passing Form1 textBox value to Form2 button

From Dev

Lock and Unlock textbox on form1, by command in form2

From Dev

How to access Form1 Function from Form2 without creating new Form();

From Dev

How to send data from Form2 to Form1 into the Form1 TextBox when the button is clicked in VB.NET?

From Dev

C# How to pause Form1 while Form2 is active?

From Dev

How to access non static method of form1 from form2

From Dev

passing data from textbox in form1 to textbox in opened form2 in visual basic

From Dev

Can't access instance of form created by the constructor of the current form

From Dev

Can't access instance of form created by the constructor of the current form

From Dev

Form1 execute Method when Form2 is closed doesn't work

From Dev

Take selected item from listbox in form2 and put it to Form1 to TextBox

From Dev

Value entered in textbox of form1 not passed to label of form2

From Dev

In my code username in the form1 should be displayed in the textbox of form2 on buttonclick

From Dev

I would like to control Form1 from Form2 (textbox,picturebox)

From Dev

in c#, I want to send Form1's string's value to appear in Form2's textBox when I click a button of Form1

From Dev

Why doesn't pressing ok in messagebox error message take you back to form1 instead of form2?

From Dev

I'm trying to access a public property from a different form however I can't seem to access it in the second form

From Dev

I'm trying to access a public property from a different form however I can't seem to access it in the second form

From Dev

Close Form2 and show Form1

From Dev

Get value of checkbox of Form1 from Form2

From Dev

Get GetItemCheckState (in Form2) of checkedCheckList (Form1)

From Dev

Get value of checkbox of Form1 from Form2

From Dev

Disable form2 to move out of form1

From Dev

C# - Form2 value to Form1

From Dev

Use XML file selected in Form1 in Form2

From Dev

Close Form1 If FileExists+ Open Form2

From Dev

Display variable from Form1 to Form2

Related Related

  1. 1

    Can't use int array from Form1 in Form2

  2. 2

    Calling Form2's function from Form1 with Form1's public variables

  3. 3

    Passing Form1 textBox value to Form2 button

  4. 4

    Lock and Unlock textbox on form1, by command in form2

  5. 5

    How to access Form1 Function from Form2 without creating new Form();

  6. 6

    How to send data from Form2 to Form1 into the Form1 TextBox when the button is clicked in VB.NET?

  7. 7

    C# How to pause Form1 while Form2 is active?

  8. 8

    How to access non static method of form1 from form2

  9. 9

    passing data from textbox in form1 to textbox in opened form2 in visual basic

  10. 10

    Can't access instance of form created by the constructor of the current form

  11. 11

    Can't access instance of form created by the constructor of the current form

  12. 12

    Form1 execute Method when Form2 is closed doesn't work

  13. 13

    Take selected item from listbox in form2 and put it to Form1 to TextBox

  14. 14

    Value entered in textbox of form1 not passed to label of form2

  15. 15

    In my code username in the form1 should be displayed in the textbox of form2 on buttonclick

  16. 16

    I would like to control Form1 from Form2 (textbox,picturebox)

  17. 17

    in c#, I want to send Form1's string's value to appear in Form2's textBox when I click a button of Form1

  18. 18

    Why doesn't pressing ok in messagebox error message take you back to form1 instead of form2?

  19. 19

    I'm trying to access a public property from a different form however I can't seem to access it in the second form

  20. 20

    I'm trying to access a public property from a different form however I can't seem to access it in the second form

  21. 21

    Close Form2 and show Form1

  22. 22

    Get value of checkbox of Form1 from Form2

  23. 23

    Get GetItemCheckState (in Form2) of checkedCheckList (Form1)

  24. 24

    Get value of checkbox of Form1 from Form2

  25. 25

    Disable form2 to move out of form1

  26. 26

    C# - Form2 value to Form1

  27. 27

    Use XML file selected in Form1 in Form2

  28. 28

    Close Form1 If FileExists+ Open Form2

  29. 29

    Display variable from Form1 to Form2

HotTag

Archive