Dan Rowe
Dan Rowe

Reputation: 151

Add tooltip control dynamically

I have a child form that is completely created in code. I would like to add tool tips to the textbox controls on the form. I know how to set up the tool tips on the fly but can't find a way to add the tooltip control to the form on the fly. All the hits I find on google refer to dragging the control from the designers toolbox.

I would need to do something like:

        ' Add tool tip control
        Dim toolTip1 As New ToolTip()
        toolTip1.ShowAlways = True
        frm.Controls.Add(toolTip1)

This does not work

I tried adding a sub to handle the from.load event(with a handler that poitn to the sub) at design time but can not get passed the error "Tooltip1 is not declared" when adding the tooltip to the iundividual dynamic controls.

If I call this dynamic form from a parent form and add the tooltip control to the parent form I can use it for the child form. But how would I do this if I was creating the form from a routine that does not live on a parent form?

thanks

        Dim frm As New Form
        ' Add tool tip control
        ''Dim toolTip1 As New ToolTip()
        ''toolTip1.ShowAlways = True
        'Draw the Form object

        'close the dynamic frm if existing already
        If frm IsNot Nothing Then
            frm.Close()
        End If

        frm = New Form()
        frm.AutoScaleDimensions = New System.Drawing.SizeF(6.0F, 13.0F)
        frm.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        frm.Name = "frm_test"
        'dimension is irrelevant at the moment
        frm.ClientSize = New System.Drawing.Size(10, 10)
        'the parent will be the current form
        'frm.MdiParent = this;
        'splash screen mode form, why not...
        frm.ControlBox = True
        frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        frm.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink
        frm.BackColor = System.Drawing.Color.LightGray
         For Each item As MYFILE.Class in the Collection
         Dim aTextBox As New TextBox()
            aTextBox.Font = New System.Drawing.Font(sFont, Single.Parse(sSizeFont), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CByte(0))
            aTextBox.BackColor = System.Drawing.Color.Yellow
            aTextBox.Location = New System.Drawing.Point(iNewColumnPosition + 5 + intMaxWidthLabel, intVertPos)
            aTextBox.Size = New System.Drawing.Size(intWidthTextBox + 10, intGapHeight)

            'store the biggest width, so that the textboxes can be vertically aligned
            If intWidthTextBox > intMaxWidthText Then
                intMaxWidthText = intWidthTextBox
            End If

            'giving a name to all your object will be the only way 
            'to retrieve them and use them
            'for the purpose of this sample, the name can be the 
            'same for all textboxes.
            aTextBox.Name = item.ParameterName
            'giving the maximun size in caracters for the textbox.
            aTextBox.MaxLength = Integer.Parse(item.ParameterLength)

            toolTip1.SetToolTip(aTextBox, "TIP" & intIndex.ToString)

            'tab have to be ordered
            aTextBox.TabIndex = intIndex
            intIndex += 1
            'Vertical position is to be manage according the 
            'tallest object in the form, in this case the 
            'textbox it self
            intVertPos += intGapHeight
            'adding the textbox to the form
            frm.SuspendLayout()
            aTextBox.SuspendLayout()
            frm.Controls.Add(aTextBox)
         Next

I left a lot of code out but this should give you an idea as to what I am doing

Upvotes: 1

Views: 13523

Answers (2)

Alexxx704
Alexxx704

Reputation: 21

In vb it is

Dim tooltip As New ToolTip(components)
tooltip.SetToolTip(textBox1, "This is a textbox tooltip")

Upvotes: 2

Scott Wylie
Scott Wylie

Reputation: 4735

Sorry this is not in VB.NET but I am pretty sure you can easily convert this. The first line is to set ToolTip control to your form components. The second is how you set a tooltip to a control and give it the related text.

    ToolTip tooltip = new ToolTip(components);
    tooltip.SetToolTip(textBox1, "This is a textbox tooltip");

Upvotes: 1

Related Questions