Check List - Get Selected Values into set of Labels (Text) - c#

I will disable the CheckBoxList once a user selects 5 values.
I want to take the 5 selected items out of the CheckBoxList and assign them to 5 different labels.
So far I have this:
string test = "";
string test2 = "";
test += CheckBoxList.SelectedValue[0];
test2 += CheckBoxList.SelectedValue[1];
Label1.Text = test;
Label2.Text = test2;
All that does is get the first character and assign the same value to both labels. How would I iterate through and take each selected value and assign them to the each label?

var labels = new List<string>();
int count = 0;
foreach (ListItem item in CheckBoxList1.Items)
{
if (item.Selected)
labels.Add(item.Value);
}
string mylabel1 = labels.Count > 0 ? labels[0] : string.Empty;
string mylabel2 = labels.Count > 1 ? labels[1] : string.Empty;
string mylabel3 = labels.Count > 2 ? labels[2] : string.Empty;
string mylabel4 = labels.Count > 3 ? labels[3] : string.Empty;
string mylabel5 = labels.Count > 4 ? labels[4] : string.Empty;

Here's a generic code, suitable for 5 or 50 items/labels:
var selected = CheckBoxList.Items.Cast<ListItem>().Where(it => it.Selected)
for (i=0; i < selected.Count(); i++)
{
lb = FindControl("Label" + i);
if(lb != null)
((Label)lb).Text = selected.ElementAt(i).Value;
}
Update
Since you stated you don't have LINQ, you can go like this:
int i = 0;
foreach (var item in CheckBoxList.Items)
{
if (item.Selected)
{
lb = FindControl("Label" + i);
if(lb != null)
((Label)lb).Text = item.Value;
i++;
}
}
Update 2
Keep in mind that both solutions assume that your labels start at Label0. Adjust accordingly. Also, code was adjusted to check if the Label was found.

Related

How to split items in Checkedlistbox having strings like A+B+C?

