This is a migrated thread and some comments may be shown as answers.

menu item on the right

15 Answers 35 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Andre
Top achievements
Rank 1
Andre asked on 29 Mar 2018, 12:16 PM

how can i align the menu item on the right?
i have inserted a menu item with properties stretch horizontallyl  = true.
all other with stretch horizontallyl  = false

thanks 

andré

15 Answers, 1 is accepted

Sort by
0
Accepted
Dimitar
Telerik team
answered on 30 Mar 2018, 06:35 AM
Hello Andre,

The items are placed in a WrapLayoutPanel and this is why you cannot just align one of the items to the right. To achieve this you can set the margin when the form size is changed. Here is an example:
private void RadForm1_SizeChanged(object sender, EventArgs e)
{
    int itemSize = 0;
    foreach (var item in radMenu1.Items)
    {
        itemSize += (int)MeasurementControl.ThreadInstance.GetDesiredSize(item, new SizeF(float.MaxValue, float.MaxValue)).Width;
    }
    int leftpadding = (radMenu1.Width - itemSize);        
    radMenuItem3.Margin = new Padding(leftpadding, 0, 0, 0);
}

I hope this will be useful. Let me know if you have additional questions.

Regards,
Dimitar
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Andre
Top achievements
Rank 1
answered on 31 Mar 2018, 07:22 AM

hi Dimitar

thank you.

This solved my problem. !

andré

0
Brandon
Top achievements
Rank 1
answered on 21 Jan 2019, 07:09 PM
This does not work for me unless the RadMenuItem's AutoSize property is set to false.  Also with AutoSize being false, I noticed that even with TextAlignment set to MiddleCenter, the text stays aligned to the TopLeft.
0
Brandon
Top achievements
Rank 1
answered on 21 Jan 2019, 07:12 PM
Here is an image showing what I mentioned.  Couldn't figure out how to modify my previous comment.
0
Dimitar
Telerik team
answered on 22 Jan 2019, 11:53 AM
Hello Brandon,

The new Margin appears to be taken into consideration now. You need to clear it before measuring the item. Here is the updated code:
Padding zeroPadding = new Padding(0);
private void RadForm1_SizeChanged(object sender, EventArgs e)
{
    int itemSize = 0;
 
    foreach (var item in radMenu1.Items)
    {
        item.Margin = zeroPadding;
        itemSize += (int)MeasurementControl.ThreadInstance.GetDesiredSize(item, new SizeF(float.PositiveInfinity, float.PositiveInfinity)).Width;
    }
    int leftpadding = (radMenu1.Width - itemSize);
    radMenuItem3.Margin = new Padding(leftpadding - 3, 0, 0, 0);
}

Let me know how this works for you.

Regards,
Dimitar
Progress Telerik
Get quickly and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Brandon
Top achievements
Rank 1
answered on 22 Jan 2019, 01:27 PM

That works - ish.  I have 3 menu items to move to the right.  When AutoSize is set to true, I only need to set the padding for the first of the 3, and it'll move the other two to the right of itself along with it.  This is great and all, but it doesn't update when the text on the menu items have changed.  I got around this by capturing the text-changed event of each of the menu items.  Great, now that works.  There's still an issue.  One of the menu items is invisible until data is loaded.  Then it's made visible and its text is updated.  (The form is in it's default state of Maximized) The padding doesn't work on this update for some reason.  If I pull the form out of maximized mode, it scales just fine.  Even when I put it back into maximized it works.  It's just the initial state of being maximized and the menu item made visible/text changed that it doesn't work.

 

Do I need to capture when the visibility of the menu item is changed as well?  If so, how?

 

I have provided a screenshot for 

1: No data loaded

2: Data loaded, text updated, visibility changed.

0
Dimitar
Telerik team
answered on 22 Jan 2019, 02:33 PM
Hello Brandon,

The margin needs to be updated in all these cases manually (or when other properties that affect the layout are changed). You can use the PropertyChanged event to handle the Text and the Visibility. For example:
public RadForm1()
{
    InitializeComponent();
     
   
    foreach (var item in radMenu1.Items)
    {
        item.PropertyChanged += Item_PropertyChanged;
    }
}
 
private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "Visibility" ||  e.PropertyName == "Text")
    {
        //update margin
    }
}

Should you have any other questions do not hesitate to ask.

Regards,
Dimitar
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Brandon
Top achievements
Rank 1
answered on 22 Jan 2019, 08:42 PM

So now we're going down a rabbit hole of concurrent problems.  I capture the Visibility change event for the menu items and it just keeps hitting it over and over and over, non-stop until an error is thrown for an overflow.  I verified there is nothing else changing the visibility of the menuitem in question.

