problem with updating a panel created at runtime.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adam Sandler

    problem with updating a panel created at runtime.

    I have a class with a method which returns a panel. The panel was not
    created with the visual editor, I coded it by hand -- because some of
    the content on the panel can be dynamic and thus I chose to do it that
    way.

    At any rate, I have an event handler for a button which instantiates
    an object of that class I mentioned and calls the method to return the
    panel. I put the panel on form1 like so:

    // it's really not called 'Class', I changed the name to simplify the
    post
    Class myClass = new Class();
    this.Controls.A dd(myClass.getP anel());

    Everything up to this point works.

    If that event handler is ever called again (i.e., n times; more than
    once), due to information which changed and needs to be displayed on
    the custom panel, the result at runtime is the panel doesn't appear to
    update.

    I've put some breakpoints in and debugged. The information which
    needs to be updated on the panel is indeed updating as viewed with the
    debugger breaks. But when this.Controls.A dd(myClass.getP anel()); is
    called, the old information remains on the form.

    Where I'm really lost is the new instance of the panel reflects the
    updated information but when it's added to the form, only the first
    instance of the panel, with the initial information, is ever
    displayed. How can I get the newest panel on the form? I was
    thinking about making a call to remove but I'm not sure that would
    work when the panel is being created at runtime. Suggestions are
    greatly appreciated.

    Thanks!
  • ssg31415926

    #2
    Re: problem with updating a panel created at runtime.

    If you're not removing the panel, why are you trying to re-add it?
    Can you not just keep a reference to the panel and modify it directly?

    On 2 Jun, 14:25, Adam Sandler <cor...@excite. comwrote:
    I have a class with a method which returns a panel. The panel was not
    created with the visual editor, I coded it by hand -- because some of
    the content on the panel can be dynamic and thus I chose to do it that
    way.
    >
    At any rate, I have an event handler for a button which instantiates
    an object of that class I mentioned and calls the method to return the
    panel. I put the panel on form1 like so:
    >
    // it's really not called 'Class', I changed the name to simplify the
    post
    Class myClass = new Class();
    this.Controls.A dd(myClass.getP anel());
    >
    Everything up to this point works.
    >
    If that event handler is ever called again (i.e., n times; more than
    once), due to information which changed and needs to be displayed on
    the custom panel, the result at runtime is the panel doesn't appear to
    update.
    >
    I've put some breakpoints in and debugged. The information which
    needs to be updated on the panel is indeed updating as viewed with the
    debugger breaks. But when this.Controls.A dd(myClass.getP anel()); is
    called, the old information remains on the form.
    >
    Where I'm really lost is the new instance of the panel reflects the
    updated information but when it's added to the form, only the first
    instance of the panel, with the initial information, is ever
    displayed. How can I get the newest panel on the form? I was
    thinking about making a call to remove but I'm not sure that would
    work when the panel is being created at runtime. Suggestions are
    greatly appreciated.
    >
    Thanks!

    Comment

    • Adam Sandler

      #3
      Re: problem with updating a panel created at runtime.

      On Jun 2, 1:29 pm, ssg31415926 <newsjunkm...@g mail.comwrote:
      If you're not removing the panel, why are you trying to re-add it?
      Because some of the objects on the panel (labels, richtextboxes, etc)
      change and I want to reflect the current data. Have I incorrectly
      assumed that trying to re-add it to the form would give me the
      "refresh" behavior I need?

      Comment

      • Marc Gravell

        #4
        Re: problem with updating a panel created at runtime.

        Are you updating the panel instance that you previously added, or are
        you creating a new panel each time?

        And what is on the panel? Regular controls? or is it custom painted?

        Marc

        Comment

        • Adam Sandler

          #5
          Re: problem with updating a panel created at runtime.

          On Jun 2, 1:40 pm, Marc Gravell <marc.grav...@g mail.comwrote:
          Are you updating the panel instance that you previously added, or are
          you creating a new panel each time?
          >
          And what is on the panel? Regular controls? or is it custom painted?
          >
          Marc
          Mostly just items to display data... labels and rich text boxes

          Comment

          • ssg31415926

            #6
            Re: problem with updating a panel created at runtime.

            You don't need to re-add the panel to give you the refresh behaviour.
            Try removing the later .Add and see what happens. If it still isn't
            working, post the rest of your code.

            On 2 Jun, 14:56, Adam Sandler <cor...@excite. comwrote:
            On Jun 2, 1:40 pm, Marc Gravell <marc.grav...@g mail.comwrote:
            >
            Are you updating the panel instance that you previously added, or are
            you creating a new panel each time?
            >
            And what is on the panel? Regular controls? or is it custom painted?
            >
            Marc
            >
            Mostly just items to display data... labels and rich text boxes

            Comment

            • Adam Sandler

              #7
              Re: problem with updating a panel created at runtime.

              On Jun 2, 2:38 pm, ssg31415926 <newsjunkm...@g mail.comwrote:
              You don't need to re-add the panel to give you the refresh behaviour.
              Try removing the later .Add and see what happens. If it still isn't
              working, post the rest of your code.
              >
              There's only one .Add. It's in the method called by the event
              handler. Of course, that can get called 1 - n times. The project is
              on a computer I cannot get to at this time; I'll try to post more code
              tonite. Thanks!

              Comment

              • Adam Sandler

                #8
                Re: problem with updating a panel created at runtime.

                On Jun 2, 2:38 pm, ssg31415926 <newsjunkm...@g mail.comwrote:>
                You don't need to re-add the panel to give you the refresh behaviour.
                Try removing the later .Add and see what happens. If it still isn't
                working, post the rest of your code.
                Okay, here's the code...

                -- The first thing I'll post is the class for the panel I created:

                class MyPanel
                {
                public Control getPanel()
                {
                Panel panel1 = new Panel();
                Label label1 = new Label();

                //[snip... removed some code to simplify for posting]

                label1.Text = var;

                panel1.Controls .Add(label1);

                return panel1;
                }
                }

                -- Next, here is the event handler:

                private void buttonNew_Click (object sender, EventArgs e)
                {
                MyPanel mp = new MyPanel();
                this.Controls.A dd(mp.getPanel( ));
                }

                -- The code works fine the first time. Let's say that the var which
                is assigned to the label1.Text equals "1000". After the user clicks
                the 'new' button, the panel with the label of "1000" is indeed
                displayed on the form. However, if the user clicks the new button
                again lets say label1.Text now equals "2000", the value on the form
                still reads "1000".

                Comment

                • Rick Lones

                  #9
                  Re: problem with updating a panel created at runtime.

                  Adam Sandler wrote:
                  >On Jun 2, 2:38 pm, ssg31415926 <newsjunkm...@g mail.comwrote:>
                  >>You don't need to re-add the panel to give you the refresh behaviour.
                  >>Try removing the later .Add and see what happens. If it still isn't
                  >>working, post the rest of your code.
                  >
                  Okay, here's the code...
                  >
                  -- The first thing I'll post is the class for the panel I created:
                  >
                  class MyPanel
                  {
                  public Control getPanel()
                  {
                  Panel panel1 = new Panel();
                  Label label1 = new Label();
                  >
                  //[snip... removed some code to simplify for posting]
                  >
                  label1.Text = var;
                  >
                  panel1.Controls .Add(label1);
                  >
                  return panel1;
                  }
                  }
                  >
                  -- Next, here is the event handler:
                  >
                  private void buttonNew_Click (object sender, EventArgs e)
                  {
                  MyPanel mp = new MyPanel();
                  this.Controls.A dd(mp.getPanel( ));
                  }
                  >
                  -- The code works fine the first time. Let's say that the var which
                  is assigned to the label1.Text equals "1000". After the user clicks
                  the 'new' button, the panel with the label of "1000" is indeed
                  displayed on the form. However, if the user clicks the new button
                  again lets say label1.Text now equals "2000", the value on the form
                  still reads "1000".
                  >
                  Well, there is still a bunch of relevant stuff you are not showing here, such
                  as where your value of "var" comes from. Plus you still have not explained why,
                  for instance, a simple update of the current panel's label (and whatever else is
                  on the panel) would not suffice for you, e.g. "panel1.label1. text = var",
                  assuming you had a reference to your panel named "panel1" somewhere in your
                  form's private field data.

                  That said though, the obvious piece that's missing from the code you did post is
                  removing the old panel from the form's Controls collection. I think it's
                  entirely possible that your new instances of the panel are there, but in the
                  same location and hidden underneath the older instances.

                  HTH,
                  -rick-

                  Comment

                  • ssg31415926

                    #10
                    Re: problem with updating a panel created at runtime.

                    Was in a seminar all day so couldn't play with this much. But this is
                    not the right way to go about it.

                    1. How dynamic will things be?
                    2. What ultimately are you trying to achieve?

                    On 3 Jun, 03:12, Adam Sandler <cor...@excite. comwrote:
                    On Jun 2, 2:38 pm, ssg31415926 <newsjunkm...@g mail.comwrote:>
                    You don't need to re-add the panel to give you the refresh behaviour.
                    Try removing the later .Add and see what happens. If it still isn't
                    working, post the rest of your code.
                    >
                    Okay, here's the code...
                    >
                    -- The first thing I'll post is the class for the panel I created:
                    >
                    class MyPanel
                    {
                    public Control getPanel()
                    {
                    Panel panel1 = new Panel();
                    Label label1 = new Label();
                    >
                    //[snip... removed some code to simplify for posting]
                    >
                    label1.Text = var;
                    >
                    panel1.Controls .Add(label1);
                    >
                    return panel1;
                    }
                    }
                    >
                    -- Next, here is the event handler:
                    >
                    private void buttonNew_Click (object sender, EventArgs e)
                    {
                    MyPanel mp = new MyPanel();
                    this.Controls.A dd(mp.getPanel( ));
                    }
                    >
                    -- The code works fine the first time. Let's say that the var which
                    is assigned to the label1.Text equals "1000". After the user clicks
                    the 'new' button, the panel with the label of "1000" is indeed
                    displayed on the form. However, if the user clicks the new button
                    again lets say label1.Text now equals "2000", the value on the form
                    still reads "1000".

                    Comment

                    • Adam Sandler

                      #11
                      Re: problem with updating a panel created at runtime.

                      On Jun 3, 11:24 am, ssg31415926 <newsjunkm...@g mail.comwrote:

                      Thanks for the reply... I got caught up in meetings today too.
                      1. How dynamic will things be?
                      Dynamic enough that I'm trying to be flexible and not hardcode. I'm
                      not trying to be difficult but much to Rick Lones' chagrin, I've
                      posted all the relevant code. I don't see the value in writing a
                      dissertation on the USENET trying to explain the business rules (which
                      ARE rocket science) behind this element of design.
                      2. What ultimately are you trying to achieve?
                      Simply trying to add the panel to the form without any code smells.
                      In the last 24 hours, I've added this code:

                      foreach (Control c in this.Controls)
                      {
                      if (c.Name.Equals( mp.Name))
                      {
                      this.Controls.R emove(c);
                      }
                      }

                      And it works. However, looking at this bit of code gives me an uneasy
                      feeling this is a bad implementation. In the meantime it works.
                      Since we're long past the days of computers with turbo buttons on
                      them, the user will never see any paint performance problems with
                      removing the panel and adding it again.

                      Comment

                      • Adam Sandler

                        #12
                        Re: problem with updating a panel created at runtime.

                        On Jun 3, 11:24 am, ssg31415926 <newsjunkm...@g mail.comwrote:

                        Thanks for the reply... I got caught up in meetings today too.
                        1. How dynamic will things be?
                        Dynamic enough that I'm trying to be flexible and not hardcode. I'm
                        not trying to be difficult but much to Rick Lones' chagrin, I've
                        posted all the relevant code. I don't see the value in writing a
                        dissertation on the USENET trying to explain the business rules (which
                        ARE rocket science) behind this element of design.
                        2. What ultimately are you trying to achieve?
                        Simply trying to add the panel to the form without any code smells.
                        In the last 24 hours, I've added this code:

                        foreach (Control c in this.Controls)
                        {
                        if (c.Name.Equals( mp.Name))
                        {
                        this.Controls.R emove(c);
                        }
                        }

                        And it works. However, looking at this bit of code gives me an uneasy
                        feeling this is a bad implementation. In the meantime it works.
                        Since we're long past the days of computers with turbo buttons on
                        them, the user will never see any paint performance problems with
                        removing the panel and adding it again.

                        Comment

                        • ssg31415926

                          #13
                          Re: problem with updating a panel created at runtime.

                          The reason I wanted to see more code was to work out where/how you're
                          updating the label on the panel, so I could play round with
                          alternatives before offering any advice.

                          I can understand the need for flexibility but given the time-savings
                          of using Visual Studio to design GUI stuff versus hand-coding it I
                          think you've really got to need it. If I were writing this and I was
                          expecting, say, ten different panel configurations, I would design
                          each in Visual Studio and have them all added to the form and then use
                          some combination of .Enabled, .Visible and possibly .Location in my
                          button handlers to hide the panels that weren't active. Clearly, you
                          could still use this technique if you hand-coded it.

                          Because I don't like to dictate to anyone how to write code (I'm self-
                          taught and probably get lots wrong), I can offer you this 'fix' to
                          your code but it's still nasty:

                          Populate the .Name property with any suitable string. Visual Studio
                          would do this for you automatically and give it the name of the
                          variable, as I have below:
                          public Control getPanel()
                          {
                          Panel panel1 = new Panel();
                          panel1.Name = "panel1";
                          :
                          :

                          Then wrap this if statement around you .Add:
                          if (!(this.Control s.ContainsKey(" panel1")))
                          {
                          this.Controls.A dd(p);
                          }

                          Please note that I would not do things this way.

                          One final question, do you have a Java background?

                          On 4 Jun, 06:27, Adam Sandler <cor...@excite. comwrote:
                          On Jun 3, 11:24 am, ssg31415926 <newsjunkm...@g mail.comwrote:
                          >
                          Thanks for the reply... I got caught up in meetings today too.
                          >
                          1. How dynamic will things be?
                          >
                          Dynamic enough that I'm trying to be flexible and not hardcode. I'm
                          not trying to be difficult but much to Rick Lones' chagrin, I've
                          posted all the relevant code. I don't see the value in writing a
                          dissertation on the USENET trying to explain the business rules (which
                          ARE rocket science) behind this element of design.
                          >
                          2. What ultimately are you trying to achieve?
                          >
                          Simply trying to add the panel to the form without any code smells.
                          In the last 24 hours, I've added this code:
                          >
                          foreach (Control c in this.Controls)
                          {
                          if (c.Name.Equals( mp.Name))
                          {
                          this.Controls.R emove(c);
                          }
                          }
                          >
                          And it works. However, looking at this bit of code gives me an uneasy
                          feeling this is a bad implementation. In the meantime it works.
                          Since we're long past the days of computers with turbo buttons on
                          them, the user will never see any paint performance problems with
                          removing the panel and adding it again.

                          Comment

                          • Rick Lones

                            #14
                            Re: problem with updating a panel created at runtime.

                            Adam Sandler wrote:
                            On Jun 3, 11:24 am, ssg31415926 <newsjunkm...@g mail.comwrote:
                            >
                            Thanks for the reply... I got caught up in meetings today too.
                            >
                            >1. How dynamic will things be?
                            >
                            Dynamic enough that I'm trying to be flexible and not hardcode. I'm
                            not trying to be difficult but much to Rick Lones' chagrin, I've
                            posted all the relevant code. I don't see the value in writing a
                            dissertation on the USENET trying to explain the business rules (which
                            ARE rocket science) behind this element of design.
                            Fair enough, I guess. And if I'm chagrined about anything it's that my sample
                            line of code was so incorrect - the label is not a field of the panel, sorry.
                            >2. What ultimately are you trying to achieve?
                            >
                            Simply trying to add the panel to the form without any code smells.
                            In the last 24 hours, I've added this code:
                            >
                            foreach (Control c in this.Controls)
                            {
                            if (c.Name.Equals( mp.Name))
                            {
                            this.Controls.R emove(c);
                            }
                            }
                            >
                            And it works. However, looking at this bit of code gives me an uneasy
                            feeling this is a bad implementation. In the meantime it works.
                            Since we're long past the days of computers with turbo buttons on
                            them, the user will never see any paint performance problems with
                            removing the panel and adding it again.
                            It is bad implementation, IMO. When you create the panel save a reference to it
                            somewhere in your form's field data so that you can use that reference later
                            in the Remove() method without having to loop to obtain it.

                            So in your button event:

                            Controls.Remove (myPanel);
                            myPanel = new Panel;
                            < initialiazation of the new myPanel instance here . . .>

                            If you absolutely must keep re-creating instances of this panel, then not
                            removing old instances from the Controls collection is NOT an option. If the
                            Controls collection continues to hold references to the old instances they will
                            never be candidates for garbage collection. Now just painting on top of them
                            with, e.g., myPanel.BringTo Front() may appear to work but in the long run your
                            Controls collection gets crapped up with old instances of the panel and you have
                            a classic kind of memory leak.

                            HTH,
                            -rick-

                            Comment

                            • Michael Ranga

                              #15
                              re: problem with updating a panel created at runtime.

                              I had same issue, and just cleared the panel before adding to the panel. Seems to be ok but i wouldn't know, I tend to just get things working, not knowing good ways or not


                              private void clearPanel()
                              {
                              pnl_name.Contro ls.Clear();
                              }

                              ....

                              private void btn_Click(objec t sender, EventArgs e)
                              {
                              clearPannel();
                              pnl_name.Contro ls.Add(new_frm) ;
                              }

                              Hope it helps

                              Comment

                              Working...