Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
I would like an example of how to move the textbox control in C#.
Posted
Updated 11-Mar-11 7:37am
v2
Comments
Albin Abel 11-Mar-11 14:13pm    
Is textbox doesn't offer a location property of type point?
Sergey Alexandrovich Kryukov 11-Mar-11 20:44pm    
How can I up-vote this? :-) Something like that would be my answer of choice, suggested for OP for formally accept.
--SA

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


C#
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);
            }
        }
 
Share this answer
 
Comments
[no name] 12-Mar-11 0:15am    
My earlier given solution were not appearing completely, that why i deleted.
amitkarnik2211 12-Mar-11 3:39am    
that is good help for me my 5
Assuming you are using Windows Forms, it would go something like this:
C#
textBox1.Left = 100;
textBox1.Top = 100;

Now, if you want to animate a textbox, that's a different story.
 
Share this answer
 
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.
 
Share this answer
 
Have you ever looked at the Formdesigner?
Try this:

C#
private void button1_Click(object sender, EventArgs e)
{
    this.textBox1.Location = new System.Drawing.Point(2, 3);
}


EDIT:
If you want a animated moving textbox, try this:

Define a timer and try the following code:

C#
public partial class Form1 : Form
{
    int tickX = 0;
    int tickY = 0;
    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        this.textBox1.Location = new System.Drawing.Point(28, 38);
    }
    private void button1_Click(object sender, EventArgs e)
    {
        textBox1.Location = new Point(0, 0);
        timer1.Enabled = true;
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
        this.textBox1.Location = new System.Drawing.Point(tickX, tickY);
        tickX++;
        tickY++;
    }
}
 
Share this answer
 
v2
private void Window_MouseMove_1(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
this.DragMove();
}



Above Code Move Form Any Direction in
 
Share this answer
 
Comments
[no name] 7-Jul-14 7:35am    
Did you even bother reading the question before posting? And, do you think that it's possible that in the past 3 years, the OP has learned to read the documentation?

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