0

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.

Andrew
  • 7,602
  • 2
  • 34
  • 42
user16307
  • 131
  • 1
  • 9

1 Answers1

2

Use a member variable.

public partial class Form1 : Form
{
  private string parameter = null;

  public Form1()
  {
    InitializeComponent();

    // ...
    parameter = abc.ToString();
  }
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • btw how can I execute button1 insice Form1 so that it will excecute the button automatically at start? when i put button1.PerformClick(); inside Form1, it doesnt work. – user16307 Oct 30 '14 at 18:52
  • 1
    Call `button1_Click(null, null)` from the `protected void Page_Load(object sender, EventArgs e)` method. – Andrew Oct 30 '14 at 20:34