当文本框移动时,另一个跟随 [英] when textbox moves, another follows

查看:69
本文介绍了当文本框移动时,另一个跟随的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.

我是否可以移动文本框,然后文本框下面的标签也会跟随.我的意思是,当我将文本框拖动到另一个位置时,标签跟随,就像我在移动时将文本框和标签视为一个控件一样.

我有一个文本框和标签的实例.

请帮助,谢谢. :)

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

推荐答案

是.一种方法是挂接到TextBox的LocationChanged事件,并将Label的位置设置为与TextBox正确的间距.这样,每次TextBox移动时,Label也会移动.示例:
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:
private void textBox1_LocationChanged(object sender, EventArgs e)
{
    TextBox temp = (TextBox)sender;
    label1.Location = new Point(temp.Location.X, temp.Location.Y + 26);
}


是的,您可以使用简单代码来实现.
yes you can do it by using Simple code.
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.


这篇关于当文本框移动时,另一个跟随的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