473,387 Members | 3,684 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.Add(myClass.getPanel());

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.Add(myClass.getPanel()); 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!
Jun 27 '08 #1
14 2395
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.Add(myClass.getPanel());

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.Add(myClass.getPanel()); 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!
Jun 27 '08 #2
On Jun 2, 1:29 pm, ssg31415926 <newsjunkm...@gmail.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?
Jun 27 '08 #3
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
Jun 27 '08 #4
On Jun 2, 1:40 pm, Marc Gravell <marc.grav...@gmail.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
Jun 27 '08 #5
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...@gmail.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
Jun 27 '08 #6
On Jun 2, 2:38 pm, ssg31415926 <newsjunkm...@gmail.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!
Jun 27 '08 #7
On Jun 2, 2:38 pm, ssg31415926 <newsjunkm...@gmail.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.Add(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".

Jun 27 '08 #8
Adam Sandler wrote:
>On Jun 2, 2:38 pm, ssg31415926 <newsjunkm...@gmail.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.Add(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-
Jun 27 '08 #9
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...@gmail.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.Add(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".
Jun 27 '08 #10
On Jun 3, 11:24 am, ssg31415926 <newsjunkm...@gmail.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.Remove(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.
Jun 27 '08 #11
On Jun 3, 11:24 am, ssg31415926 <newsjunkm...@gmail.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.Remove(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.
Jun 27 '08 #12
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.Controls.ContainsKey("panel1")))
{
this.Controls.Add(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...@gmail.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.Remove(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.
Jun 27 '08 #13
Adam Sandler wrote:
On Jun 3, 11:24 am, ssg31415926 <newsjunkm...@gmail.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.Remove(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.BringToFront() 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-
Jun 27 '08 #14
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.Controls.Clear();
}

....

private void btn_Click(object sender, EventArgs e)
{
clearPannel();
pnl_name.Controls.Add(new_frm);
}

Hope it helps
Oct 13 '08 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: KvS | last post by:
Hi all, I'm pretty new to (wx)Python so plz. don't shoot me if I've missed something obvious ;). I have a panel inside a frame, on which a Button and a StaticText is placed: self.panel =...
3
by: keith | last post by:
I have a panel on my form and add controls (e.g. TextBox) into it at runtime. What I want is the controls will display on same line if they are visiable in the panel horizontally. If any of the...
2
by: R Duke | last post by:
I have tried everything I can think of to change the visible property of a design time created control from a dynamically created control's command event handler. Here is the scenario. I have...
9
by: Matt Tapia | last post by:
I having a problem that receives the following error: Specified cast is not valid And I need some help. Here is what is happening: I have a form with a drop-down control that contains a list...
7
by: Dino Buljubasic | last post by:
Hi, I am using C# 2.0 (VS2005) to build my user control that contains a number of dynamically loaded ListViews. ListViewItems have their ForeColor properties set to either black or blue to...
1
by: Brian | last post by:
I am running into a problem with the CanFocus property not allowing me to set the focus to a control. The application is a Form which has a Panel and in the panel are a series of user controls. ...
14
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using VS2005 and .net 2.0. I'm creating an application that has 3 forms. I want allow users to move forward and backward with the forms and retain the data users have entered. I thought...
1
by: aeroumr | last post by:
In the following code, I have created a panel with a button and a textctrl object on it. I have also created a menubar that will create a new text file (i.e. textctrl object). My problem is that...
2
by: =?Utf-8?B?TUNN?= | last post by:
I have an asp.net page that contains an update panel. Within the update panel, controls get added dynamically. During partial page post backs the controls within the panel will change. I have a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.