【问题标题】:Add tooltip control dynamically动态添加工具提示控件
【发布时间】:2013-04-01 20:15:12
【问题描述】:

我有一个完全用代码创建的子表单。 我想向表单上的文本框控件添加工具提示。我知道如何即时设置工具提示,但找不到动态将工具提示控件添加到表单的方法。我在谷歌上找到的所有点击都是指从设计器工具箱中拖动控件。

我需要做类似的事情:

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

这不起作用

我尝试在设计时添加一个 sub 来处理 from.load 事件(使用指向 sub 的处理程序),但在将工具提示添加到 iundividual 动态控件时无法传递错误“未声明 Tooltip1” .

如果我从父表单调用此动态表单并将工具提示控件添加到父表单,我可以将它用于子表单。但是,如果我从一个不在父表单上的例程创建表单,我该怎么做呢?

谢谢

        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

我遗漏了很多代码,但这应该让您了解我在做什么

【问题讨论】:

  • 您能否提供更多关于您提供的代码示例的信息……?请显示您在哪里创建子表单我认为这可能会有所帮助..我们无法判断 frm 是否是 Parent form or Child form

标签: vb.net visual-studio-2010


【解决方案1】:

在vb中是

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

【讨论】:

  • 我尝试在组件位置添加子表单(frm),但这不正确。暗淡工具提示作为新工具提示(frm)。所以这不会做我想要的。
  • @DanRowe ToolTip 控件将位于每个表单上,因此您必须在每个表单上都有它。带有 ToolTip 控件的基本表单将是继承所有子表单的良好开端。如果您不想这样做,您可以将一个工具提示控件传递给您的每个子窗体,但您仍然需要使用类似的代码来初始化您的工具提示控件。它只能采用表单的组件。
  • 好的 - 所以如果我从没有父表单的代码模块创建此表单,我将无法即时添加工具提示控件?
【解决方案2】:

抱歉,这不在 VB.NET 中,但我很确定您可以轻松地转换它。第一行是将 ToolTip 控件设置为表单组件。第二个是如何为控件设置工具提示并为其提供相关文本。

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

【讨论】:

  • 斯科特在以下回复中的评论回答了我的问题 IMO
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-23
  • 1970-01-01
  • 1970-01-01
  • 2022-10-23
  • 2012-12-21
  • 2018-11-17
  • 2017-05-19
相关资源
最近更新 更多