Finding the wrong UIElement in Grid - wpf

Problem: When I select a check box (in Column '1'), the OnSelection event should find a Label on Column '0' in the same Row, but I am getting:
System.InvalidCastException: 'Unable to cast object of type 'System.Windows.Controls.CheckBox' to type 'System.Windows.Controls.Label'.'
I am initializing the view as such:
Label lbName = new() { Content = "labelName" };
Grid.SetColumn(lbName, 0);
GrWindow.Children.Add(lbName);
CheckBox ckSelection = new();
ckSelection.Checked += OnSelection;
Grid.SetColumn(ckSelection, 1);
GrWindow.Children.Add(ckSelection);
And on the OnSelection method, where the exception is triggered:
string? labelName = GrWindow.Children.
Cast<Label>().
First(e => Grid.GetRow(e) == row && Grid.GetColumn(e) == 0).
Content as string;
In other place in my app, I am using a very similar approach to get a name from Row '0' and works fine.
There is only one children on every row/column = '0', and its a label, I dont know how to debug such situations, since the checkbox is on column='1'.
Thanks in advance.

Replace
.Cast<Label>()
with
.OfType<Label>()
to get only the Labels.
Otherwise you are trying to cast all child elements to Label, which will of course fail for the CheckBox.
An alternative to querying the Children collection might be to assign the associated Label to the Tag property of the CheckBox
ckSelection.Tag = lblName;
and get it back in the event handler by
var checkBox = (CheckBox)sender;
val label = (Label)checkBox.Tag;

Related

XPages get Value selected from combo box

I want to display the selected value from the comboBox into a label.
<xp:label id="label4">
<xp:this.value><![CDATA[#{javascript:var componenta = Contr.getItemValueString("txt_tipcontractcv");
if (componenta == "")
{ return "void";}
if (componenta !="")
{ return "My value is "+componenta}}]]></xp:this.value>
</xp:label>
The label is on a panel, and I did a partial refresh to the respective panel.
My comboBox is binded to a data element.
label4 is always void. Why?
Thank you,
Florin
I changed code into:
var componenta = getComponent("combo").getValue();
if ((componenta == null) || (null == componenta))
{ return "void";}
else if ((componenta != null) || (null != componenta))
{ return "My value is "+componenta}
and now it returns : My value is
It seems that componenta is an empty string. Why?
Try Contr.getValue("txt_tipcontractcv") (assuming that Contr is your datasource).
The reason is the value selected is not sent to the server.
Could you give us the code for combobox, and panel with label. It will give us clear idea for the cause
Are you using partial execution mode?
Is combo box bind to data source value="Contr.txt_tipcontractcv"?
You may also code the label as below, if that suits
<xp:label id="label4" value="Contr.txt_tipcontractcv" />

selectionchanged event is not working

I have two felds in my dialog, one is text field(leftpadding) and another is dropdown with four options(option 1, option 2, option 3, option 4).My requirement is to display/enable the textfiled when the user selects option 3 and option 4 only.
I added listeners(selectionchanged) node to the dropdown field. loadcontent event is firing but selectionchanged is not working fine. Please find below for the code snippet
selectionchanged:
function(field,value){
var layoutoption = "";
layoutoption = field.findParentByType('dialog').form.findField('./footerstyle').getValue();
alert('layoutoption :'+layoutoption);
if(layoutoption =='3' || layoutoption =='4'){
field.findParentByType('dialog').form.findField('./leftPadding').enable(this);
}else {
field.findParentByType('dialog').form.findField('./leftPadding').disable(this);
}}
If you look at the CQ.form.Selection API, you would find that when the selectionchanged event is fired, the following arguments would be passed to the listener.
this : The selection field
value : The raw value of the selected field
isChecked : true / false, used when the selection is of type checkbox.
Thus, you could modify your listener as shown below.
function(field, value) {
var dlg = field.findParentByType('dialog');
// the value holds the value of the selection
if(value =='3' || value =='4'){
// getField() of the dialog class returns the field with the given name
dlg.getField('./leftPadding').show();
}else {
dlg.getField('./leftPadding').hide();
}
}
The getField() method of the CQ.Dialog returns the field with the given name. In case more than one field with the same name exists, it returns an array of those fields. You can then show() or hide() the fields based on your requirements.
EDIT :
CQ 5.4 somehow doesn't remove the entire widget, instead removes only the input field leaving behind the Field Label, Description etc.
In such cases the following function may be used.
function(field, value) {
var dlg = field.findParentByType('dialog');
if(value == '3' || value == '4') {
dlg.getField('./leftPadding').el.parent('.x-form-item').show();
}
else {
dlg.getField('./leftPadding').el.parent('.x-form-item').hide();
}
}

SmartGWT ComboBox default checked items

I am using GWT combobox with Select item (dropdown checkbox)
I want to make some items defaultly checked, but i cant find any solutions..
#Override
protected void success(List<warehouseDTO> t)
{
warehouse_list = t;
for (int i = 0; i < warehouse_list.size(); i++)
{
whl.put(warehouse_list.get(i).getId() + "", warehouse_list.get(i).getName());
}
selectItemMultiplePickList.setValueMap(whl);
selectComboForm.setItems(selectItemMultiplePickList);
}
On new "Article" (thing in warehouse) its good, but on edit i need to have checked by default.
Maybe its posible with setAttribute but cant find list of attributes.
Thanks
You should use the following:
setValues(values);
Here values accepts multiple String values.
Now as you're doing:
whl.put(warehouse_list.get(i).getId() + "", warehouse_list.get(i).getName());
your key for the combobox will be warehouse_list.get(i).getId() and value will be warehouse_list.get(i).getName().
So to show multiple values as selected values, you need to pass multiple warehouse_list.get(i).getId() as values.
For example, if you want show first 3 values as selected, you can do the following:
selectItemMultiplePickList.setValues(
warehouse_list.get(0).getId(),
warehouse_list.get(1).getId(),
warehouse_list.get(2).getId());

Assigning values to array of 'Combo Boxes' in a 'Group Box' using for each loop in c#

I have 10 comboBox in a groupBox
for I just want to display a calculated value in respective comboBox like this say if I set a varible double i=08.00; then on button click cmboBox should display values like this
CB1-08.00
CB2-09.50
CB3-10.00
CB4-10.50
CB5-11.00
CB6-11.50
.... and so on upto CB10 But I am getting output like this
And Code
private void button1_Click(object sender, EventArgs e)
{
double i=08.00;
foreach (var comboBox in groupBox1.Controls.OfType<ComboBox>())
{
comboBox.Text = i.ToString("00.00");
i = i + 0.5;
}
}
Your combobox order is different in the collection so it inserts the numbers randomly. May be you can name your combobox for instance like cmb1,cmb2,cmb3 etc. and if you update your code it will run.
Your controls in the Controls collection are not sorted by their appearance on the form. You will need to find a way to sort them if you need different values in each based on their position.
Foreach loop doesn't give the collection in the order you wanted. The way to go forward is to give a tag id to each combo box, then you can use that to assign a value to them them.
So your first combo box will start with tag id 0, and the last one will have 8,
double val = 08.00;
for (int i = 0; i < groupBox1.Controls.Count; ++i)
{
var combobox = groupBox1.Controls[i] as ComboBox;
int tag = int.Parse(combobox.Tag.ToString());
double value = val + (0.5 * tag);
combobox.Text = value.ToString("00.00");
}
Make sure you tag the cobbo box in the order you wanted them.

DataGridViewComboBoxCell Value Not Updating When Set Programmatically C# Winforms

