Click here to Skip to main content
15,905,504 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
this is my code

C#
private void panel2_MouseClick(object sender, MouseEventArgs e)
    {

        Label newlabel = new Label();
        panel2.Controls.Add(newlabel);
        newlabel.Text = "0";
        newlabel.Location = new Point (e.Location.X, 7);

    }


for some reason when I click like in the middle a label spawns but when I click right from the new label, a new label doesn't spawn, they only spawn from the left, can anyone plz help?
Posted

That is because of one label hiding another. Add this line newlabel.Size=new Size(10,15); to define a size for the label or else remove the labels already added before adding a new one.
 
Share this answer
 
Comments
[no name] 21-Mar-11 2:26am    
thanks it worked but, the labels hide agent each other, is there a way were i can just see the text and not the backcolor stuff ?
Sergey Alexandrovich Kryukov 21-Mar-11 2:35am    
I voted 5, but I understand this is not a practical advice but yet another experimental trick to show the problem. (Setting explicit (hard-coded!) size is a way of clipping the label), so you might want to add a node to avoid accusation in impractical advice :-)
Also, the problem is hiding a part of the panel, please see my Answer.
--SA
Albin Abel 21-Mar-11 2:40am    
I think you can't remove the backcolor as it has a text in it. You may set the label to right size using the MeasureString function. For that you need to handle the label's paint event.
First, when you click on some other Control, such as newly created Label, it will consume your event before it is dispatched to your Panel. Consider adding new event to other children of the panel as you created them.

Another reason is this: new label can be clipped by the right side of your panel if you click on the right side. Always use the debugger when you're puzzled with such simple things: it will show your you if the event is fired or not and all the parameters of the object you create.

You code does not seem to have any application, but I understand: this is just experimenting.

—SA
 
Share this answer
 
Comments
Albin Abel 21-Mar-11 2:48am    
So how about routing child events to the panel. An old challenge of yours, the burnt controls :). My 5
Sergey Alexandrovich Kryukov 21-Mar-11 2:58am    
Thank you, Albin.
Sorry, don't quite understand the question... your have a handler in a separate method. As you create a new label, add the same handler to it. It's body does not depend on label, only on parent Panel, so newLabel.Click += ...same thing.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900