I have a checkedlistbox in which i am populating items like:
Biology+Physics+Chemistry
English+Urdu+Islamiyat
and so on. Now when i retrieve the values of selected items by splitting them on the basis of '+' sign, it gives me an output like:
Biology
Physics
ChemistryEnglish
Urdu
Islamiyat
Now you can look at the output as all values are right except ChemistryEnglish which have got concatenated. What should i be doing so to make this right? I want the output like this:
Biology
Physics
Chemistry
English
Urdu
Islamiyat
UPDATED
MY CODE IS:
String items = "";
string SQLString = "";
if (this.subjects_listbox.CheckedItems.Count != 0)
{
for (int i = 0; i < this.subjects_listbox.Items.Count; i++)
{
items += this.subjects_listbox.CheckedItems[i].ToString();
}
} //
String[] subNames = items.Split('+');
foreach (var item in subNames)
{
MessageBox.Show(item);
}
Finally i achieved my goal this by doing this:
String items = "";
string SQLString = "";
if (this.subjects_listbox.CheckedItems.Count != 0)
{
for (int i = 0; i < this.subjects_listbox.Items.Count; i++)
{
items += this.subjects_listbox.CheckedItems[i].ToString() + "+";
}
} //
String[] subNames = items.Split('+');
foreach (var item in subNames)
{
MessageBox.Show(item);
}
I think you need to split out the items in the CheckedListBox individually before you do what you are doing with items. Take the following code (assuming myCheckedListBox is the name of your CheckedListBox)
var subNameList = new List<string>();
foreach (var item in myCheckedListBox.Items)
{
foreach (string subName in (item.ToString().Split('+'))
{
subNameList.Add(subName);
}
}
This will result in you having a list of strings at the end in subNameList. You may want to use myCheckedListBox.CheckedItems rather than myCheckedListBox.Items depending on your use case.
I achieved my goal by doing this:
String items = "";
string SQLString = "";
if (this.subjects_listbox.CheckedItems.Count != 0)
{
for (int i = 0; i < this.subjects_listbox.Items.Count; i++)
{
items += this.subjects_listbox.CheckedItems[i].ToString() + "+";
}
}
String[] subNames = items.Split('+');
foreach (var item in subNames)
{
MessageBox.Show(item);
}

How to use variable names that are created in a loop and stored in List<string>?

I have a List object that gets automatically filled with the names of textboxes. Now I'd like to cycle through all these text boxes.
How can I let C# know that the string-names in the List are actually TextBox objects with that string-name?
List<string> txtOppsNames = new List<string>();
for (int i = 1; i < numOpps; i++)
{
txtOppsNames.Add("txtOpp" + i);
}
foreach (var txtName in txtOppsNames)
{
if (txtName.Text != "")
{
// do stuff
}
}
The current code reads txtName as a string. I would like it to read as a TextBox.
Edit - the below code contains the solution for me.
List<string> txtOppsNames = new List<string>();
for (int i = 1; i < numOpps; i++)
{
txtOppsNames.Add("txtOpp" + i);
}
foreach (var txtName in txtOppsNames)
{
TextBox textBox = this.Controls.Find(txtName, true).FirstOrDefault() as TextBox;
if (textBox.Text != "")
{
MessageBox.Show("Thanks Amir Popovich");
}
}
Try this :
List<string> txtOppsNames = new List<string>();
for (int i = 1; i < numOpps; i++)
{
txtOppsNames.Add("txtOpp" + i);
}
foreach (var txtName in txtOppsNames)
{
var cntrl= FindControl(txtName);
if (cntrl!=null && cntrl is TextBox)
// do something with
((TextBox)cntrl)
}
Use Control.ControlCollection.Find:
string textBoxName = "txtOpp1";
TextBox textBox = this.Controls.Find(textBoxName, true).FirstOrDefault() as TextBox;
In your case:
List<string> txtOppsNames = new List<string>();
for (int i = 1; i < numOpps; i++)
{
txtOppsNames.Add("txtOpp" + i);
}
foreach (var txtName in txtOppsNames)
{
var control = this.Controls.Find(txtName, true).FirstOrDefault();
if(control != null && control is TextBox)
{
TextBox textBox = control as TextBox;
if(textBox.Text != string.Empty)
{
//logic
}
}
}
Assuming it is Winform, There seems to be no pretty way to do it.
But you could use Loop through Textboxes.
once you have the text box collection, check the names with your string.

ASP.NET Converting repeater item to the textbox

I'm trying to convert item repeaters to the text boxes and check all text boxes if all of them are empty but i get this error in line 3.
System.InvalidCastException
int check = 0;
foreach (RepeaterItem item in searchResultRepeater.Items)
{
if (item.Controls.Count > 0 && item.Controls[0] is ITextControl)
{
if (((TextBox)item.Controls[0]).Text == "") // Exception here
{
check = 0;
}
else
{
check = 1;
break;
}
}
}
How can i fix this ? Any help would be appriciated.
You can try it
int check = 0;
foreach (RepeaterItem item in searchResultRepeater.Items)
{
TextBox txt = (TextBox)item.FindControl("yourTextBoxName");
if (txt.Text == string.Empty)
{
check = 0;
}
else
{
check = 1;
break;
}
}

Clear contents of specific listview columns?

This is how I currently add info to my listview:
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
int totItems = Seq3.Count - 1;
if (PercentPopTolerance1.Count - 1 > totItems) totItems = PercentPopTolerance1.Count - 1;
for (int i = 0; i <= totItems; i++)
{
ListViewItem lvi = new ListViewItem();
string item1 = "";
string item2 = "";
if (Seq3.Count - 1 >= i) item1 = Seq3[i].ToString();
if (PercentPopTolerance1.Count - 1 >= i) item2 = PercentPopTolerance1[i].ToString();
lvi.SubItems.Add(item1);
lvi.SubItems.Add(item2);
listView2.Items.Add(lvi);
}
}
View of empty listview:
Now how would I clear the contents of any particular column? Say I want to add to column YYMM but before I do this, I want to clear that particular column. How would this be done?
You can clear column values like this
var items = listView1.Items;
foreach (ListViewItem item in items)
{
a.SubItems["YYWW"].Text = "";
}
You should specify a column by its name (a column corresponds to a subitem in a ListViewItem), note that this Name is not ColumnHeader.Name, I mean the corresponding ListViewSubItem.Name:
public void ClearColumn(string colName){
foreach(ListViewItem item in listView1.Items){
var cell = item.SubItems[colName];
if(cell != null) cell.Text = "";
}
}
The following code will work for ColumnHeader.Name passed in instead of ListViewSubItem.Name as the code above does:
public void ClearColumn(string columnHeaderName){
int i = listView1.Columns.IndexOfKey(columnHeaderName);
if(i == -1) return;
foreach(ListViewItem item in listView1.Items){
item.SubItems[i].Text = "";
}
}
You can try the following code to make it work for Text instead of Name:
public void ClearColumn(string colText){
if(listView1.Items.Count == 0) return;
var col = listView1.Columns.Cast<ColumnHeader>()
.Select((x,i)=>new{x,i})
.FirstOrDefault(a=>a.x.Text == colText);
if(col == null) return;
foreach(ListViewItem item in listView1.Items){
item.SubItems[col.i].Text = "";
}
}

