Updating my main form (gui) from another class
-
Sure. Let me build a sample. Free your mind...
-
Sure. Let me build a sample. Free your mind...
-
On my main form I have a progess bar "progressBar1" (Public). I have another class file "pb.cs", in which is the code...
using System;
namespace WindowsApplication8
{
/// /// Summary description for pb.
///
public class pb
{
Form1 guid=new Form1();
public pb()
{
guid.progressBar1.Value=100;
}
}
}Why can I not change the value of the progess bar to 100? /\ |_ E X E GG
I made a similar test and I managed to change the progress bar's value to 100. If you could specify the error message you received it would be very helpful. You may want to check these things first: 1. Make sure Maximum value for the progress bat is at least 100. 2. Check that the progress bar is declared as PUBLIC in your Form1, otherwise create a property to access your progress bar. Hope it helps! ;)
-
I made a similar test and I managed to change the progress bar's value to 100. If you could specify the error message you received it would be very helpful. You may want to check these things first: 1. Make sure Maximum value for the progress bat is at least 100. 2. Check that the progress bar is declared as PUBLIC in your Form1, otherwise create a property to access your progress bar. Hope it helps! ;)
Hello.... I do not recieve any exceptions, but here is what I understand is going on.... when I do this code in in a sperate class.... Form1 guid=new Form1(); apparently it just makes a duplicate of the from Form1. I can completely control this new from from the seperate class, with no problems... if I go guid.Show(); it then displays the duplicate with the controls muniplited, as i configured. However, I don't want to manipulate the controls (i.e. progress bar) on the dup;icate Form1, I want to do it on the original. From what I understand so far, I think I have to make a REFERENCE... and yes the progressBar is public. alos, I do not have access to the Form 1 from the seperate class "logging.cs" because my code eventually gets to that class after going though other classes before it.... does this make sense? is it possible? thankyou for your time. /\ |_ E X E GG
-
Here it goes...
namespace WindowsApplication1 { public class Form1 : System.Windows.Forms.Form { private bool _done = false; private ProgressBar _pb = new ProgressBar(); private Form2 _form = new Form2(); private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private int _value=0; public Form1() { this.Size = new System.Drawing.Size(300,300); this.button1= new Button(); this.button1.Location = new System.Drawing.Point(8, 240); this.button1.Name = "button1"; this.button1.Click += new System.EventHandler(this.button1_Click); this.button2= new Button(); this.button2.Location = new System.Drawing.Point(100, 240); this.button2.Name = "button2"; this.button2.Click += new System.EventHandler(this.button2_Click); this.Controls.Add(this.button1); this.Controls.Add(this.button2); this._pb.Location = new Point(0,0); this._pb.Width = 100; this._pb.Minimum=0; this._pb.Maximum=100; } private void button1_Click(object sender, System.EventArgs e) { _form.Show(); if (!_done) { _form.Controls.Add(_pb); _done=true; } } private void button2_Click(object sender, System.EventArgs e) { if(_value<100) _value +=10; else _value=0; _pb.Value=_value; } [STAThread] static void Main() { Application.Run(new Form1()); } } }
I think that's what you want, isn't it ? :eek: Free your mind... -
Hello.... I do not recieve any exceptions, but here is what I understand is going on.... when I do this code in in a sperate class.... Form1 guid=new Form1(); apparently it just makes a duplicate of the from Form1. I can completely control this new from from the seperate class, with no problems... if I go guid.Show(); it then displays the duplicate with the controls muniplited, as i configured. However, I don't want to manipulate the controls (i.e. progress bar) on the dup;icate Form1, I want to do it on the original. From what I understand so far, I think I have to make a REFERENCE... and yes the progressBar is public. alos, I do not have access to the Form 1 from the seperate class "logging.cs" because my code eventually gets to that class after going though other classes before it.... does this make sense? is it possible? thankyou for your time. /\ |_ E X E GG
-
As much trouble as you are having, you may want to invest in a good book on .Net Threading. Thank You Bo Hunter
-
eggie5 wrote: Why can I not change the value of the progess bar to 100? Do you mean that the progress bar doens't visually change or do you mean that the framework throws an exception. Without seeing more of your code I would suspect its the former one. From what I can see you are expecting this pb class to update the progress bar in the applications main form. However, the pb class has no reference to the main form. When the pb class is created, a new object of type Form1 is created. This new is not the same object as the one displayed on the screen when you run your program. To get your guid member to reference the displayed form - pass a Form1 object reference to the pb constructor. An example would be
public class Form1
{
pb m_pb;public Form1()
{
m_pb = new pb(this);
//...
}//...
}public class pb
{
Form1 guid;public pb(Form1 p_ref)
{
guid = p_ref;
guid.progressBar1.Value = 100;
//...
}//...
} -
I made a similar test and I managed to change the progress bar's value to 100. If you could specify the error message you received it would be very helpful. You may want to check these things first: 1. Make sure Maximum value for the progress bat is at least 100. 2. Check that the progress bar is declared as PUBLIC in your Form1, otherwise create a property to access your progress bar. Hope it helps! ;)
-
I made a similar test and I managed to change the progress bar's value to 100. If you could specify the error message you received it would be very helpful. You may want to check these things first: 1. Make sure Maximum value for the progress bat is at least 100. 2. Check that the progress bar is declared as PUBLIC in your Form1, otherwise create a property to access your progress bar. Hope it helps! ;)