Question

I want to change the radiobutton location and make it move up while i am clicking button

tried this

private void up_MouseDown(object sender, MouseEventArgs e)
{
    while(P.Location.Y>0)
    P.Location = new System.Drawing.Point(P.Location.X, P.Location.Y - 1);    
}

P is a radiobutton


I want it to keep moving up while I'm pressing, but it's just jumping up to the up of the form. it's working good in debugging but it's really moving fast I want to slow the movement of the radiobutton and make it visible

Was it helpful?

Solution

Actually you are starting a while loop that will not exit until your RadioButton is at the top of your Form wether you are still pressing the Button or not. You can slow it down by putting a Thread.Sleep in your loop that way it is slowed down visible.

private void up_MouseDown(object sender, MouseEventArgs e)
{
    while (P.Location.Y > 0)
    {
        P.Location = new System.Drawing.Point(P.Location.X, P.Location.Y - 1);
        System.Threading.Thread.Sleep(10);
    }

}

If you want to have better control I would use a Timer. In this example the Interval is set to 10.

private void up_MouseDown(object sender, MouseEventArgs e)
{
    timer1.Start();
}

private void up_MouseUp(object sender, MouseEventArgs e)
{
    timer1.Stop();
}

private void timer1_Tick(object sender, EventArgs e)
{
    if (P.Location.Y > 0)
    {
        P.Location = new System.Drawing.Point(P.Location.X, P.Location.Y - 1);
    }
}

OTHER TIPS

You can use timer. Add a timer from the toolbox, say its name was timer1, then add following method:

private void P_MouseUp(object sender, MouseEventArgs e) {
    timer1.Enabled=false;
}

private void P_MouseDown(object sender, MouseEventArgs e) {
    timer1.Enabled=true;
}

private void timer1_Tick(object sender, EventArgs e) {
    if(P.Location.Y>0)
        P.Location=new System.Drawing.Point(P.Location.X, P.Location.Y-1);
}

You can change the interval of timer1 in properties window. I guess you write this for fun; so, have fun!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top