I have a windows form application which has a text box and 3 buttons. Now i need to add focus to a button for 1 second, and then add focus to the other button for a second.. like wise to adding focus to all these 3 buttons for 1 second each.

How do i do this. I have tried everything but nothing worked. Can someone help me here or link me to a website that does this.

Recommended Answers

All 7 Replies

public partial class Form1 : Form
    {
        Timer timer1;
        Button[] btns;
        public Form1()
        {
            InitializeComponent();
            timer1 = new Timer();
            timer1.Interval = 1000;
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Start();

            //create an array of buttons:
            btns = new Button[] { button1, button2, button3 };
        }

        protected override void OnShown(EventArgs e)
        {
            //set the button1 to be focused on the beginning:  
            button1.Focus();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < btns.Length; i++)
            {
                if (i.Equals(2))
                    timer1.Stop();
                else if (btns[i].Focused)
                {
                    btns[i + 1].Focus();
                    break;
                }                
            }
        }
    }

What you have to do is:
- put three buttons on the forms (do not chaange their namee, by defualt they will be names: button1, button2, button3)
- put a textBox
- and put a label over textBox

(so you only have to put 5 controls explained above).

Then you simple copy / paste the code I gave you. But be careful on th Form1 name. Form1 is by defaurt, if you change the project name when creating it, it will have different name.

Maybe you have to change the Timer namespace to:

System.Windows.Forms.Timer timer1;

//when creating a new instance you do:
timer1 = new ystem.Windows.Forms.Timer();

What you have to do is:
- put three buttons on the forms (do not chaange their namee, by defualt they will be names: button1, button2, button3)
- put a textBox
- and put a label over textBox

(so you only have to put 5 controls explained above).

Then you simple copy / paste the code I gave you. But be careful on th Form1 name. Form1 is by defaurt, if you change the project name when creating it, it will have different name.

Thank you very much Mitja Bonca, it works ! Now i am trying to edit the code in a way that it could focus back on button1 and so on like an infinite loop.

I am glad I could help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.