Click here to Skip to main content
15,903,203 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.

Is there a possibility that I can move a textbox then the label under textbox will also follow.? I mean when I drag the textbox in another location, the label follow as if I textbox and label is considered one control when moving.

I have an instances of textboxes and labels.

Please Help, Thanks. :)
Posted
Updated 19-Jan-11 18:54pm
v2

Yes. One way to do it is to hook into the TextBox's LocationChanged event and set the Label's location to the correct spacing from the TextBox. This way each time the TextBox moves, the Label will too. Example:
C#
private void textBox1_LocationChanged(object sender, EventArgs e)
{
    TextBox temp = (TextBox)sender;
    label1.Location = new Point(temp.Location.X, temp.Location.Y + 26);
}
 
Share this answer
 
Comments
yummy02 18-Jan-11 1:48am    
Thanks for that JOAT-MON. I will try that.
yes you can do it by using Simple code.
C#
Point lp; 
private void textBox1_MouseDown(object sender, MouseEventArgs e)
        {
            lp = new Point(e.X, e.Y); // lp will store current X and Y cordinates of textbox
        }
        private void textBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left || e.Button == MouseButtons.Right)
            {
               // Code for Move textbox 
                this.textBox1.Left += e.Location.X - lp.X;
                this.textBox1.Top += e.Location.Y - lp.Y;
               // Code for move label
                this.label1.Left += e.Location.X - lp.X;
                this.label1.Top += e.Location.Y - lp.Y;
            }
        }


Now when you drag your textbox Label follow it and moves as textbox moves.
 
Share this answer
 
Comments
yummy02 18-Jan-11 1:47am    
Thanks for that RaviRanjankr. I would like to know if I have an instances of textbox in my form. And in all instances of textbox, label is also included. So the problem is when I move the textbox in different location. I would like the label to move too. Is that possible for instances of picturebox?

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