Windows forms focus

rongrace

Member
Joined
Jul 2, 2012
Messages
23
Programming Experience
Beginner
Hi guys,

I'm pretty new to visual studio and I have a problem that I can't seem to resolve.
I created a simple form that has a toolstrip at the top of the form which accepts a parameter from the user. It then uses this parameter to populate a number of text fields on the form.
My problem is that when the form loads the focus is in the first text field on the form where as I want the focus to be in the toolstrip that is requesting the user to enter data. I've tried to do this with

Private Sub frmQuoteHead_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
QuoteNoToolStripTextBox.Focus()
End Sub

But this doesn't work, what am I missing
 
At the end of the Load process, the Form automatically gives focus to the control with the lowest TabIndex property (usually 0). Reorder the controls.
 
The toolstrip has the lowest tab index on the form. The field that actually gets the focus is the third field, as the first two are in the toolstrip
 
It's no use putting code related to focus in the Load event handler because that is executed before the form is displayed. You need to handle the Shown event, which is raised after the form is displayed, at which point focus is relevant. The code you use would be:
QuoteNoToolStripTextBox.TextBox.Select()
 
I've tried the following, but still no luck

Private Sub frmQuoteHead_Show(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
QuoteNoToolStripTextBox.TextBox.Select()
End Sub
 
Exactly what you showed worked for me. I suggest that you create a brand new project to test just that functionality. If it works there and not in your current project then there's obviously something else at play in your current project. If it doesn't work there either then I think that something might be broken. I tested in VB 2010 but I'm not aware of any reason that that should make a difference.
 
I tried a new project,same result. Maybe there is something amiss with my setup.
Somebody else said that there's a property setting that sets the focus on the toolstrip, but apart from the Tab index I can't see anything else that might do it, any ideas
 
Stick with your original command but move it to the shown event ... this worked for me.

VB.NET:
    Private Sub Form1_Shown(sender As System.Object, e As System.EventArgs) Handles MyBase.Shown


        ToolStripTextBox1.Focus()


    End Sub
 
Still no joy, here's the full forms code

Public
Class frmQuoteHead



Private Sub FillToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FillToolStripButton.Click


Try


Me.GetQuoteDetTableAdapter.Fill(Me.HP3000_HansonDataSet.GetQuoteDet, Me.CompanyNo, QuoteNoToolStripTextBox.Text)


Catch ex As System.Exception

System.Windows.Forms.MessageBox.Show(ex.Message)


End Try



End Sub


'Private Sub frmQuoteHead_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


' QuoteNoToolStripTextBox.Focus()


'End Sub


Private Sub frmQuoteHead_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown

QuoteNoToolStripTextBox.Focus()


End Sub



Private Sub btnMain_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMain.Click


Me.Hide()

frmButtonMenu.Show()


End Sub



Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click


End


End Sub



Private Sub btnSq_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSq.Click


Me.Hide()

frmSQ.Show()


End Sub



Private Sub frmQuoteHead_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load



End Sub

End
Class
 
rongrace said:
QuoteNoToolStripTextBox.Focus()
QuoteNoToolStripTextBox is a ToolStripItem and not a TextBox, focusing this will have no effect on the hosted TextBox control. Like jmc posted earlier, to select the hosted TextBox use access it from the TextBox property and use the Select method rather than the Focus method.
QuoteNoToolStripTextBox.TextBox.Select()
 
focusing this will have no effect on the hosted TextBox control

Er ... yes it will. I successfully shifted focus to each one of three embedded controls in turn using this method in a test program.

But if it won't do the job here, try every alternative!
 
Last edited:
What am I missing. I've tried all the suggestions without any luck. The focus still remains on the first text box on the form and not the textbox in the toolstrip
 
What version and edition of VS are you using? Your profile says .NET 2.0. Does that VB 2005 Express? If so, is there any reason that you can't update to VB 2010 Express?
 
You're correct I'm using 2005.
What I have noticed is that some of my screens are working correctly and none of them appear to need any extra coding to focus on the toolstrip, example code below

Public Class frmOrdProgSth
Private Sub FillToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FillToolStripButton.Click
Try
Me.GetProgressOfOrdersSouthTableAdapter.Fill(Me.HP3000_HansonDataSet.GetProgressOfOrdersSouth, AcnoToolStripTextBox.Text, PlantToolStripTextBox.Text, _DateToolStripTextBox.Text, Me.CompanyNo)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub frmOrdProgSth_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End
Class
 
That's why I asked whether the form in question was the initial form or was activated by some other process. But definitely get 2010 unless there's some really pressing reason not to. You may well find the the problem simply disappears with the updated .net framework.
 
Back
Top