• Hello and welcome to our new forums. We upgraded our forum sites to a more robust and modern system which we hope you will enjoy. Be sure to check out your profile by clicking the button on the top right and configure your preferences, signature, time zone, avatar, etc. as you wish. If you need help with using this new forum'ware try the help link on the bottom right.

    Click here to review your account now.

Question How to change button control tags & tooltip info at runtime?

dbtryout

Member
Joined
Nov 14, 2018
Messages
12
Programming Experience
Beginner
Hi everyone,

I face some issues on how i'm about to change a button.text and *.tag info.

I have over 5000 buttons(in total) on over 100 forms.
I created a Treeview on a form which i can enter only, that shows all my forms with buttons & tag names.

For example:
a textbox has the name of the button (fe. button1)
a tag name could include name of the product and a serialnumber, so does the tooltip.

If the productname changes, and so does the serialnumber how do i update the appropriate tag and tooltip?

The top textbox is showing what is selected in the treeview.
The down one needs to be entered manually
The update button would then update the tag and tooltip...

4435

I'm stuck.
 
Last edited:

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,511
Location
Lansing, MI; USA
Programming Experience
10+
Setting the tag property is easy, instead of .Text = "" it's .Tag = ""
Setting the tooltip isn't difficult either, you call the SetToolTip() function on your form's ToolTip control, you pass it the control you want the tooltip changed on followed by the text you want it to say. For example ToolTip1.SetToolTip(YourTextBox, "Tool tip text here")

ToolTip.SetToolTip(Control, String) Method (System.Windows.Forms) | Microsoft Docs
 

dbtryout

Member
Joined
Nov 14, 2018
Messages
12
Programming Experience
Beginner
Setting the tag property is easy, instead of .Text = "" it's .Tag = ""
Setting the tooltip isn't difficult either, you call the SetToolTip() function on your form's ToolTip control, you pass it the control you want the tooltip changed on followed by the text you want it to say. For example ToolTip1.SetToolTip(YourTextBox, "Tool tip text here")

ToolTip.SetToolTip(Control, String) Method (System.Windows.Forms) | Microsoft Docs

Hi JuggaloBrotha,

Yes indeed, this is the way. But i'm having difficulties with how i'm about to tell the program that the selection that is made needs to change that particular button on that particular form.
 

dbtryout

Member
Joined
Nov 14, 2018
Messages
12
Programming Experience
Beginner
Hi JuggaloBrotha,

Yes indeed, this is the way. But i'm having difficulties with how i'm about to tell the program that the selection that is made needs to change that particular button on that particular form.
Actually what I need is the following:

I have a treeview from all forms and the buttons on these forms.
When i click a particular button name on that treeview i have the buttonname clicked projected in a textbox.
I want to put a new tag description for that particular button, weather it is on form 1, form 2, or form X
I click the update button to be save the new tag description to the button name selected in the treeview
I see the problem is that the form possibly isn't open. So how to get a buttons properties on a closed form in running mode?
 

JimA

New member
Joined
Feb 14, 2012
Messages
4
Location
New Mexico, USA
Programming Experience
10+
If I understand the issue correctly, you're trying to access button controls on form2 from code in form1.

Place a button named 'btnTryIt' on Form1.

On Form2, place 5 buttons named 'Button1' through 'Button5', then

Try this in Form1:

Private Sub Btn_Click(sender As Object, e As EventArgs) Handles btnTryIt.Click

Change_button("Form2", "Button3")

End Sub
Private Sub Change_button(FormName As String, ButtonName As String)


Dim YourProjectName = "tmp" & "."
Dim typ = Type.GetType(YourProjectName & FormName)
Dim frm = DirectCast(Activator.CreateInstance(typ), Form)
frm.Show()
Dim Ctrls As Control.ControlCollection = frm.Controls
Dim c = Ctrls.Find(ButtonName, False).First
c.Tag = "I'm a tag"
c.Text = c.Tag.ToString

End Sub

This example will change the tag and visible text on Button3 of Form2.

It will also open/show Form2. I know of no way to do it without showing the form. If that's important, you might try immediately using Form2.Hide, setting Form2.Visible = False or setting it to open in minimized.
 
Last edited:

dbtryout

Member
Joined
Nov 14, 2018
Messages
12
Programming Experience
Beginner
If I understand the issue correctly, you're trying to access button controls on form2 from code in form1.

Place a button named 'btnTryIt' on Form1.

On Form2, place 5 buttons named 'Button1' through 'Button5', then

Try this in Form1:

Private Sub Btn_Click(sender As Object, e As EventArgs) Handles btnTryIt.Click

Change_button("Form2", "Button3")

End Sub
Private Sub Change_button(FormName As String, ButtonName As String)


Dim YourProjectName = "tmp" & "."
Dim typ = Type.GetType(YourProjectName & FormName)
Dim frm = DirectCast(Activator.CreateInstance(typ), Form)
frm.Show()
Dim Ctrls As Control.ControlCollection = frm.Controls
Dim c = Ctrls.Find(ButtonName, False).First
c.Tag = "I'm a tag"
c.Text = c.Tag.ToString

End Sub

This example will change the tag and visible text on Button3 of Form2.

It will also open/show Form2. I know of no way to do it without showing the form. If that's important, you might try immediately using Form2.Hide, setting Form2.Visible = False or setting it to open in minimized.
Thanks for the reply.

For the moment i'm coding like this:
----------------------------
In the load event:

