Problem calling one SUB from within another.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nfenlon
    New Member
    • Jul 2007
    • 8

    Problem calling one SUB from within another.

    Hi guys. I'm using VB express 2005 and im having trouble calling one sub function from inside another. Can you help?

    I have written an event procedure for a button click. Now i want to call that sub in the ENTER event of a textbox.
    I have tried simply copying the name of the sub into the ENTER sub but it wont work, i get the error message "expression expected" and an underline under the "ByVal" part of the argument.


    Imports MySql.Data.MySq lClient
    Imports System.Data

    Public Class frmLogin
    Dim conn As MySqlConnection

    Private Sub Button_cancel_C lick(ByVal sender As Object, ByVal e As System.EventArg s) Handles Button_cancel.C lick
    Application.Exi t() 'kills the application/process
    End Sub

    Private Sub Button_login_Cl ick(ByVal sender As Object, ByVal e As System.EventArg s) Handles Button_login.Cl ick
    'an example connection string: server=localhos t; user id=mike; password=12345; database=in_out
    conn = New MySqlConnection
    Dim Ser, Use, Pas As String
    Ser = TextBox_server. Text & ";"
    Use = TextBox_usernam e.Text & ";"
    Pas = TextBox_passwor d.Text & ";"
    '
    conn.Connection String = "server=" & Ser & "user id=" & Use & "password=" & Pas & "database=nfenl on_TestDB01"
    '
    Try
    conn.Open()
    MessageBox.Show ("Connection Opened Successfully")
    conn.Close()
    Catch myerror As MySqlException
    MessageBox.Show ("Error Connecting to Database: " & myerror.Message )
    Finally
    conn.Dispose()
    End Try
    '
    End Sub

    Private Sub frmLogin_Load(B yVal sender As Object, ByVal e As System.EventArg s) Handles Me.Load
    TextBox_server. Focus()
    End Sub

    Private Sub TextBox_passwor d_Enter(ByVal sender As Object, ByVal e As System.EventArg s) Handles TextBox_passwor d.Enter
    Button_login_Cl ick(byval sender As Object, byval e As System.EventArg s) Handles Button_login.Cl ick
    End Sub
    End Class
  • Sick0Fant
    New Member
    • Feb 2008
    • 121

    #2
    Originally posted by nfenlon
    Hi guys. I'm using VB express 2005 and im having trouble calling one sub function from inside another. Can you help?

    I have written an event procedure for a button click. Now i want to call that sub in the ENTER event of a textbox.
    I have tried simply copying the name of the sub into the ENTER sub but it wont work, i get the error message "expression expected" and an underline under the "ByVal" part of the argument.


    Imports MySql.Data.MySq lClient
    Imports System.Data

    Public Class frmLogin
    Dim conn As MySqlConnection

    Private Sub Button_cancel_C lick(ByVal sender As Object, ByVal e As System.EventArg s) Handles Button_cancel.C lick
    Application.Exi t() 'kills the application/process
    End Sub

    Private Sub Button_login_Cl ick(ByVal sender As Object, ByVal e As System.EventArg s) Handles Button_login.Cl ick
    'an example connection string: server=localhos t; user id=mike; password=12345; database=in_out
    conn = New MySqlConnection
    Dim Ser, Use, Pas As String
    Ser = TextBox_server. Text & ";"
    Use = TextBox_usernam e.Text & ";"
    Pas = TextBox_passwor d.Text & ";"
    '
    conn.Connection String = "server=" & Ser & "user id=" & Use & "password=" & Pas & "database=nfenl on_TestDB01"
    '
    Try
    conn.Open()
    MessageBox.Show ("Connection Opened Successfully")
    conn.Close()
    Catch myerror As MySqlException
    MessageBox.Show ("Error Connecting to Database: " & myerror.Message )
    Finally
    conn.Dispose()
    End Try
    '
    End Sub

    Private Sub frmLogin_Load(B yVal sender As Object, ByVal e As System.EventArg s) Handles Me.Load
    TextBox_server. Focus()
    End Sub

    Private Sub TextBox_passwor d_Enter(ByVal sender As Object, ByVal e As System.EventArg s) Handles TextBox_passwor d.Enter
    Button_login_Cl ick(byval sender As Object, byval e As System.EventArg s) Handles Button_login.Cl ick
    End Sub
    End Class
    I didn't bother reading all of you're code, but if you're trying to go to the code in the button click event when the user enters the text box, you could just have the click event handler handle the enter event as well.

    Comment

    • nfenlon
      New Member
      • Jul 2007
      • 8

      #3
      ok. great. how exactly do i do that???

      Comment

      • Sick0Fant
        New Member
        • Feb 2008
        • 121

        #4
        Originally posted by nfenlon
        ok. great. how exactly do i do that???
        Notice that the handler methods all have the keyword "handles" at the end of them. You can supply a comma-separated list of events that you want it to handle. Now, I'm not sure if you're going to be able to do that, since it's likely that Enter and click probably have different signatures. If that's the case, you can either call the click event directly from the enter handler, supplying Nothing as the arguments, or else both events can call a common function.

        Comment

        • nfenlon
          New Member
          • Jul 2007
          • 8

          #5
          Ok, not bad. It kind of works.
          But now it executes the code when i tab into or out of the text box. not when i hit enter...

          Comment

          • Sick0Fant
            New Member
            • Feb 2008
            • 121

            #6
            Originally posted by nfenlon
            Ok, not bad. It kind of works.
            But now it executes the code when i tab into or out of the text box. not when i hit enter...
            I thought you wanted the event to be handled when the control gained focus. If you want to handle the event for a user pressing the enter key, then handle the keypress event. Examine what key was pressed. If it was the enter key, then you'll want to call the method to deal with it.

            Comment

            • nfenlon
              New Member
              • Jul 2007
              • 8

              #7
              Thanks man.
              Sorry, didn't realise that the enter event was the same as getfocus, my mistake.

              One last thing; how to determine if the enter key was pressed??

              cheers

              Comment

              • Sick0Fant
                New Member
                • Feb 2008
                • 121

                #8
                Originally posted by nfenlon
                Thanks man.
                Sorry, didn't realise that the enter event was the same as getfocus, my mistake.

                One last thing; how to determine if the enter key was pressed??

                cheers
                Check if e.KeyChar is equal to the newline character (ASCII #13)

                Comment

                • nfenlon
                  New Member
                  • Jul 2007
                  • 8

                  #9
                  thanks again. that worked like a dream but i'm still gettin an error when i call the button sub in the keypress sub.
                  If i type the sub with no arguments i get an "expected argument" error and if i type it as it is below it doesn't seem to like the ByVal parts of the argument....

                  Button_login_Cl ick(ByVal sender As Object, ByVal e As System.EventArg s)

                  thanks for your help

                  Comment

                  • Sick0Fant
                    New Member
                    • Feb 2008
                    • 121

                    #10
                    Originally posted by nfenlon
                    thanks again. that worked like a dream but i'm still gettin an error when i call the button sub in the keypress sub.
                    If i type the sub with no arguments i get an "expected argument" error and if i type it as it is below it doesn't seem to like the ByVal parts of the argument....

                    Button_login_Cl ick(ByVal sender As Object, ByVal e As System.EventArg s)

                    thanks for your help
                    If you are calling the Button_login_Cl ick method, you have to supply the necessary arguments (you don't need to put ByVal if you're calling the method). In your case, you would call it like:

                    Button_login_Cl ick(Nothing, Nothing)

                    Comment

                    • nfenlon
                      New Member
                      • Jul 2007
                      • 8

                      #11
                      sweet!!!

                      Thanks for the help dude

                      Comment

                      Working...