Private Sub MenuPosUpdateTrigger(sender As Object, e As PropertyChangedEventArgs) Handles mnuLocation.PropertyChanged, mnuAccount.PropertyChanged, mnuSetting.PropertyChanged
 
    If TypeOf sender Is RadMenuItem Then
        If e.PropertyName = "Visibility" Or e.PropertyName = "Text" Then
            UpdateMenuPos()
        End If
    End If
 
End Sub
 
Public Sub UpdateMenuPos()
    Dim itemSize As Integer = 0
    Dim ZeroPadding As New Padding(0, 0, 0, 0)
    For Each item As RadMenuItem In mnuMenu.Items
 
        item.Margin = ZeroPadding
        Dim Sz As Integer = MeasurementControl.ThreadInstance.GetDesiredSize(item, New SizeF(Single.MaxValue, Single.MaxValue)).Width
        itemSize += Sz
 
    Next
 
    Dim LeftPadding As Integer = (mnuMenu.Width - itemSize)
    mnuLocation.Margin = New Padding(LeftPadding, 0, 0, 0)
End Sub
0
Dimitar
Telerik team
answered on 23 Jan 2019, 09:41 AM
Hi Brandon,

I was able to reproduce this, the issue is that I am measuring hidden items as well. This is not necessary and you can avoid it like this:
Private Sub UpdateMargin()
    Dim itemSize As Integer = 0
 
    For Each item In radMenu1.Items
        item.Margin = zeroPadding
        If item.Visibility <> ElementVisibility.Visible Then
            Continue For
        End If
        itemSize += CInt(MeasurementControl.ThreadInstance.GetDesiredSize(item, New SizeF(Single.PositiveInfinity, Single.PositiveInfinity)).Width)
    Next item
    Dim leftpadding As Integer = (radMenu1.Width - itemSize)
    radMenuItem3.Margin = New Padding(leftpadding - 3, 0, 0, 0)
End Sub

Should you have any other questions do not hesitate to ask.

Regards,
Dimitar
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Brandon
Top achievements
Rank 1
answered on 23 Jan 2019, 04:49 PM
Thank you for your solution.  That made a difference.  The problem I face now is that since it doesn't Calculate the size of the mnuLocation item until it's visible, it ends up looking like the attached picture 1 until it becomes visible in the attached picture 2.
0
Dimitar
Telerik team
answered on 24 Jan 2019, 11:57 AM
Hi Brandon,

How are you hiding the item? You need to set its visibility to collapsed so its size is not taken into account: 
radMenuItem1.Visibility = ElementVisibility.Collapsed;

Should you have any other questions do not hesitate to ask.

Regards,
Dimitar
Progress Telerik
Get quickly onboard and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Brandon
Top achievements
Rank 1
answered on 31 Jan 2019, 10:47 PM
That solved my problem until today when I changed themes.  If I change the font size at all, the proper positioning stops.  It's set to Segoe UI at size 8, Regular.  If I change it to anything higher, the last two menuitems end up on a new row.
0
Brandon
Top achievements
Rank 1
answered on 31 Jan 2019, 10:55 PM
So I'm guessing it's because the MeasurementControl.ThreadInstance.GetDesiredSize uses the Themed font instead of the active font.  What I don't know is how to adjust it accordingly.
0
Brandon
Top achievements
Rank 1
answered on 31 Jan 2019, 11:05 PM

Fixed it myself by modifying the function to set the Font.

 

Public Sub UpdateMenuPos()
    If Not Me.Visible Then Exit Sub
    Dim itemSize As Integer = 0
    Dim ZeroPadding As New Padding(0, 0, 0, 0)
    Dim F As New Font("Segoe UI", 12, FontStyle.Regular)
 
    For Each item As RadMenuItem In mnuMenu.Items
 
        If item.Visibility <> ElementVisibility.Visible Then
            Continue For
        End If
 
        item.Margin = ZeroPadding
 
        MeasurementControl.ThreadInstance.Font = F
        Dim Sz As Integer = MeasurementControl.ThreadInstance.GetDesiredSize(item, New SizeF(Single.MaxValue, Single.MaxValue)).Width
        itemSize += Sz
 
    Next
 
    Dim LeftPadding As Integer = (mnuMenu.Width - itemSize)
    If mnuLocation.Visibility = ElementVisibility.Collapsed Then
        mnuAccount.Margin = New Padding(LeftPadding, 0, 0, 0)
    ElseIf mnuLocation.Visibility = ElementVisibility.Visible Then
        mnuLocation.Margin = New Padding(LeftPadding, 0, 0, 0)
    End If
End Sub
0
Hristo
Telerik team
answered on 01 Feb 2019, 12:28 PM
Hi Brandon,

Thank you for the update. The themes indeed may define different fonts and it is possible that this will influence how the font sizes will be calculated. As I understand, you managed to find a suitable solution. 

Let us know if you have other questions.

Regards,
Hristo
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Menu
Asked by
Andre
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Andre
Top achievements
Rank 1
Brandon
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or