Answered Get button name into textbox when clicked

dbtryout

Member
Joined
Nov 14, 2018
Messages
14
Programming Experience
Beginner
It seems that my code doesn't seem to work very much.​

(The code to get the tag data from buttons into a textbox), nothing really happens when I press a button...

Here's the code I use in a class module:


[/COLOR][/INDENT]

[INDENT][COLOR=#333333]Public Class clsbuttons[/COLOR][/INDENT]

[INDENT][COLOR=#333333]    Public WithEvents Btn As Button[/COLOR][/INDENT]

[INDENT][/INDENT]

[INDENT][/INDENT]

[INDENT][COLOR=#333333]    Private Sub Btn_Click()[/COLOR][/INDENT]

[INDENT][COLOR=#333333]        Form5.TextBox3.Text = Btn.Tag[/COLOR][/INDENT]

[INDENT][COLOR=#333333]        Form5.TextBox5.Text = Format(Now(), "yyyy-mm-dd")[/COLOR][/INDENT]

[INDENT][COLOR=#333333]        Form5.Show()[/COLOR][/INDENT]

[INDENT][COLOR=#333333]    End Sub[/COLOR][/INDENT]

[INDENT][COLOR=#333333]End Class[/COLOR][/INDENT]

[INDENT][COLOR=#333333]


Here's the code I use in the normal form with the buttons:


[/COLOR][/INDENT]

[INDENT][COLOR=#333333]Public Class Form4[/COLOR][/INDENT]

[INDENT][COLOR=#333333]    Dim colButtons As New Collection[/COLOR][/INDENT]

[INDENT][COLOR=#333333]    Private Sub Form4_Initialize(sender As Object, e As EventArgs) Handles MyBase.Load[/COLOR][/INDENT]

[INDENT][COLOR=#333333]        Dim ctl As Control[/COLOR][/INDENT]

[INDENT][COLOR=#333333]        Dim obEvents As clsbuttons[/COLOR][/INDENT]

[INDENT][/INDENT]

[INDENT][/INDENT]

[INDENT][COLOR=#333333]        For Each ctl In Controls[/COLOR][/INDENT]

[INDENT][COLOR=#333333]            If TypeOf ctl Is Button Then[/COLOR][/INDENT]

[INDENT][COLOR=#333333]                obEvents = New clsbuttons[/COLOR][/INDENT]

[INDENT][COLOR=#333333]                obEvents.Btn = ctl[/COLOR][/INDENT]

[INDENT][COLOR=#333333]                If obEvents.Btn.Tag Like "GLUE*" Then[/COLOR][/INDENT]

[INDENT][COLOR=#333333]                    colButtons.Add(obEvents)[/COLOR][/INDENT]

[INDENT][/INDENT]

[INDENT][/INDENT]

[INDENT][COLOR=#333333]                End If[/COLOR][/INDENT]

[INDENT][COLOR=#333333]            End If[/COLOR][/INDENT]

[INDENT][COLOR=#333333]        Next ctl[/COLOR][/INDENT]

[INDENT][/INDENT]

[INDENT][/INDENT]

[INDENT][COLOR=#333333]    End Sub[/COLOR][/INDENT]

[INDENT][/INDENT]

[INDENT][/INDENT]

[INDENT][COLOR=#333333]End Class[/COLOR][/INDENT]

[INDENT][COLOR=#333333]


Pfff don't know what i'm doing wrong...:concern:
 
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,346
Location
Sydney, Australia
Programming Experience
10+
In your first code snippet, there's no connection between the field and the method. The whole point of declaring a field WithEvents is so that you can include that field in a Handles clause to handle the events of the object assigned tot hat field. You can see what an event handler should actually look like from your second code snipet. Ignoring the terrible name for the moment, that class should look like this:
Public Class clsbuttons
    Public WithEvents Btn As Button 
 
    Private Sub Btn_Click(sender As Object, e As EventArgs) Handles Btn.Click
        Form5.TextBox3.Text = CStr(Btn.Tag)
        Form5.TextBox5.Text = Date.Now.ToString("yyyy-MM-dd")
        Form5.Show()
    End Sub
End Class

Note that I have also fixed your date formatting. This is not VB6 so don't use Format or Now. Most critically though, lower-case "m" is for minutes when formatting dates/times, while upper-case "M" is for month. Finally, I've added a cast of the Tag property, which is type Object, to type String. You really ought to require that because you really ought to set Option Strict On.
 
Top Bottom