tags:

views:

99

answers:

3

I have this form class

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, System.EventArgs e)
        {
            //This is where I'm working
        }
    }

from inside the button1_Click method how can I access other elements on the form?
I tried to do this

private void button1_Click(object sender, System.EventArgs e)
{
    statusStrip1.Text = "You pressed the button.";
}

But that is not working. In PHP I would use $this->otherObject = 'text';.

+1  A: 

Use the this keyword to get intellisense access to the available properties, methods, and available events on your class. For example if you need to access a textbox name CustomerName you can do so like this:

private void button1_Click(object sender, System.EventArgs e)
{
  this.CustomerName.Text = "Your Name";
  // is the same as
  CustomerName.Text = "Your Name";
}

Update This updated code you added should work. Right click the InitializeComponent method and choose Go To Definition to view the Designer generated code. Maybe you statusStrip is not protected, if so update your questions or add a comment.

bendewey
It would not need to be protected. Controls are usually private members of the form
Ed Swangren
That is working I just have some other error somewhere, because when I place this.primaryStatusBar.Text = ""; in the form1() method it works, but not in the button1_Click method.
Unkwntech
A: 

I'm not familiar with c#, but in general, when you initialize your form, I think you store references to the various parts as instance variables of the class, and then the methods of the class can access them. I'm not sure if this is done for you or not...

DGM
A: 

To display text in a StatusStrip control (which I assume statusStrip in your code is), first add a ToolStripStatusLabel to it. Then set the text of the ToolStripStatusLabel:

toolStripStatusLabel1.Text = "You pressed the button.";

Is this what you're looking for?

Jay Riggs