C# - Assign values to controls thought loop

I have controls that are named in numeric sequence.
I'd like to assign values those controls using loop.
The code below is the way i'm currently using.
txtSalesInvoiceForm_Qty1.Text = (salesInvoice.ItemQty1 == 0) ? string.Empty : salesInvoice.ItemQty1.ToString();
txtSalesInvoiceForm_Qty2.Text = (salesInvoice.ItemQty2 == 0) ? string.Empty : salesInvoice.ItemQty2.ToString();
txtSalesInvoiceForm_Qty3.Text = (salesInvoice.ItemQty3 == 0) ? string.Empty : salesInvoice.ItemQty3.ToString();
txtSalesInvoiceForm_Qty4.Text = (salesInvoice.ItemQty4 == 0) ? string.Empty : salesInvoice.ItemQty4.ToString();
txtSalesInvoiceForm_Qty5.Text = (salesInvoice.ItemQty5 == 0) ? string.Empty : salesInvoice.ItemQty5.ToString();
txtSalesInvoiceForm_Unit1.Text = salesInvoice.Unit1;
txtSalesInvoiceForm_Unit2.Text = salesInvoice.Unit2;
txtSalesInvoiceForm_Unit3.Text = salesInvoice.Unit3;
txtSalesInvoiceForm_Unit4.Text = salesInvoice.Unit4;
txtSalesInvoiceForm_Unit5.Text = salesInvoice.Unit5;
txtSalesInvoiceForm_Particulars1.Text = salesInvoice.Particulars1;
txtSalesInvoiceForm_Particulars2.Text = salesInvoice.Particulars2;
txtSalesInvoiceForm_Particulars3.Text = salesInvoice.Particulars3;
txtSalesInvoiceForm_Particulars4.Text = salesInvoice.Particulars4;
txtSalesInvoiceForm_Particulars5.Text = salesInvoice.Particulars5;
Is there any way something like this?
int index = 1;
foreach (SalesInvoiceItem item in salesInvoice.SalesInvoiceItems)
{
(txtSalesInvoiceForm_Qty + index.ToString()).Text = Value;
indexer++
}
Control parent = this.pnlParent; // this must be the immediate parent control
int index = 1;
foreach (SalesInvoiceItem item in salesInvoice.SalesInvoiceItems)
{
TextBox tb = parent.FindControl( "txtSalesInvoiceForm_Qty" + index++ ) as TextBox;
tb.Text = Value;
}
The key is FindControl(), which searches the immediate children of a parent. Personally I think this is sloppy code in most cases.
Use an array rather than so many named variables and just index into the array.

Categories

Resources