Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1740
  • Last Modified:

ContextMenuStrip text alignment

So I am trying to right-align the text items on a context menu strip, but the code below doesn't seem to work, the items are still left-aligned.  Is it possible to do this?

Thanks very much.
Dim cm As New ContextMenuStrip

    Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        cm.Items.Clear()
        Dim tsmi As New ToolStripMenuItem("hello")
        tsmi.TextAlign = ContentAlignment.MiddleRight


        Dim tsmi2 As New ToolStripMenuItem("four score and seven years ago")
        tsmi2.TextAlign = ContentAlignment.MiddleRight


        Dim tsmi3 As New ToolStripMenuItem("abc")
        tsmi3.TextAlign = ContentAlignment.MiddleRight

        cm.Items.Add(tsmi)
        cm.Items.Add(tsmi2)
        cm.Items.Add(tsmi3)

    End Sub

    Private Sub Form2_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
        cm.Show(Me, New Point(e.X, e.Y))
    End Sub

Open in new window

0
riceman0
Asked:
riceman0
1 Solution
 
Deepak LakkadCommented:
Hi

Here is the revised code for your requirement

Dim cm As New ContextMenuStrip

    Private Sub MDIMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        cm.Items.Clear()

        Dim tsmi As New ToolStripMenuItem("hello")
        tsmi.RightToLeft = Windows.Forms.RightToLeft.Yes
        tsmi.TextAlign = ContentAlignment.MiddleRight

        Dim tsmi2 As New ToolStripMenuItem("four score and seven years ago")
        tsmi2.RightToLeft = Windows.Forms.RightToLeft.Yes
        tsmi2.TextAlign = ContentAlignment.MiddleRight


        Dim tsmi3 As New ToolStripMenuItem("abc")
        tsmi3.RightToLeft = Windows.Forms.RightToLeft.Yes
        tsmi3.TextAlign = ContentAlignment.MiddleRight

        cm.Items.Add(tsmi)
        cm.Items.Add(tsmi2)
        cm.Items.Add(tsmi3)
    End Sub

Open in new window


- Deepak Lakkad
0
 
riceman0Author Commented:

Thanks, that did it!

You get the points, but maybe you know this too:

I am manually changing the width of the menu strip to match what I am opening it next to.  I turn off AutoSize and set the width to the desired dimension.

The right-alignment of the text seems to be stuck on the original width (which turns out to be the middle of my strip).  Do you have any idea how to force it to align to my manual width?

I've been trying different functions to force it but no luck yet.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now