Forcing focus to treeview

Last post 09-04-2003 16:16 by Anonymous. 8 replies.
Page 1 of 1 (9 items)
Sort Posts: Previous Next
  • 09-04-2003 13:35

    Forcing focus to treeview

    I've written a small app in a way such that focus constantly moves to the treeview when you do something. I did this so that I could use the app without the stylus. I added "treeView1.Focus();" throughout the app to accomplish this. I've found 2 cases where focus doesn't end up in the tree, I'm looking for suggestions on this..

    1. If I run my app first, then run another app, when I switch back to my app focus is nowhere. How can I determine when my app is active and move focus back to the treeview?

    2. There are 2 radio buttons on the form. Currently I capture when either button changes and immediately treeView1.Focus(); which works great. But if I purposely click the same radio button twice, focus is lost and no longer appears in the tree.

    I'm using Visual C# .NET 2003, any suggestions are welcome.
  • 09-04-2003 13:54 In reply to

    Re: Forcing focus to treeview

    1. Override your forms's OnActivated method e.g. :

    Protected Overrides Sub OnActivated(ByVal e As EventArgs)
    MyBase.OnActivated(e)
    TreeView1.Focus()
    End Sub

    2. Not sure with this one, are you changing the focus in the CheckedChanged event or in the GotFocus event of the RadioButton?
  • 09-04-2003 14:26 In reply to

    Re: Forcing focus to treeview

    > 2. Not sure with this one, are you changing the focus in the CheckedChanged event or in the GotFocus event of the RadioButton?

    I'm changing the focus in the CheckedChanged event of each Radio Button.

    Thanks for the OnActivated info.
  • 09-04-2003 14:45 In reply to

    Re: Forcing focus to treeview

    Ok I know what's happening. The CheckedChanged event is not fired if the checked state does not change, however when you click on a RadioButton it still receives focus. This is why the TreeView control is losing focus but not regaining it. Try placing TreeView1.Focus() in the GotFocus event of both RadioButtons.

    Hope this works [:)]
  • 09-04-2003 14:50 In reply to

    Re: Forcing focus to treeview

    > Try placing TreeView1.Focus() in the GotFocus event of both RadioButtons.

    How do I do that? [:)]
    I'm running Visual C# .NET 2003 (NOT VB)
  • 09-04-2003 15:24 In reply to

    Re: Forcing focus to treeview

    private void radioButton1_GotFocus(object sender, System.EventArgs e)
    {
    treeView1.Focus;
    }

    And do the same for the other radiobutton [:)]
  • 09-04-2003 15:39 In reply to

    Re: Forcing focus to treeview

    I added it and it compiled successfully but it didn't change the behavior. [:(]

    Do I need to add something below InitializeComponent();?

    Thanks for all your help
  • 09-04-2003 16:16 In reply to

    Re: Forcing focus to treeview

    Ok, for the designer to do all the code do the following :

    1.Click on the radiobutton on your form so that it shows all properties for it in the Properties tab.
    2.Click the button with a yellow lightning rod (somewhere near the top of the Properties tab) which is used to display the events (This is not available in VB.NET)
    3.Double click on the GotFocus Event.

    This should generate the code and then you only need to put in the treeView1.Focus; part. Do the same on the other radiobutton.
  • 09-04-2003 16:16 In reply to

    Re: Forcing focus to treeview

    Nevermind, I added

    this.radioButton1.Click += new EventHandler(radioButton1_Click);

    under InitializeComponent(); and it's finally working! [:D]
Page 1 of 1 (9 items)