954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Accesing controls on the form from custom method

Hi,

Here's what I've got:
Visual Studio 2005
C# Application with form and a label on it
(This is a simplified project ;) )

Here's what I'd like to do:
I want to change text on the label from the method I'll create. Looks simple... If I'll create a button which will change a label's text, it works. But if I want to change it from method I've created on my own, it's impossible... I just don't see any label in my method... Is there some way to access it?

Thanks, Steve

Stivi
Light Poster
26 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

Hi,

void change(string s)
{
  label1.Text = s;
}

private void button1_Click(object sender, System.EventArgs e)
{
  change("Hakan");
}


As you see you have to call your own method from somewhere.Loren Soth

Lord Soth
Posting Whiz in Training
233 posts since Mar 2006
Reputation Points: 28
Solved Threads: 4
 

If you cant see your label, you may have created the new method in a different class as your form. Did you make a new cs file?

plazmo
Posting Whiz in Training
207 posts since Aug 2005
Reputation Points: 23
Solved Threads: 16
 

Thanks Loren,

I've declared my method as:

<strong>public static </strong>void change(string s)
{
  label1.Text = s;
}


and because of that I was unable to use label1 :rolleyes: . I've tried to declare it as void only and it works...

So thanx a lot,
Steve

Stivi
Light Poster
26 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You