none
New form focus problem RRS feed

  • Question

  • I think I found something that it should work but it doesn't.
    Here is how my example works...
    The first form, "frmBase," is a base forum that has only one button, "exit," (with the modifier set to "protected") that does the following...
    Public
     Class
     frmBase
        Private
     Sub
     btnExit_Click(ByVal
     sender As
     System.Object, ByVal
     e As
     System.EventArgs) Handles
     btnExit.Click
            Me
    .Close()
        End
     Sub
    
    End
     Class
    
    
    The second form, "frmMain", uses the inherited exit button from "frmBase", and contains three additional textboxes: txtCostPrice, txtRetailPrice, and txtDescription. The following code uses a shared event when either those three loses focus.
    Here's the code for that form...
    Public
     Class
     frmMain
        Private
     Sub
     stockTextBox_Leave(ByVal
     sender As
     System.Object, ByVal
     e As
     System.EventArgs) Handles
     _<br/>
    txtCostPrice.Leave, txtDescription.Leave, txtRetailPrice.Leave
            Dim
     genericTextBox As
     TextBox
            genericTextBox = CType
    (sender, TextBox)
    
            If
     genericTextBox.Text.Length > 0 And
     genericTextBox.Modified Then
    
                Dim
     textboxName As
     String
     = genericTextBox.Name
                Dim
     textboxValue As
     String
     = genericTextBox.Text
                Dim
     stockForm As
     New
     frmDialog(textboxName, textboxValue)
                stockForm.ShowDialog()
            End
     If
    
        End
     Sub
    
    End
     Class
    
    
    Finally, the third form, "frmDialog" contains three textBoxes, named the same as frmMain. This is what it has...
    Public Class frmDialog
        Dim mstrColumnName As String, mstrColumnValue As String
    
        Public Sub New(ByVal columnName As String, ByVal columnValue As String)
            InitializeComponent()
    
            mstrColumnName = columnName
            mstrColumnValue = columnValue
        End Sub
    
        Private Sub frmDialog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            txtRetailPrice.Focus()
        End Sub
    End Class
    
     
    Now, the problem in the frmDialog form is that it couldn't set focus to txtRetailPrice. Instead, it would always focus on txtDescription because it is the first on the tab index.

    I'm not sure if this is the proper way to set focus when using a form that was instantiated by another form. (Hence the constructor)
    I'm pretty much stumped. How can I set focus to other textboxes other than txtDescription when frmDialog loads up?
    • Edited by johnsprogram Tuesday, April 21, 2009 8:00 PM changed the third code to view as vbnet
    Tuesday, April 21, 2009 7:59 PM

Answers

  • You cannot set Focus() until a control is visible.  Use txtRetailPrice.Select() instead.

    Please post these kind of questions to the Windows Forms General forum in the future.
    Hans Passant.
    • Marked as answer by johnsprogram Tuesday, April 21, 2009 11:41 PM
    Tuesday, April 21, 2009 9:25 PM
    Moderator