olhodolago
olhodolago

Reputation: 77

Can't retrieve user's TextBox input

I have a program with two Forms:

Form 1 has a TextBox and a Button. Form2 has a DataGridView.

Then I have a class with a constructor that accepts a string as parameter:

public SymbolData(string symbol)
{ /* Do stuff */ }

The DataGridView displays the data from a table defined in SymbolData when user clicks the button.

My problem is that when I click the button the string retrieved from the textbox is the one I inserted in its Text property regardless of what is inserted in the TextBox enter code herewhen the program is running

Here is where I create SymbolData instance:

    public Form2()
    {
        InitializeComponent();
        SymbolData sd = new SymbolData(f1.textButton1.Text);

        dataGridView1.DataSource = sd.Table;
    }

Can anyone help me to pass the user's input from the TextBox in my SymbolData object's constructor when I call it?

Upvotes: 2

Views: 78

Answers (2)

Rahul
Rahul

Reputation: 77934

If you really want to instantiate SymbolData in Form2 then have your form2 constructor accept a string parameter and pass that argument to SymbolData constructor like below

public Form2(string form1data)
{
    InitializeComponent();
    SymbolData sd = new SymbolData(form1data);
    dataGridView1.DataSource = sd.Table;
}

Then in your Form1 button click event get a instance of Form2 and pass textbox data

protected void btn1_Click(object sender, EventArgs e)
{
  Form2 frm2 = new Form2(this.textButton1.Text.Trim());
  frm2.Showdialog();
}

Upvotes: 1

Brad
Brad

Reputation: 37

You can do it multiple ways but most of the ways will require an event to be created. You can create a button on the form, when you double click it a click event is created. You then can pass your text in that way.

private void button1_Click(object sender, EventArgs e)
    {
            SymbolData sd = new SymbolData(f1.textButton1.Text);
    }

You can also create an event on the textbox, such as Keypress, keyup, keydown, you can find those events on the properties of the text box and click the lightning bolt on the top of the panel to see the events.

Upvotes: 1

Related Questions