text box movement

R

RobcPettit

Hi, Can somebody point in the right area? If I have 6 textboxes in a
line vertically, I want to move them across the screen, left to right.
They would each have individual positions, so they would look like
there moving at different speeds. Id like to do this every 2 seconds,
with the end result bieng after 5 mins they would be either spread
across the screen or at the oher end. The text box contains a value
will determine its position. I suppose its like charting without the
x,y axis. Im trying to simulate them racing each other accross the
screen. I dont know what to google for though. Ive tried gaming and
graphics.
Regards Robert
 
M

Morten Wennevik [C# MVP]

Hi Robert,

I don't really understand what you are trying to do, but attached is some
sample code that will move a set of TextBoxes as well as some UserControls
with varying speeds. You need a timer to trigger each movement, then in the
timer event adjust the position of the control. You might be better off
using a UserControl than using a TextBox.

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}

protected override void OnLoad(EventArgs e)
{
Button button1 = new Button();
button1.Click += new EventHandler(button1_Click);

timer1.Interval = 100;
timer1.Tick += new EventHandler(timer1_Tick);

for (int i = 0; i < 5; i++)
{
TextBox tb = new TextBox();
tb.Location = new Point(0, (tb.Height + 1) * i );
Controls.Add(tb);

Car car = new Car(i);
car.Location = new Point(0, (5 * (tb.Height + 1) + 5) +
(car.Height + 1) * i);
Controls.Add(car);
}
}

void timer1_Tick(object sender, EventArgs e)
{
foreach (Control c in Controls)
{
if (c is TextBox)
c.Location = new Point(c.Left + 1, c.Top);
else if (c is Car)
((Car)c).MoveCar();
}
}

Timer timer1 = new Timer();
private void button1_Click(object sender, EventArgs e)
{
if (timer1.Enabled)
timer1.Enabled = false;
else
timer1.Enabled = true;
}

class Car : UserControl
{
private int _speed;

public Car(int speed)
{
_speed = speed;
Size = new Size(100, 20);
BorderStyle = BorderStyle.FixedSingle;
}

public void MoveCar()
{
Location = new Point(Left + _speed, Top);
}
}
}
 
R

RobcPettit

Thankyou for your reply, thats just what I need. I was planning on
using usercontrols also, so this is great.
Regards Robert


Hi Robert,

I don't really understand what you are trying to do, but attached is some
sample code that will move a set of TextBoxes as well as some UserControls
with varying speeds.  You need a timer to trigger each movement, then inthe
timer event adjust the position of the control.  You might be better off
using a UserControl than using a TextBox.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }

    protected override void OnLoad(EventArgs e)
    {
        Button button1 = new Button();
        button1.Click += new EventHandler(button1_Click);

        timer1.Interval = 100;
        timer1.Tick += new EventHandler(timer1_Tick);

        for (int i = 0; i < 5; i++)
        {
            TextBox tb = new TextBox();
            tb.Location = new Point(0, (tb.Height + 1) * i );
            Controls.Add(tb);

            Car car = new Car(i);
            car.Location = new Point(0, (5 * (tb.Height + 1)+ 5) +
(car.Height + 1) * i);
            Controls.Add(car);
        }
    }

    void timer1_Tick(object sender, EventArgs e)
    {
        foreach (Control c in Controls)
        {
            if (c is TextBox)
                c.Location = new Point(c.Left + 1, c.Top);
            else if (c is Car)
                ((Car)c).MoveCar();
        }
    }

    Timer timer1 = new Timer();
    private void button1_Click(object sender, EventArgs e)
    {
        if (timer1.Enabled)
            timer1.Enabled = false;
        else
            timer1.Enabled = true;
    }

    class Car : UserControl
    {
        private int _speed;

        public Car(int speed)
        {
            _speed = speed;
            Size = new Size(100, 20);
            BorderStyle = BorderStyle.FixedSingle;
        }

        public void MoveCar()
        {
            Location = new Point(Left + _speed, Top);
        }
    }

}

--
Happy Coding!
Morten Wennevik [C# MVP]



Hi, Can somebody point in the right area? If I have 6 textboxes in a
line vertically, I want to move them across the screen, left to right.
They would each have individual positions, so they would look like
there moving at different speeds. Id like to do this every 2 seconds,
with the end result bieng after 5 mins they would be either spread
across the screen or at the oher end. The text box contains a value
will determine its position. I suppose its like charting without the
x,y axis. Im trying to simulate them racing each other accross the
screen. I dont know what to google for though. Ive tried gaming and
graphics.
Regards Robert- Hide quoted text -

- Show quoted text -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top