Visual Basic:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For Each formprop In My.Forms.GetType.GetProperties 'percorre todos os forms
            Dim node = Me.TreeView1.Nodes.Add(formprop.Name)
            Dim form As Form = CType(formprop.GetValue(My.Forms, Nothing), Form)
            ControlsTree(node, form.Controls)
        Next
    End Sub
--------------------------------------------------
The tree event:

Visual Basic:
Private Sub ControlsTree(ByVal node As TreeNode, ByVal controls As Control.ControlCollection)
        For Each ctrl As Control In controls
            If ctrl.Name <> String.Empty And ctrl.GetType.Name = "Button" And ctrl.Tag <> "" Then
                Dim child = node.Nodes.Add(ctrl.Tag).Nodes.Add(ctrl.Name)
                ControlsTree(child, ctrl.Controls)
            End If
        Next
    End Sub
This is bringing up the treeview of all forms as parent, with all buttons/form as child, and all tag descriptions/buttons as 2nd child:
--------------------------------------------------------------------------
After I select a tag I get the child and the parent in 2 seperate textboxes.

Visual Basic:
Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterSelect

        TextBox3.Text = e.Node.Text
        If e.Node.Parent IsNot Nothing AndAlso e.Node.Parent.[GetType]() = GetType(TreeNode) Then
            TextBox1.Text = e.Node.Parent.Text
        Else
            TextBox1.Text = "No control selected"
        End If
    End Sub
------------------------------------------------------------------------------------------------------------------
If the textbox which returns the Parent(root), which is a formname, I hide the form by this code:


Visual Basic:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim asm = System.Reflection.Assembly.GetExecutingAssembly

        Dim myTypes As Type() = asm.GetTypes()
        Dim frm As Form

        For Each t As Type In myTypes
            If t.IsSubclassOf(GetType(System.Windows.Forms.Form)) AndAlso TextBox1.Text = t.Name Then
                frm = CType(Activator.CreateInstance(t), Form)
                frm.Hide()
            End If
-----------------------------
then I perform a search over all controls which are buttons, from which the .Tag equals textbox3 (the selected node)
if the system finds the control with that tag, that tag needs to be changed with the text of textbox 2 (doesn't work)



Visual Basic:
 For Each ctrl As Control In Controls
                If (TypeOf ctrl Is Button) And ctrl.Tag = TextBox3.Text Then
                    ctrl.Tag = TextBox2.Text
                End If

            Next ctrl
-------------------------------------------
Refresh Treeview:


Visual Basic:
TreeView1.Nodes.Clear()

            For Each formprop In My.Forms.GetType.GetProperties 'percorre todos os forms
                Dim node = Me.TreeView1.Nodes.Add(formprop.Name)
                Dim form As Form = CType(formprop.GetValue(My.Forms, Nothing), Form)
                ControlsTree(node, form.Controls)
            Next
        Next


End sub
4437
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,511
Location
Lansing, MI; USA
Programming Experience
10+
Actually what I need is the following:

I have a treeview from all forms and the buttons on these forms.
When i click a particular button name on that treeview i have the buttonname clicked projected in a textbox.
I want to put a new tag description for that particular button, weather it is on form 1, form 2, or form X
I click the update button to be save the new tag description to the button name selected in the treeview
I see the problem is that the form possibly isn't open. So how to get a buttons properties on a closed form in running mode?
When you click on an item in the TreeView you change a couple of controls on your form, at least a Label and Textbox towards the top, that tells me you have code that does a lookup of what's selected in the TreeView to know what values to set those control's Text property to. You'd do the same for settings the SetToolTip() function as well except one of the parameters is a Control itself (the Label or Textbox).
Without knowing what your code is I can't specifically what you need to put in to set it, but you have all the knowledge you need on how to set a ToolTip and you know what your code currently does as well as what you want the program to do when the Update button is clicked. Just put all the pieces together.
 

dbtryout

Member
Joined
Nov 14, 2018
Messages
12
Programming Experience
Beginner
When you click on an item in the TreeView you change a couple of controls on your form, at least a Label and Textbox towards the top, that tells me you have code that does a lookup of what's selected in the TreeView to know what values to set those control's Text property to. You'd do the same for settings the SetToolTip() function as well except one of the parameters is a Control itself (the Label or Textbox).
Without knowing what your code is I can't specifically what you need to put in to set it, but you have all the knowledge you need on how to set a ToolTip and you know what your code currently does as well as what you want the program to do when the Update button is clicked. Just put all the pieces together.
Wish it was that easy... :cautious:
 

dbtryout

Member
Joined
Nov 14, 2018
Messages
12
Programming Experience
Beginner
Doesn't make any sense 😕

changed this:
Visual Basic:
 For Each ctrl As Control In Controls
                If (TypeOf ctrl Is Button) And ctrl.Tag = TextBox3.Text Then
                    ctrl.Tag = TextBox2.Text
                End If

            Next ctrl
to this:
Visual Basic:
Dim thisButtonName As String = TextBox3.Text ' This is the name of the button I'm looking for
                Dim thisButtonName2 As String = TextBox2.Text ' This is the new TAG name
                ' Loop all controls in this form
                For Each ctrl As Control In Controls
                    ' Is this control a button
                    If TypeOf (ctrl) Is Button Then
                        ' Is this the correct button
                        If CType(ctrl, Button).Name = thisButtonName Then
                            ' Set property's value
                            CType(ctrl, Button).Tag = thisButtonName2
                        End If
                    End If
                Next
And still doesn't work ... 🤬
 
Top Bottom