I am trying to programmatically set the width of the dropdown list when the user clicks the DropDownButton, but my items are getting cut off because I can't seem to find the width of the CheckBox on the list so I can pad the width. How can I get to that information?
TIA.
Mark
1 Answer, 1 is accepted
Hello, Mark,
If I understand your requirement correctly, you are trying to programmatically set the width of the drop down that shows the items in RadCheckedDropDownList. To do so, you should set the DropDownWidth property as follows:
this.radCheckedDropDownList1.CheckedDropDownListElement.DropDownWidth = 250;
I hope this helps. If you have any other questions, do not hesitate to contact me.
Regards,
Nadya | Tech Support Engineer
Progress Telerik
Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.
Yes, I know that, but our data still gets cut off in the list. The list doesn't auto-size to the longest description. So, I have code that does it when we set the DataSource to the dropdown, but I am running into problems with both the DropDownList and the CheckedDropDownList. I have see some examples online on how to calculate the width needed for the dropdownlist, but doesn't always calculate correctly. And for the CheckedDropDownList, you have the extra space for the CheckBoxes in the dropdownlist that I need to account for when calculating the width to set the DropDownList to, but I can't seem to find how to get that from the CheckedDropDown.
Here is an example of our code that we are using.
//- list = a BindingList<CheckedListItem>()
radDropDownList.BeginUpdate();
radDropDownList.ValueMember = "Value";
radDropDownList.DisplayMember = "Display";
radDropDownList.CheckedMember = "Checked";
radDropDownList.ShowCheckAllItems = false;
radDropDownList.DataSource = list;
radDropDownList.EndUpdate();
Graphics grahpics = radDropDownList.CreateGraphics();
if (radDropDownList.Items.Any())
{
string display = radDropDownList.Items.ToList()
.Select(i => i.DisplayValue.ToString().ToUpper().Trim())
.OrderBy(s => s.Length)
.First();
int width = (int)Math.Ceiling(grahpics.MeasureString(display, radDropDownList.Font).Width)
+ (radDropDownList.DefaultItemsCountInDropDown < radDropDownList.Items.Count
? radDropDownList.CheckedDropDownListElement.ListElement.VScrollBar.MinSize.Width
: 0)
+ radDropDownList.Padding.Horizontal;
}
// Only change the dropdownwidth if the new size is > than the current dropdown's width.
if (radDropDownList.Width + radDropDownList.CheckedDropDownListElement.ArrowButtonMinWidth < width)
{
radDropDownList.CheckedDropDownListElement.DropDownWidth = width;
}
Hello, Mark,
Thank you for providing additional details regarding what exactly you need to measure. You can use the following code snippet in VisualListItemFormatting to get the width of the checkbox from the RadCheckedListVisualItem:
private void RadCheckedDropDownList1_VisualListItemFormatting(object sender, VisualItemFormattingEventArgs args)
{
RadCheckedListVisualItem item = args.VisualItem as RadCheckedListVisualItem;
if (item != null && item.CheckBox.DesiredSize.Width != 0)
{
float width= item.CheckBox.DesiredSize.Width;
}
}
Can you give this approach a try and let me know how it works for you.
What I am looking to do is get this information so I can set the Width of the DropDownListElement at the LOAD of the form. I can't wait until the event of the DropDownLists fires, at that time, it is too late.
Hello, Mark,
The check box is added into RadCheckedListVisualItem. The RadCheckedListVisualItems get created when the popup is opened, not earlier. I understand your idea but to get the since of the checkbox, the RadCheckedListVisualItem should be created. I prepared another code snippet which you can call on a button. Click, however, you should firstly open the popup so the visual item gets cteated.
private void radButton1_Click(object sender, EventArgs e)
{
foreach (RadElement item in this.radCheckedDropDownList1.CheckedDropDownListElement.ListElement.ViewElement.Children)
{
RadCheckedListVisualItem visualElement = item as RadCheckedListVisualItem;
if (visualElement != null)
{
float width = visualElement.CheckBox.DesiredSize.Width;
}
}
}
А possible solution that I can suggest is to use a variable with a fixed value for your calculations.
I hope this helps. Please let me know if I can assist you further.
We have an extension method for dropdown controls called "BindList," in which the list that will be set to the DataSource is passed. I am trying to set the width of the dropdown list at this point. However, we do have a class based on RadDropDown that we use in most places, so I will try your solution to see if that will work, so making the change once should work for those controls through out the system. As for the controls that are using RadCheckedDropDownList directly, I will have to come up with a different solution, as I don't want to have to change 100's of locations to do it.
Thanks