Results 1 to 6 of 6

Thread: Help: HowTo Acces a control from within a class

  1. #1

    Thread Starter
    Registered User Olly's Avatar
    Join Date
    Apr 2001
    Location
    Switzerland
    Posts
    252

    Help: HowTo Acces a control from within a class

    Hello again!
    I fear this i a pretty easy one
    I got a progressbar on my form which needs to be updated from time to time. My problem is that the progress is being done in a class in another namespace. How could I change the Value of the progressbar from that class? I just don't see a way to access it.

  2. #2
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    You could either pass a reference of the progress bar into your class or you could create a delegate which will callback to a routine on the form that updates the progress bar.

  3. #3

    Thread Starter
    Registered User Olly's Avatar
    Join Date
    Apr 2001
    Location
    Switzerland
    Posts
    252
    I think I could need an example, I've never used delegates before, neither did I create any references, no matter what I try it does not work. I end with a static void from which I can't access any controls on the form.

  4. #4
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Does this help at all?

    Code:
    private void button1_Click(object sender, System.EventArgs e)
    {
    	DoSomething mySomething = new DoSomething("Hello World",new DoSomething.UpdateControl(DisplayText));
     
    }
    
    private void DisplayText(string myText)
    {
    	this.textBox1.Text = myText;
    }
    
    }
    
    public class DoSomething
    {
    	string message;
    	public delegate void UpdateControl(string message);
    
    	public DoSomething(string message,UpdateControl myUpdate)
    	{
    		this.message = message;
    		myUpdate(message);
    	}
    }
    You could also raise events.

  5. #5

    Thread Starter
    Registered User Olly's Avatar
    Join Date
    Apr 2001
    Location
    Switzerland
    Posts
    252
    It helps , very much indeed, thank you!

  6. #6

    Thread Starter
    Registered User Olly's Avatar
    Join Date
    Apr 2001
    Location
    Switzerland
    Posts
    252
    Ok, Iv'e done that! The value is passed to the progressbar!
    BUT WHY IS THE PROGRESSBAR NOT REFRESHING?
    I've tried to write the value into a textbox for checking, but the text alsow won't change . But when I put a messagebox after that like MessageBox.Show (textBox1.Text, "My Application", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

    It will show the the correct value, though the text in the textbox is not the textBox1.Text which the MessageBox says!
    Is there something needed to refresh the stuff?
    Last edited by Olly; Nov 1st, 2002 at 09:28 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width