I have check boxes that are created by code depending on a database result.
var checkbox = new CheckBox(this);
var checkBoxes = new CheckBox[0];
//in a for loop I have
checkBox.LayoutParameters = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
line view.AddView(checkBox);
Array.Resize(ref checkBoxes, i + 1);
CheckBoxes[i] = checkBox;
All works, I get my checkBoxes that contain a all the name tables from the database so I have 4 tables I get 4 checkboxes
The issue is I'm not sure on the best way to check if its checked.
Any help would be appreciated
Tried if statement to check if the checkBox.Checked and Al's tried
checking if it's TRUE, however seams the if statement has no affect
Have you assigned value true for your checkBox's property Checked?
If you didn't do this,then the value of the checkBox.Checked will be the default value false.
You can refer to the following code,I tested on my side, it works properly.
var layout_main = new LinearLayout(this);
LinearLayout.LayoutParams p2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MatchParent,
LinearLayout.LayoutParams.WrapContent
);
layout_main.LayoutParameters = p2;
layout_main.Orientation = Orientation.Vertical;
LinearLayout.LayoutParams param_btn = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.WrapContent);
param_btn.SetMargins(0,500,0,0);
var myBtn = new Button(this);
myBtn.LayoutParameters = param_btn;
myBtn.Text = "chang text";
myBtn.Gravity = GravityFlags.Center;
LinearLayout.LayoutParams param_checkbox = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.WrapContent);
param_btn.SetMargins(0, 100, 0, 0);
var checkBox = new CheckBox(this);
checkBox.LayoutParameters = param_checkbox;
checkBox.Gravity = GravityFlags.Center;
checkBox.Text = "test";
checkBox.Checked = true;
myBtn.Click += delegate {
if (checkBox.Checked)
{
Toast.MakeText(this, "---> checkBox's checked = " + checkBox.Checked, ToastLength.Long).Show();
}
};
layout_main.AddView(myBtn);
layout_main.AddView(checkBox);
Related
i am using code behind visual state managers to give selected labels a background color, but this doesnt work, any idea why?
var frameStackLayoutX = new StackLayout
{
Spacing = 5
};
var vsg = new VisualStateGroup() { Name = "CommonStates" };
var vs = new VisualState { Name = "Selected" };
vs.Setters.Add(new Setter
{
TargetName = "Selected",
Property = Label.BackgroundColorProperty,
Value = Colors.Red
});
vsg.States.Add(vs);
VisualStateManager.GetVisualStateGroups(frameStackLayoutX).Add(vsg);
var LabelName = new Label();
LabelName.Text = "Jhon Doe"
LabelName .WidthRequest = 100;
LabelName .Padding = new Thickness(10, 0, 0, 0);
frameStackLayoutX.Add(LabelName );
return frameStackLayoutX;
This is inside a grid, wherei use _lines.SelectedItem = pressedItem; to make sure that I can click on my label.
I would like to use different background colors for the elements in the drop-down list. I generated the drop-down as follow:
var list = new System.Collections.Generic.List<string>();
list.Add("okay");
list.Add("delete");
list.Add("else (Comment)");
var myDDL = string.Join(",", list.ToArray());
var celldd = (Excel.Range)mysheet.Cells[row_counter, 10];
celldd.Validation.Delete();
try
{
int type = celldd.Validation.Type;
Console.WriteLine("The validation have already been added");
}
catch (System.Runtime.InteropServices.COMException)
{
celldd.Validation.Add(
Excel.XlDVType.xlValidateList,
Excel.XlDVAlertStyle.xlValidAlertInformation,
Excel.XlFormatConditionOperator.xlBetween,
myDDL, misValue);
}
celldd.Validation.InCellDropdown = true;
celldd.Validation.IgnoreBlank = true;
celldd.Locked = false;
celldd.Value2 = "Choose";
celldd.Interior.Color = ColorTranslator.ToOle(Color.Plum);
Is this possible? Until now, just the default value has a background color.
I am creating combobox dynamically in winforms
ComboBox ddCntrl = new ComboBox();
ddCntrl.Width = 100;
ddCntrl.Name="dd_" + tpObj.RowColId;
ddCntrl.DropDownStyle = ComboBoxStyle.DropDownList;
Dictionary<int, string> DC = new Dictionary<int, string>();
DC[-1] = "N/A";
DC[0] = "Y";
DC[1] = "N";
ddCntrl.DataSource = new BindingSource(DC,null);
ddCntrl.DisplayMember = "Value";
ddCntrl.ValueMember = "Key";
ddCntrl.SelectedIndex = ddCntrl.Items.IndexOf("N");
TableLayoutPanel.Controls.Add(ddCntrl, 1, 1);
I tried couple of option to set the selected value nothing is working
I tried below options to set selected value
ddCntrl.SelectedValue ="N";
ddCntrl.SelectedIndex = ddCntrl.FindStringExact("N")
You will need to change some things. First, if you are using this code in the constructor, you will need to move it to Load or Shown event.
And set the index after add the comboBox to the panel. ddCntrl.FindStringExact("N") should works ok:
ComboBox ddCntrl = new ComboBox();
ddCntrl.Width = 100;
ddCntrl.Name = "dd_";
ddCntrl.DropDownStyle = ComboBoxStyle.DropDownList;
Dictionary<int, string> DC = new Dictionary<int, string>();
DC[-1] = "N/A";
DC[0] = "Y";
DC[1] = "N";
ddCntrl.DataSource = new BindingSource(DC, null);
ddCntrl.DisplayMember = "Value";
ddCntrl.ValueMember = "Key";
tableLayoutPanel.Controls.Add(ddCntrl, 1, 1);
ddCntrl.SelectedIndex = ddCntrl.FindStringExact("N");
Since you are binding to a Dictionary you should set selected item as follows:ddCntrl.SelectedItem = DC[1];
If you would like to set depending on display value (which i really do not suggest) you have to find it in DC and then set it to ddlCntrl
I am trying to add a rows to a RadGridView when a use click a button on the form (ie. "Add".)
When the form load I add columns to the RadGridView and make all of them all ReadOnly except one .
When a user click the "Add" button, I want to add a row to this RadGridView. Basically, a user types a UPC code, then I read data from the database and add new row in a grid view with the item name, item price.
Here is what I did to create the columns
private void Register_Load(object sender, EventArgs e) {
GridViewTextBoxColumn UPC = new GridViewTextBoxColumn();
UPC.Name = "UPC";
UPC.HeaderText = "UPC";
UPC.FieldName = "UPC";
UPC.MaxLength = 50;
UPC.TextAlignment = ContentAlignment.BottomRight;
radGridView1.MasterTemplate.Columns.Add(UPC);
radGridView1.Columns["UPC"].Width = 120;
radGridView1.Columns["UPC"].ReadOnly = true;
GridViewTextBoxColumn ItemName = new GridViewTextBoxColumn();
ItemName.Name = "Item Name";
ItemName.HeaderText = "Item Name";
ItemName.FieldName = "ItemName";
ItemName.MaxLength = 100;
ItemName.TextAlignment = ContentAlignment.BottomRight;
radGridView1.MasterTemplate.Columns.Add(ItemName);
radGridView1.Columns["Item Name"].Width = 210;
radGridView1.Columns["Item Name"].ReadOnly = true;
GridViewDecimalColumn QtyColumn = new GridViewDecimalColumn();
QtyColumn.Name = "Qty";
QtyColumn.HeaderText = "Quantity";
QtyColumn.FieldName = "Qty";
QtyColumn.DecimalPlaces = 1;
radGridView1.MasterTemplate.Columns.Add(QtyColumn);
radGridView1.Columns["Qty"].Width = 75;
GridViewMaskBoxColumn PriceColumn = new GridViewMaskBoxColumn();
PriceColumn.Name = "Unit Price";
PriceColumn.FieldName = "UnitPrice";
PriceColumn.HeaderText = "Unit Price";
PriceColumn.MaskType = MaskType.Numeric;
PriceColumn.Mask = "C";
PriceColumn.TextAlignment = ContentAlignment.BottomRight;
PriceColumn.FormatString = "{0:C}";
PriceColumn.DataType = typeof(decimal);
radGridView1.MasterTemplate.Columns.Add(PriceColumn);
radGridView1.Columns["Unit Price"].Width = 75;
radGridView1.Columns["Unit Price"].ReadOnly = true;
GridViewMaskBoxColumn TotalColumn = new GridViewMaskBoxColumn();
TotalColumn.Name = "Total Price";
TotalColumn.FieldName = "TotalPrice";
TotalColumn.HeaderText = "Total Price";
TotalColumn.MaskType = MaskType.Numeric;
TotalColumn.Mask = "C";
TotalColumn.TextAlignment = ContentAlignment.BottomRight;
TotalColumn.FormatString = "{0:C}";
TotalColumn.DataType = typeof(decimal);
radGridView1.MasterTemplate.Columns.Add(TotalColumn);
radGridView1.Columns["Total Price"].Width = 75;
radGridView1.Columns["Total Price"].ReadOnly = true;
}
To add the row Here is what I am doing "when a user click the 'Add' button)
private void ButtonAdd_Click(object sender, EventArgs e) {
string UPC = InputUPC.Text.Trim();
if (UPC.Length < 3) {
return;
}
string sql = " SELECT p.productName, p.price "
+ " FROM products AS p "
+ " WHERE p.productUPC = #upc ";
var parms = new List<MySqlParameter>();
parms.Add(new MySqlParameter("#upc", UPC));
var db = new dbConnetion();
foreach (var i in db.getData(sql, parms, r =>
new ProductsTable() {
_productName = r["productName"].ToString(),
_price = Convert.ToDouble(r["price"])
}
)
) {
//radGridView1.Rows[0].Cells[0].Value = 4.3;
radGridView1.Rows[0].Cells["UPC"].Value = UPC;
radGridView1.Rows[0].Cells["Item Name"].Value = i._productName;
radGridView1.Rows[0].Cells["Qty"].Value = "1";
radGridView1.Rows[0].Cells["Unit Price"].Value = i._price;
radGridView1.Rows[0].Cells["Total Price"].Value = (Convert.ToDouble(i._price) * Convert.ToInt32(radGridView1.Rows[0].Cells["Qty"].Value)).ToString();
}
}
But the add row is giving me an error
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
How can I correctly add rows to the RadGridView when the "Add" button is clicked.
ButtonAdd_Click is wrong because the foreach is always trying to set row 0, which doesn't exist.
You don't specify what GridView you are using, so I can't give specifics, but typically you should be able to call Rows.Add(...).
When the add button is clicked second time, there is supposed to be two lines of data rows in the GridView ( the first row is the first click and the second row is the newly added data). However there is only one data row.
List<DonationReceivedItem> drList = new List<DonationReceivedItem>();
protected void lbnAdd_Click(object sender, EventArgs e)
{
DonationReceivedItem temp = new DonationReceivedItem();
temp.donation = dID;
temp.productVariant = gvSelectVairant.SelectedRow.Cells[1].Text;
temp.productQuantity = tbQuantity.Text;
temp.isDistributed = "0";
drList.Add(temp);
gvNonExpired.DataSource = drList;
gvNonExpired.DataBind();
}
Try changing the following code:
DonationReceivedItem temp = new DonationReceivedItem();
temp.donation = dID;
temp.productVariant = gvSelectVairant.SelectedRow.Cells[1].Text;
temp.productQuantity = tbQuantity.Text;
temp.isDistributed = "0";
drList.Add(temp);
gvNonExpired.DataSource = drList;
gvNonExpired.DataBind();
to:
DonationReceivedItem temp = new DonationReceivedItem();
drList = gvNonExpired.DataSource;
temp.donation = dID;
temp.productVariant = gvSelectVairant.SelectedRow.Cells[1].Text;
temp.productQuantity = tbQuantity.Text;
temp.isDistributed = "0";
drList.Add(temp);
gvNonExpired.DataSource = drList;
gvNonExpired.DataBind();
See if that makes a difference :)
Because you are creating a new list you are wiping the previous data. First instantiate the list with the old data, then add the new data.