Accesing controls on the form from custom method

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2005
Posts: 26
Reputation: Stivi is an unknown quantity at this point 
Solved Threads: 0
Stivi's Avatar
Stivi Stivi is offline Offline
Light Poster

Accesing controls on the form from custom method

 
0
  #1
Aug 27th, 2006
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
Last edited by Stivi; Aug 27th, 2006 at 2:05 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 233
Reputation: Lord Soth is an unknown quantity at this point 
Solved Threads: 4
Lord Soth's Avatar
Lord Soth Lord Soth is offline Offline
Posting Whiz in Training

Re: Accesing controls on the form from custom method

 
0
  #2
Aug 28th, 2006
Hi,

  1. void change(string s)
  2. {
  3. label1.Text = s;
  4. }
  5.  
  6. private void button1_Click(object sender, System.EventArgs e)
  7. {
  8. change("Hakan");
  9. }

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

Loren Soth
Best regards,
Loren Soth

Crimson K. Software _________________________________________________________________ Crimson K. Blog
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 206
Reputation: plazmo is an unknown quantity at this point 
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

Re: Accesing controls on the form from custom method

 
0
  #3
Aug 28th, 2006
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 26
Reputation: Stivi is an unknown quantity at this point 
Solved Threads: 0
Stivi's Avatar
Stivi Stivi is offline Offline
Light Poster

Re: Accesing controls on the form from custom method

 
0
  #4
Aug 28th, 2006
Thanks Loren,

I've declared my method as:

public static 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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2468 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC