How to get Selected Listbox Items using devexpress in winforms app? - winforms

I am trying to get selected item text. I used this below code
MessageBox.Show(listBoxColumnHeaders.SelectedItems);
Output
Devexpress.XtraEditors.BaseListboxControl+SelectedItemCollection
But my text is Country
Update
I add listbox items from another class. That class called FilterColumnHeader using below code
FilterControl fc = Application.OpenForms.OfType<FilterControl>().SingleOrDefault();
List<FilterColumnHeader> headers = new List<FilterColumnHeader>();
while (rd.Read())
{
headers.Add(new FilterColumnHeader { typeOfHeader = rd["type"].ToString(), columnHeadersName = rd["AsHeading"].ToString() });
}
fc.listBoxColumnHeaders.DisplayMember = "columnHeadersName";
fc.listBoxColumnHeaders.ValueMember = "typeOfHeader";
fc.listBoxColumnHeaders.DataSource = headers;
Now When I try to print using this below code,
MessageBox.Show(""+ listBoxColumnHeaders.SelectedItems[0].ToString());
It is showing in message box like below
`ProjectName.FilterColumnHeader`

The SelectedItems property returns a collection of selected objects. All you need to do is to cast the required object to your type:
var filterColumnHeader = (FilterColumnHeader)listBoxControl.SelectedItems[0];

I don't know if you found an answer but this works for me :
StringBuilder list = new StringBuilder();
foreach(var item in listBoxColumnHeaders.SelectedItems)
{
list.AppendLine(item as string);
}
MessageBox.Show(list.ToString());

Related

Grid with LitTemplate not updating checkbox consistently

We are updating our Grid forms to use LitTemplates for rendering. However we have run into an issue with a checkbox state not being updated even though the underlying data for the state has been updated. The following code demonstrates the issue:
HorizontalLayout hlTest = new HorizontalLayout();
List<String> selected = new ArrayList<>();
Grid<String> testGrid =new Grid<>();
testGrid.setWidth("250px");
testGrid.setHeight("250px");
List<String> dataList = new ArrayList<>(List.of("AAAAA", "BBBBB", "CCCCC", "DDDDD", "EEEEE"));
ListDataProvider<String> dataProvider = new ListDataProvider<>(dataList);
testGrid.setItems(dataProvider);
String template = """
<vaadin-checkbox #click="${handleCheckClick}" ?checked="${item.rowSelected}"></vaadin-checkbox>
<div>${item.text}</div>
""";
LitRenderer<String> lit = LitRenderer.of(template);
lit.withProperty("rowSelected", item -> {
boolean rc = selected.contains(item);
System.out.println("Item selected: " + item + " => " + rc);
return rc;
});
lit.withProperty("text",item -> item);
lit.withFunction("handleCheckClick", (item) -> {
if(selected.contains(item))
selected.remove(item);
else
selected.add(item);
});
testGrid.addColumn(lit);
Button bSelect = new Button("Select");
bSelect.addClickListener(buttonClickEvent -> {
if(selected.size() > 0)
{
dataList.clear();
dataList.addAll(selected);
selected.clear();
dataProvider.refreshAll();
}
});
hlTest.add(testGrid, bSelect);
With the given grid, click a few checkboxes and click 'Select'. The list is reduced to the selected rows, but the checkboxes are still ticked even though 'selected' is empty. The logging within 'rowSelected' is correctly returning the state.
If I remove the column and re-add to grid, then all is displayed correctly. Also, if I select all items in the grid then the select also works correctly.
Currently using Java17 and Vaadin 22.0.6 (have also tried 23.0.0 beta3).
Thanks.
I think you need to call dataProvider refresh when you update
lit.withFunction("handleCheckClick", (item) -> {
if(selected.contains(item))
selected.remove(item);
else
selected.add(item);
testGrid().getDataProvider().refreshAll();
});

Setting values to each comboBox item

populate combobox code:
ComboBox getCategoryComboBox = new ComboBox();
searchOptionForm.add(getCategoryComboBox);
getCategoryComboBox.setUIID("TextField");
getCategoryComboBox.addItem("Choose Category");
for (Map<String, Object> entry : alacc.responseCategory) {
String categoryName = (String) entry.get("name");
String categoryId = (String) entry.get("id");//how to set this to combobox item
getCategoryComboBox.addItem(categoryName);
}
categoryId is taken from for loop above, how to set it in each combobox items? I need to get the categoryId of each selected combobox item, how can i get this?
You have several ways to do this.
One way is to just do:
getCategoryComboBox.addItem(entry);
Which would provide you the full entry on getSelectedItem() effectively solving that problem.
To make the name render properly though you would need to do this:
cb.setRenderer(new DefaultListCellRenderer<Object>() {
public Component getCellRendererComponent(Component list, Object model, Object value, int index, boolean isSelected) {
if(value instanceof Map) {
value = ((Map)value).get("name");
}
return super.getCellRendererComponent(list, model, value, index, isSelected);
}
});
Notice you will also need to define the theme constant otherPopupRendererBool to false for this to work properly.

Updating Griffon JavaFX combo boxes

I'm trying to update the content of a combo box (using Griffon 1.2.0, with the JavaFX plugin).
My model:
class MyModel {
List monthList = FXCollections.observableList([new DateMidnight()])
def convertDate = [
fromString: { String s ->
return new DateMidnight(DateTimeFormat.forPattern("yyyy-MM").parseDateTime(s))
},
toString: { DateMidnight d ->
return "2011-10"
}
] as StringConverter
}
My view contains:
comboBox(items: (model.monthList), converter: model.convertDate)
Now I have a controller action which gets invoked when they push a button:
def load = {
execInsideUIAsync {
def months = myService.buildMonthList()
model.monthList.addAll(months)
}
}
The problem is that the combo box content never changes. Can anyone help me understand what I'm missing?
There's no documentation on ComboBox yet http://groovyfx.org/docs/guide/single.html#choiceBoxComboBox
Also, have I implemented the converter correctly?
The problem is that GroovyFX.comboBox creates a new List instead of using the one you pass as argument for items: This problem occurs with tableView as well. A temporary workaround would be to set the items property directly, like this
comboBox(id: 'combo')
noparent { combo.items = model.monthList }

How do I programmatically create a GUI and create it for my windows form?

As above, how do I create buttons or labels programmatically instead of using the drag and drop function?
The following sample is a code for creating a panel.
public Panel createPanel()
{
Panel p = new Panel
{
BorderStyle = BorderStyle.FixedSingle,
Size = new Size(506, 110),
Name = "Panel"
};
Button button = new Button
{
Text = "Clear",
Name = "Button",
Location = new Point(410, 40)
};
p.Controls.Add(button);
return p;
}
Go to YourForm.Designer.cs and see how it's done.
Remember that they're just object members.

How do I get a context menu to work on a Telerik RadGridView column?

I have the following method which adds a new column to a Telerik RadGridView:
private void CreateNewColumn(FieldDescriptor fd, uint fieldno) {
fieldGrid.Columns.Add(new GridViewDataColumn() {
UniqueName = fd.fieldName,
Header = fd.displayName,
DataMemberBinding = new Binding("Fields[" + fieldno + "]"),
ContextMenu = new ContextMenu() {
Tag = fieldno,
Items = {
new MenuItem() {
Header = "Field Properties",
Command = Commands.FieldProperties,
CommandBindings = { new CommandBinding(Commands.FieldProperties, FieldProperties_Execute) }
},
new MenuItem() {
Header = "Delete Field",
Command = Commands.DeleteField,
CommandBindings = { new CommandBinding(Commands.DeleteField, DeleteField_Execute) }
}
}
}
});
}
The problem I'm having is that the context menu never appears when I right click anywhere on the grid. If I bind the context menu directly to the grid, i.e.
fieldGrid.ContextMenu = new ContextMenu() { ...
then the context menu shows up, but I have no way of determining which column the user right-clicked on. Has anyone gotten context menus to work on individual columns or column headers?
I cannot speak for Telerik's grid, but with the Infragistics grid you would attach the context menu to the grid, and then use the mouse location to determine what the user right clicked on in the grid. The Infragistics grid has some decent helper methods to facilitate the hit testing.
You can check my answer on your forum post:
http://www.telerik.com/community/forums/wpf/gridview/column-contextmenu.aspx

Resources