none
How to focus in textbox1 RRS feed

  • Question

  • If Dr(0) = "Administrator" then Return to ComboBox Selected Index = 0. Then If Dr(0)<> "Administrator" then Focus to Textbox1. Please solve my Problem. My Code is below :

    Private Sub cboRole_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboRole.SelectedIndexChanged
            If cboRole.SelectedIndex = 0 Then
                cboRole.Focus()
            Else
                Cons()
                Str = "Select Role From Users where Role= Administrator"
                Cmd = New MySqlCommand(Str, Con)
                Dr = Cmd.ExecuteReader
                If Dr.Read() Then
                    MessageBox.Show("User Role Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
                    cboRole.SelectedIndex = 0
                    cboRole.Focus()
                Else
                    txtName.Focus()
                End If
                connection_closed
            End If
        End Sub


    dopenoinam


    • Edited by Dopen Oinam Saturday, June 17, 2017 4:45 PM
    Saturday, June 17, 2017 12:41 PM

All replies

  • This example is similar and seems to work here.

    Are you sure you are executing the set Focus statement?

    Put a break point on the focus lines and see if they are executed or maybe you have another error somewhere.

    Option Strict On

    Public Class Form2 Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load ComboBox1.Items.Add("TextBox 1") ComboBox1.Items.Add("TextBox 2") End Sub Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged If ComboBox1.Items(ComboBox1.SelectedIndex) Is "TextBox 1" Then TextBox1.Focus() Else TextBox2.Focus() End If End Sub End Class



    Saturday, June 17, 2017 1:25 PM
  • PS Make sure your project is set to break on errors in debug using the menu item Debug - Window - Exceptions (or just Debug - Exceptions depending on VS version) and make sure all items for Common Language Runtime errors is checked. Otherwise you could be getting an error but Visual Studio is not showing it.

    PS Put the line:

       Option Strict On

    as the first line in your form code as I show in my example. This will make VS show any errors to you while editing.

    Saturday, June 17, 2017 1:36 PM
  • Hi Dopen,

    I use one DataTable to bind ComboBox's DataSource, and if the ComboBox.Text="Administrator", the ComboBox.Focus(), if the ComboBox.Text<>" Administrator ", I set TextBox.Focue().

    Is this the effect you want? If so, please refer to the code below.

    Private Sub fun()
            Dim dt As New DataTable()
            dt.Columns.Add("Id", GetType(Int32))
            dt.Columns.Add("UserName", GetType(String))
    
            dt.Rows.Add(1, "--select--")
            dt.Rows.Add(2, "Administrator")
            dt.Rows.Add(3, "B")
            dt.Rows.Add(4, "C")
    
            ComboBox1.DataSource = dt
            ComboBox1.DisplayMember = "UserName"
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            fun()
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            If ComboBox1.Text.ToString() = "Administrator" Then
                ComboBox1.Focus()
            Else
                TextBox1.Focus()
    
            End If
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 19, 2017 8:42 AM
    Moderator
  • Dopen,

    Maybe better to tell what is the logical problem. 

    It seems you use MySQL and want to create a Role Table. 

    However, then it becomes vague because you have already code and that seems not to be made for that purpose. 

    Some here like guessing, most of those who try to help do that not. 


    Success
    Cor


    Monday, June 19, 2017 9:42 AM
  • In order to solve the SQL error, try apostrophes or quotation marks:

       Str = "Select Role From Users where Role='Administrator' "

    But this is a temporary solution, because you should use parameterised queries and probably the current value of the combo-box (cboRole.Text or .SelectedValue).

    Then adjust the code to deal correctly with Administrator role. The query does not seem necessary in case of non-Administrators.

    Monday, June 19, 2017 11:12 AM