hello,
I tried to change the value of a textbox from another window form.

the value is passed to another form properly, but I couldn't change the value in the textbox.

here is the code

Form1.cs

public void setTextbox(String sd)
        {

                    MessageBox.Show(sd);
                    msg.Text = "fd";


        }

additem.cs

 private void button1_Click(object sender, EventArgs e)
        {

            Form1 f = new Form1("dfs");
            f.setTextbox("df");

        }

Output:
msgbox shows df which means the data has been passed properly, but the next line "msg.Text = "fd";" returns nullpointer exception :
Object reference not set to an instance of an object.

THanks

Edited 3 Years Ago by Reverend Jim: Fixed formatting

hello,
I tried to change the value of a textbox from another window form.

the value is passed to another form properly, but I couldn't change the value in the textbox.

here is the code
Form1.cs
public void setTextbox(String sd)
{

MessageBox.Show(sd);
msg.Text = "fd";


}

additem.cs
private void button1_Click(object sender, EventArgs e)
{

Form1 f = new Form1("dfs");
f.setTextbox("df");

}

Output:
msgbox shows df which means the data has been passed properly, but the next line "msg.Text = "fd";" returns nullpointer exception :
Object reference not set to an instance of an object.

THanks

Messagebox is a class that you cannot instantiate and that has not property (Get,set) this is way you cannot edit the Textbox. It return a null pointer because you did not define the variable msg (msg.Text means set the Text property of the object msg).

However here is a way you could do it.

public partial class Form1 : Form
        {
                public Form1(string text)
                {
                        InitializeComponent();
                        this.Text = text; // this will set the form title
                }
        }

You could then add any component (Label, buttons, textbox) and set their value from additem.cs (making the components public).

To show your new form or Dialog

private void button1_Click(object sender, EventArgs e)
        {

            Form1 f = new Form1("dfs");
            f.ShowDialog();
    
        }

Edited 6 Years Ago by PierlucSS: n/a

I'm sorry I didn't explain properly.

here msg is a TextBox ( that is msg = new System.Windows.Forms.TextBox(); )

msg.Text = "fd";

I wanted to change this textbox text to "fd" by clicking on button that is on another form.

hello,
I tried to change the value of a textbox from another window form.

the value is passed to another form properly, but I couldn't change the value in the textbox.

here is the code
Form1.cs

public void setTextbox(String sd)
        {

                    MessageBox.Show(sd);
                    msg.Text = "fd";


        }

additem.cs

private void button1_Click(object sender, EventArgs e)
        {

            Form1 f = new Form1("dfs");
            f.setTextbox("df");

        }

Output:

msgbox shows df which means the data has been passed properly, but the next line "msg.Text = "fd";" returns nullpointer exception :
Object reference not set to an instance of an object.

Thanks

.

I'm sorry I didn't explain properly.

here msg is a TextBox ( that is msg = new System.Windows.Forms.TextBox(); )

msg.Text = "fd";

I wanted to change this textbox text to "fd" by clicking on button that is on another form.

Do your contructor contains InitializeComponent(); ?

Edited 3 Years Ago by mike_2000_17: Fixed formatting

yes it does

public Form1()
        {
            InitializeComponent();
        }
        public Form1(string st)
        {
            this.st = st;
        }

you can make the variables static. ex public static var1 on the other form just use form2.var1

it gives too many errors if I change the variable to static. I think making it static may not be a good idea.

yes it does

public Form1()
        {
            InitializeComponent();
        }
        public Form1(string st)
        {
            this.st = st;
        }

No..it doesnt.
The code you posted here is an example of method overloading; providing different implementations of a method with the same name but different parameters.

If you call Form1 f = new Form1(); without passing it a string, it will run the default constructor. If you call Form1 f = new Form1("something"); with a parameter then it will run the method that matches the parameters passed in.

The constructor that accetps a string does not include the InitializeComponent() method call so your controls are not instantiated.

Change your code to this:

public Form1()
        {
            InitializeComponent();
        }
        public Form1(string st)
        {
            InitializeComponent();
            this.st = st;
        }

only one of the constructors will run. Which one runs depends on the parameters you pass. You should ensure that all of the constructors include a call to InitializeComponent() so that your form is created correctly.

This article has been dead for over six months. Start a new discussion instead.