Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Updating my main form (gui) from another class

Updating my main form (gui) from another class

Scheduled Pinned Locked Moved C#
question
16 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E eggie5

    sure... do you think you can help a guy out? /\ |_ E X E GG

    G Offline
    G Offline
    Guillermo Rivero
    wrote on last edited by
    #7

    Sure. Let me build a sample. Free your mind...

    E 1 Reply Last reply
    0
    • G Guillermo Rivero

      Sure. Let me build a sample. Free your mind...

      E Offline
      E Offline
      eggie5
      wrote on last edited by
      #8

      gracias. /\ |_ E X E GG

      G 1 Reply Last reply
      0
      • E eggie5

        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

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #9

        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! ;)

        E 3 Replies Last reply
        0
        • L Lost User

          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! ;)

          E Offline
          E Offline
          eggie5
          wrote on last edited by
          #10

          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

          B 1 Reply Last reply
          0
          • E eggie5

            gracias. /\ |_ E X E GG

            G Offline
            G Offline
            Guillermo Rivero
            wrote on last edited by
            #11

            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...

            1 Reply Last reply
            0
            • E eggie5

              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

              B Offline
              B Offline
              Bo Hunter
              wrote on last edited by
              #12

              As much trouble as you are having, you may want to invest in a good book on .Net Threading. Thank You Bo Hunter

              E 1 Reply Last reply
              0
              • B Bo Hunter

                As much trouble as you are having, you may want to invest in a good book on .Net Threading. Thank You Bo Hunter

                E Offline
                E Offline
                eggie5
                wrote on last edited by
                #13

                i have already expressed intrest of a C# threading book. But, recieved little reply on recomendations. Do you have one? /\ |_ E X E GG

                1 Reply Last reply
                0
                • A Andy Davey

                  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;
                  //...
                  }

                  //...
                  }

                  E Offline
                  E Offline
                  eggie5
                  wrote on last edited by
                  #14

                  ok, nevermind... I get it now. thanks again for your code.:-D /\ |_ E X E GG

                  1 Reply Last reply
                  0
                  • L Lost User

                    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! ;)

                    E Offline
                    E Offline
                    eggie5
                    wrote on last edited by
                    #15

                    that's not exactly what I need. But, thanks for your help. It turned out I had to make a refrence, as opposed to making a new object of the Form... thanks again.;P /\ |_ E X E GG

                    1 Reply Last reply
                    0
                    • L Lost User

                      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! ;)

                      E Offline
                      E Offline
                      eggie5
                      wrote on last edited by
                      #16

                      actually no, I figured it out. thanks.:laugh: /\ |_ E X E GG

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups