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.

Dani AI

Generated

Short summary: wanted each of three buttons to receive focus for one second in sequence. 's Timer approach is the right direction; the common causes for "doesn't work" are using a non-UI timer or trying to set focus from a background thread. For WinForms, use System.Windows.Forms.Timer so the Tick runs on the UI thread, and cycle the focused control with a simple index modulo the button count.

A compact, different implementation that loops forever:

List<Button> cycle;
int current = 0;
System.Windows.Forms.Timer uiTimer;

public Form1()
{
    InitializeComponent();
    cycle = new List<Button> { button1, button2, button3 };
    uiTimer = new System.Windows.Forms.Timer { Interval = 1000 };
    uiTimer.Tick += (s,e) =>
    {
        current = (current + 1) % cycle.Count;
        cycle[current].Select();
    };
    uiTimer.Start();
}

protected override void OnShown(EventArgs e)
{
    base.OnShown(e);
    cycle[0].Select();
}

Troubleshooting tips and practical notes:

  • Programmatic focus only works if a control is Visible, Enabled and TabStop is true. If a control refuses focus, check those properties.
  • Some Windows themes hide the dotted focus rectangle unless the user navigated with the keyboard. To make the active button obvious, change its appearance on Enter/Leave (for example change BackColor) instead of relying on the focus cue.
  • Never manipulate WinForms controls from background timers (System.Threading.Timer or System.Timers.Timer) without Invoke/BeginInvoke; doing so causes exceptions or silent failures. If another timer type is used, marshal back to the UI thread.
  • Stop and Dispose the timer on FormClosing. To pause the cycle when the user interacts, stop the timer in Click/Enter handlers and restart when appropriate.

This complements 's working example and shows a minimal, robust way to get an indefinite cycle with clearer visual feedback and fewer cross-thread pitfalls.

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.