I've read all the similiar posts about this darn control (DataGridViewComboBoxCell) and setting it's value programmatically but implementing all the suggestions hasn't worked. I could probably change the UI to get around this problem but I don't like to be beat!
private void PopulateAreaForRoleAssociation()
{
// If businessRoleList is null then no data has been bound to the dgv so return
if (businessRoleList == null)
return;
// Ensure businessArea repository is instantiated
if (businessAreaRepository == null)
businessAreaRepository = new BusinessAreaRespository();
// Get a reference to the combobox column of the dgv
DataGridViewComboBoxColumn comboBoxBusinessAreaColumn = (DataGridViewComboBoxColumn)dgvBusinessRole.Columns["BusinessArea"];
// Set Datasource properties to fill combobox
comboBoxBusinessAreaColumn.DisplayMember = "Name";
comboBoxBusinessAreaColumn.ValueMember = "Id";
comboBoxBusinessAreaColumn.ValueType = typeof(Guid);
// Fill combobox with businessarea objects from list out of repository
comboBoxBusinessAreaColumn.DataSource = businessAreaRepository.GetAll();
// loop through the businessRoles list which the dgv is bound to and get out each dgv row based upon the current id in the loop
businessRoleList.Cast<BusinessRole>().ToList().ForEach(delegate(BusinessRole currentRole)
{
DataGridViewRow currentRowForRole = dgvBusinessRole.Rows.Cast<DataGridViewRow>().ToList().Find(row => ((BusinessRole)row.DataBoundItem).Id == currentRole.Id);
// Get a reference to the comboBox cell in the current row
DataGridViewComboBoxCell comboBoxCell = (DataGridViewComboBoxCell)currentRowForRole.Cells[2];
// Not sure if this is necessary since these properties should be inherited from the combobox column properties
comboBoxCell.DisplayMember = "Name";
comboBoxCell.ValueMember = "Id";
comboBoxCell.ValueType = typeof(Guid);
// Get the business area for the current business role
BusinessArea currentAreaForRole = businessAreaRepository.FetchByRoleId(currentRole.Id);
// if the role has an associated area then set the value of the cell to be the appropriate item in the combobox
// and update the cell value
if (currentAreaForRole != null)
{
foreach (BusinessArea area in comboBoxCell.Items)
{
if (currentAreaForRole.Id == area.Id)
{
comboBoxCell.Value = area.Id;
dgvBusinessRole.UpdateCellValue(2, comboBoxCell.RowIndex);
}
}
}
});
}
The dgv is first bound to a binding list holding BusinessRole objects, then the combobox column is bound to a basic list of BusinessArea objects that come out of a repository class. I then loop through the bindinglist and pull out the row of the dgv that is bound to the current item in the bindinglist loop.
With that row I make a database call to see if the BusinessRole entity is associated with a BusinessArea entity. If it is then I want to select the item in the combobox column that holds the BusinessAreas.
The problem is that when the grid is loaded, all the data is there and the comboboxes are populated with a list of available areas, however any values that are set are not displayed. The code that sets the value is definately getting hit and the value I am setting definately exists in the list.
There are no data errors, nothing. It's just refusing to update the UI with the value I programmatically set.
Any help would be greatly appreciated as always.
Thanks
This code works for my first combo box I have in my row with the following code, but I try it with the next one in the row and it doesn't work. I could use the help with the rest I have 3 more to do. I do set the combo boxes on a second form from this form with the same code as is on the last line of the try block but using the cell information instead of the dataset
try
{
string strQuery = "select fundCode from vwDefaultItems where IncomeType = '" + stIncome + "'";
SqlDataAdapter daDefault = new SqlDataAdapter(strQuery, conn);
DataSet dsDefault = new DataSet();
daDefault.Fill(dsDefault);
strDefFund = dsDefault.Tables[0].Rows[0].ItemArray[0].ToString();
dgvCheckEntry.Rows[curRow].Cells[7].Value = dsDefault.Tables[0].Rows[0].ItemArray[0].ToString();
}
catch (Exception eq)
{
}

Resources