2013-04-01 112 views
1

我有一个完全在代码中创建的子窗体。 我想将工具提示添加到窗体上的文本框控件。我知道如何快速设置工具提示,但无法找到将工具提示控件添加到表单中的方法。在google上找到的所有匹配都是指从设计器工具箱中拖动控件。动态添加工具提示控件

我需要做这样的事情:

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

这不起作用

我尝试添加子处理from.load事件(与poitn到子处理程序)在设计时间,但在将工具提示添加到个人动态控件时无法通过错误“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 

我留下了很多的代码了,但这个应该给你一个想法,以我在做什么

+0

你可以多一点点向前与您提供的代码示例来了..?请显示你正在创建子窗体的位置我认为这可能有所帮助..我们无法判断“frm”是“父窗体还是子窗体” – MethodMan

回答

1

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

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

是Scott在以下回复中的回答,它回答了我的问题IMO –

2

在VB是

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

我试过了在组件位置添加子窗体(frm),但这是不对的。 Dim tooltip As New ToolTip(frm)。所以这不会做我想找的。 –

+1

@DanRowe ToolTip控件将位于每个表单上,因此您必须在每个表单上都有。带有ToolTip控件的基本表单将很好地开始继承您的所有子表单。如果你不想这样做,你可以传递一个工具提示控制给你的每个子窗体,但是你仍然必须有类似的代码来初始化你的工具提示控件。它只能使用表单的组件。 –

+0

好的 - 所以如果我从一个没有父窗体的代码模块创建这个表单,我将无法动态添加工具提示控件? –