将字体和工具提示设置为动态创建的contextmenustrip项

时间:2013-03-14 09:04:22

标签: vb.net

几乎没有静态contextmenustrip项我会动态地从数据库创建其他项目,如下所示:

reader = cmd.ExecuteReader()
If reader.HasRows Then Arh_ContextMenuStrip.Items.Add("-", Nothing, Nothing)
While (reader.Read())
    If CStr(reader.GetValue(0)).Length = 17 And Not CStr(reader.GetValue(0)) = mydatabase Then
        Arh_ContextMenuStrip.Items.Add(CStr(reader.GetValue(0)), Nothing, AddressOf myHandler_Click)
        'How to set font "Courier New" for this item?
        'How to set tooltip for this item?
    End If
End While

问题是:我可以和如何设置不同的字体,例如" Courier New"对于此类项目,并为每个此类项目添加工具提示文本。那是因为我需要这些项目的比例字体。

1 个答案:

答案 0 :(得分:1)

试试这个,

While (reader.Read())

     If CStr(reader.GetValue(0)).Length = 17 And Not CStr(reader.GetValue(0)) _
     = mydatabase Then

        'Adding item into a context menu strip will return the same.
        Dim xAddedItem = Arh_ContextMenuStrip.Items.Add(CStr(reader.GetValue(0)), _
        Nothing,AddressOf myHandler_Click)

        'Set Different fonts matching your requirement
        xAddedItem .Font = New Font("Courier New", 14, FontStyle.Bold)
        xAddedItem .ToolTipText = "Hello I'm the tool tip"

    End If

End While