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

Size Property of CommandBar does not Reflect Actual Size of the Control

3 Answers 246 Views
CommandBar
This is a migrated thread and some comments may be shown as answers.
Terry
Top achievements
Rank 1
Terry asked on 25 Jan 2021, 09:09 PM

I have several panels on a form.  I need to dynamically create a CommandBar (vertical in this case) and place it on a panel.  I then need to reposition this panel and resize the one above so that the entire CommandBar is visible.  I would like to set the size and location of these panels based on the size of the CommandBar, but the Size property does not reflect the size required to display all the added buttons.

The CommandBar in the attached image has 5 button, but only three are visible.  If I knew the size of the CommandBar, I could set the Location of the panel containing the CommandBar and the Height of the panel above it, so that all 5 buttons would be visible.

How can I get the information needed to set the Size and Locations of the two panels as needed to make the dynamically created CommandBar visible.


3 Answers, 1 is accepted

Sort by
0
Todor
Telerik team
answered on 28 Jan 2021, 11:20 AM

Hello Terry,

If I understand you correctly, you add a RadCommandBar control dynamically(at runtime) with vertical orientation. It seems you want to set its height to a value so all the buttons added are visible.

To achieve this you can get the desired height of the CommandBarStripElement and set it to the command bar control. I prepared a code sample that replicates the desired behavior:

private void button1_Click(object sender, EventArgs e)
{
    RadCommandBar commandBar = new RadCommandBar();
    commandBar.Dock = DockStyle.Bottom;

    CommandBarRowElement row = new CommandBarRowElement();
    commandBar.Rows.Add(row);

    CommandBarStripElement strip = new CommandBarStripElement();
    row.Strips.Add(strip);

    for (int i = 1; i <= 5; i++)
    {
        CommandBarButton button = new CommandBarButton();
        button.DrawText = true;
        button.DrawImage = false;
        button.AngleTransform = 270;
        button.Text = "Button " + i;                
        strip.Items.Add(button);
    }

    this.radPanel1.Controls.Add(commandBar);

    commandBar.Orientation = Orientation.Vertical;

    // set the height to a larger value so the Strip can be measured correctly
    commandBar.Height = 500;

    // Get the desired height of the strip and assign it to the control
    commandBar.Height = (int)strip.DesiredSize.Height;
}

The result on my side is illustrated in the image below:

I hope this will help. Let me know if you will need further assistance.

Regards,
Todor Vyagov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Terry
Top achievements
Rank 1
answered on 28 Jan 2021, 03:42 PM

When I try this, strip.DesiredSize.Height is 0, so the command bar is not visible at all.  Even without the line to set the height, I had to dock it at the top, because the CommandBar was not visible when docked at the bottom.

 


0
Accepted
Todor
Telerik team
answered on 29 Jan 2021, 03:55 PM

Hello Terry,

In order force the layout of RadCommandBar you can add it as a child to a parent control(shown in the code snippet in my previous post) or invoke LoadElementTree method of the control, which allows layout operations to be executed:

// create command bar and add items
commandBar.Orientation = Orientation.Vertical;
commandBar.LoadElementTree(); // loads element tree and allows layout operations
commandBar.Height = 500; // set the height to a larger value so the Strip can be measured correctly
int desiredHeight = (int)strip.DesiredSize.Height; // in my case this returns 147
// add it to a panel

I am also attaching my test form. Feel free to extend it in a way to achieve your exact requirement.

I hope this helps. Let me know if you have other questions. 

Regards,
Todor Vyagov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
CommandBar
Asked by
Terry
Top achievements
Rank 1
Answers by
Todor
Telerik team
Terry
Top achievements
Rank 1
Share this question
or