如何在C#中移动文本框控件 [英] How to Move the Textbox Control in C#

查看:183
本文介绍了如何在C#中移动文本框控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个如何在C#中移动文本框控件的示例。

I would like an example of how to move the textbox control in C#.

推荐答案

嘿,只需使用以下代码。

Take在Form中的textBox控件,为Form创建一个鼠标移动事件,例如。 (Form1_MouseMove)

注意你可以向任何方向移动textBox,只需将鼠标放在任何一个边缘,如左,右,上或下。如果你将鼠标放在textBox的左端,只需向右移动鼠标..





Hey just use following code.
Take a textBox control at your Form, make a mouse move event for Form eg. (Form1_MouseMove)
Note you can move textBox in any direction just keep mouse at any one edge like left, right, top or down. If you keep mouse at left end of textBox just move mouse at right direction..


private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
if (e.X == textBox1.Location.X - 1 && (e.Y >= textBox1.Location.Y - 1 && e.Y <= textBox1.Location.Y + textBox1.Height + 1))
            {
                textBox1.SetBounds(textBox1.Location.X + 5, textBox1.Location.Y, 75, 23);
            }
            if (e.X == textBox1.Location.X + textBox1.Width + 1 && (e.Y >= textBox1.Location.Y - 1 && e.Y <= textBox1.Location.Y + textBox1.Height + 1))
            {
                textBox1.SetBounds(textBox1.Location.X - 5, textBox1.Location.Y, 75, 23);
            }
            if (e.Y == textBox1.Location.Y - 1 && (e.X >= textBox1.Location.X - 1 && e.X <= textBox1.Location.X + textBox1.Width + 1))
            {
                textBox1.SetBounds(textBox1.Location.X, textBox1.Location.Y + 5, 75, 23);
            }
            if (e.Y == textBox1.Location.Y + textBox1.Height + 1 && (e.X >= textBox1.Location.X - 1 && e.X <= textBox1.Location.X + textBox1.Width + 1))
            {
                textBox1.SetBounds(textBox1.Location.X, textBox1.Location.Y - 5, 75, 23);
            }
            if (textBox1.Location.X >= this.Width - textBox1.Width - 1)
            {
                textBox1.SetBounds(1, textBox1.Location.Y, 75, 23);
            }
            if (textBox1.Location.X <= 1)
            {
                textBox1.SetBounds(textBox1.Location.X + textBox1.Width + 1, textBox1.Location.Y, 75, 23);
            }
        }


假设您使用的是Windows Forms,它会是这样的:

Assuming you are using Windows Forms, it would go something like this:
textBox1.Left = 100;
textBox1.Top = 100;



现在,如果你想为文本框设置动画,那就是一个不同的故事。


Now, if you want to animate a textbox, that's a different story.


嘿,

根据我的理解,如果你想跟踪文本框控制从一个地方到另一个地方那么你使用的应该使用 C#中的跟踪对象 [ ^ ]



这个文档,示例可以很好地重复拖放任何对象。
Hey,
according my understanding, if you want track Text box control from one place to another then you used should use Tracking object in C#[^]

this document ,example gives good repesention of drag and drop of any object.


这篇关于如何在C#中移动文本框控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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