Sammie
Sammie

Reputation: 11

In Visual Basic (Windows Forms) how can I add a label to a MenuStrip?

I created a Windows Form in Visual Basic which contains a MenuStrip. I need to add a label in that MenuStrip which should display the current user's login details and domain info but for some reason I can't drag and drop a label in that MenuStrip. Any help would be highly appreciated!

I went through VB tutorials on how to add labels in Windows forms. I also tried adding different objects such as 'buttons' but still did not work I'm hoping to get an answer on how to add a label to a Windows form that contains a label which will display the current user details

Upvotes: 0

Views: 201

Answers (2)

Sammie
Sammie

Reputation: 11

I found this set of instructions that helped resolve this issue

  1. Add a ToolStripMenuItem to your form where you want the label.
  2. In the Properties window, change the Text property to "Find" and the Name property to an appropriate value, e.g. "findToolStripLabel".
  3. Click the 'Show All Files' button at the top of the Solution Explorer.
  4. Expand the node for the form in the Solution Explorer.
  5. Double-click the Designer.vb file for the form to open the designer code file.
  6. Change references to the ToolStripMenuItem type for the item added above to ToolStripLabel

Upvotes: 0

jmcilhinney
jmcilhinney

Reputation: 54417

I just added this class to a project:

Imports System.Windows.Forms.Design

<ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.MenuStrip)>
Public Class ToolStripLabel
    Inherits ToolStripControlHost

    Public ReadOnly Property Label As Label
        Get
            Return DirectCast(Control, Label)
        End Get
    End Property

    Public Sub New()
        MyBase.New(New Label)
    End Sub

End Class

After building, when I clicked the drop-down to add a new item to a MenuStrip, there was a Label option in the list, along with the usual MenuItem, ComboBox and TextBox. After adding one and opening the Properties window, I was able to expand the Label control and configure it like any other Label.

Upvotes: 0

Related Questions