[RESOLVED] calling textbox_keypress from within button_click event ,how to?-VBForums
Results 1 to 9 of 9

Thread: [RESOLVED] calling textbox_keypress from within button_click event ,how to?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2015
    Posts
    30

    Resolved [RESOLVED] calling textbox_keypress from within button_click event ,how to?

    Code:
      Private Sub TextBox72_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox72.KeyPress
            Dim faCal As New System.Globalization.PersianCalendar
            Dim d As String = TextBox72.Text
            Dim z As Double = 0
            Select Case e.KeyChar
                Case Chr(Keys.Enter)
                    TextBox17.Text = Format(TextBox72.Text, "####/##/##")
    
                    DataGridView2.DataSource = gatherIncome(My.Settings.Conn, d)
                    DataGridView2.Columns(0).Width = 70
                    DataGridView2.Columns(1).Width = 200
                    DataGridView2.Columns(2).Width = 150
                    For x = 0 To DataGridView2.RowCount - 1
                        z = z + DataGridView2.Rows.Item(x).Cells(2).Value
                    Next
                    tIncome.Text = Format(z, "###,###")
            End Select
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            TextBox72.Text = DateAdd(DateInterval.Day, -1, CDate(TextBox72.Text))
            TextBox72_KeyPress(Me, e)
       
        End Sub
    hi dears
    I want to pass ENTER to textbox_keypress frow with[CODE]in Button_click,let me know how can i do that.
    sincere bayat

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    31,553

    Re: calling textbox_keypress from within button_click event ,how to?

    Take all the code in the Keypress event handler and move it into a Sub, then call that sub from both the Keypress event handler and the button click event handler. However, consider the arguments you want to send to that sub. Most of what is in the Keypress e argument is stuff that you aren't using. In fact, the only item you want is a keyCharacter. In fact, all you want is the enter key. So, what you could do is make a sub like this:
    Code:
    Public Sub EnterKeyPressed()
                    Dim faCal As New System.Globalization.PersianCalendar
                    Dim d As String = TextBox72.Text
                    Dim z As Double = 0
                    TextBox17.Text = Format(TextBox72.Text, "####/##/##")
    
                    DataGridView2.DataSource = gatherIncome(My.Settings.Conn, d)
                    DataGridView2.Columns(0).Width = 70
                    DataGridView2.Columns(1).Width = 200
                    DataGridView2.Columns(2).Width = 150
                    For x = 0 To DataGridView2.RowCount - 1
                        z = z + DataGridView2.Rows.Item(x).Cells(2).Value
                    Next
                    tIncome.Text = Format(z, "###,###")
    End Sub
    You can call that from the button click event handler. You can also call it from your keypress handler like this:
    Code:
    Private Sub TextBox72_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox72.KeyPress
            
            Select Case e.KeyChar
                Case Chr(Keys.Enter)
                    EnterKeyPressed()
            End Select
        End Sub
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2015
    Posts
    30

    Re: calling textbox_keypress from within button_click event ,how to?

    thanks for ur code .
    is there something more simple like vb6 in vb.net?

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    31,553

    Re: calling textbox_keypress from within button_click event ,how to?

    I don't know what that means. What are you asking for? A simpler language? In what way? VB6 left a whole lot out. People have been able to add to VB6 most of what was left out, but that wasn't simple.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2015
    Posts
    30

    Re: calling textbox_keypress from within button_click event ,how to?

    i meant that as far as i remember in vb6 , we had a simple way to call an event procedure from within another. somthing like this statement: Call Textbox_keypress.and that was very simple.no more code.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    31,553

    Re: calling textbox_keypress from within button_click event ,how to?

    You can still do that. An event handler is nothing more than a sub just like any other sub. What has happened is that every event handler in .NET takes a pair of arguments, the first of type Object and the second of some other type. You could call the method and pass Nothing, Nothing for the two arguments, or something else, if you wanted to. There's nothing stopping you from doing that, it just isn't good practice.

    In VB6 the event model was a bit different. You needed control arrays to allow one handler to handle events from multiple objects. In .NET, that is managed through the arguments. The first argument IS the object that raised the event. In other words, for your keypress event handler, the sender argument IS the textbox. You could simply cast it back to the textbox with something like:

    dim txtBox = DirectCast(sender,Textbox)

    and then you have the textbox to work with. Because of this, a single handler can handle events from lots of different controls, and even different types of events, as long as the event supplies the right type for the e argument. Therefore, your button click event handler could actually handle the keypress events, since the click event takes an EventArgs argument for e, and the KeyPress takes a KeyPressEventArgs, which derives from EventArgs. So, you could have the click handler handle both the click of the button and the keypress of the textbox....it's just a pretty bad idea to do it that way. After all, you would have to examine the e argument to determine whether it really was a KeyPressEventArgs, and cast it to one, in case it was. The cost of that examination and conversion is more than the cost of having a different handler for keypresses and button clicks.

    This ends up being a more versatile system. The 'magic' that was used in VB6 has been replaced with a clear and rational system. An event handler is just a method like any other method. You often ignore the arguments, in which case you wouldn't even care what they are. If you need the arguments, they supply consistent and reliable items. The first argument is ALWAYS the object that raised the event, the second argument may be useless (EventArgs is pretty useless), or it may contain information you might find useful when the event is raised, such as the key codes for a KeyPress event, or the mouse location for a MouseMove event. It's still just a method, though, and can be called like any other method. What I showed was better practice, but it's not the ONLY way.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Aug 2015
    Posts
    30

    Re: calling textbox_keypress from within button_click event ,how to?

    I tried calling textbox72_keypress(textbox72,e) or textbox72_keypress(sender,e)and this error was raised:
    Unable to cast object of type 'System.Windows.Forms.MouseEventArgs' to type 'System.Windows.Forms.KeyPressEventArgs'.
    as far as I understood your efficient explanation ,I could use code above . what I can't get is why the error raise.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    31,553

    Re: [RESOLVED] calling textbox_keypress from within button_click event ,how to?

    You must have gotten that error when you tried to call the method from within some other event handler. That would have also received an argument named e, it just would have been the wrong type. That second argument is ALWAYS named e, but it isn't always the same type. It's just like any other variable: The name of the variable isn't all that important, only the type is really important.

    Calling textbox72_keypress(textbox72, e) was pretty close, except that the second argument was wrong. Had you passed Nothing for the second argument, it would have mostly worked. You can't pass the RIGHT thing, though, unless you can build a KeyPressEventArgs (which you may be able to, I haven't looked). That won't work in your keypress event handler, though, because you check e.KeyCode. If you do that, and an incorrect e was passed in (such as Nothing), then the code would crash on that line. My original suggestion would solve that. The fact that it is so hard to get the right items to pass as arguments is one of the reasons that calling event handlers directly is discouraged.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Aug 2015
    Posts
    30

    Re: [RESOLVED] calling textbox_keypress from within button_click event ,how to?

    although late ,I appreciate all time you spent for spreading knowledge.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width