Accessing a Control
I am doing some of my first GUI C# programming. I have a program.cs and a form.cs code files. I have a refreshData() function that I want my button to call. It works if I use
[code]
RSSGuiVersion.Program.refreshData();
[/code]
However, at the end of the function I want my function access a label called "currentSong" and change its text. How do I do this? I've tried
[code]
RSSGuiVersion.Armitunes.currentSong.Text = "a";
[/code]
And I get
An object reference is required for the nonstatic field, method, or property 'member'
For the life of me I do not know where to go next.
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.

Jan MontanoRank #1,295 · 1.3K reputationPosted Jul 15, 2007, 9:24 PM EDT
You just need to modify RSSGuiVersion.Armitunes.currentSong.Text = "a"; into currentSong.Text = "a";
AlanRank #669 · 3.6K reputationPosted Jul 16, 2007, 1:17 AM EDT
Also, if the currentSong label is private or protected, he'll need to change it to public or internal so that it can be accessed from outside of the form.
If I'm right about this, Alex, and you're using .NET 2.0 or 3.0 (VS 2005) then you could access the label like this:
Application.OpenForms[0].currentSong.Text = "whatever";
If you're using .NET 1.0 or 1.1, then this won't work. Instead, you could change the refreshData method so that it takes a parameter of type Armitunes, like this:
public static void refreshData(Armitunes armitunes)
{
// code
armitunes.currentSong.Text = "whatever";
}
When calling this method from the form, you'd need to use:
RSSGuiVersion.Program.refreshData(this);