C# print number from for loop - c#

I want to display the number say 1 to 100 by selecting a item in drop down list. I mean, if I select 4 times, it should count as 4 and display.
I have tried the code below, but it is not working.
//Method
public void cl()
{
if (Catddl.SelectedIndex != 0)
{
for (int i = 1; i <= 100; i++)
{
Label12.Text = Convert.ToString(i);
}
}
}
//called the method
protected void Catddl_SelectedIndexChanged(object sender, EventArgs e)
{
cl();
}

I worked on your problem and this is the result.It works fine for me.Hope it also work for you.
static int count = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
private void bind()
{
ArrayList ar = new ArrayList();
ar.Add("first");
ar.Add("Second");
ar.Add("Third");
ar.Add("Four");
ar.Add("Five");
ar.Add("Six");
ar.Add("Seven");
DropDownList1.DataSource = ar;
DropDownList1.DataBind();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//string str = DropDownList1.SelectedValue;
if (count == 0)
count = 1;
Label1.Text = count++.ToString();
}
Still if you have any doubt then ask.

If you are trying to count how many times the user selects something from a drop down list, you can do:
int counter = 0;
private void Catddl_SelectedIndexChanged(object sender, EventArgs e)
{
counter++;
Label12.Text = counter.ToString();
}

Related

How to get selected row value from C1FlexGrid

I have C1FlexGrid in my form with multiple rows. I want to select random rows and get those selected row value.
Selection Mode:
this.CliAcctHolderGrid.SelectionMode = C1.Win.C1FlexGrid.SelectionModeEnum.ListBox;
Function Code:
private void Submit(object sender, EventArgs e)
{
List<string> holderIdentificationId = new List<string>();
if (CliAcctHolderGrid.RowSel >= 1)
{
for (int CliAcctHolder = 1; CliAcctHolder <= CliAcctHolderGrid.Row; CliAcctHolder++)
{
C1.Win.C1FlexGrid.Row rowSel = CliAcctHolderGrid.Rows[CliAcctHolder];
holderIdentificationId.Add((string)rowSel["HolderIdentifierId"]);
}
}
}
From the code I have done, I am getting values which I didn't selected. Getting all values from the grid. Can anyone please suggest me where I am making a mistake.
private void Submit(object sender, EventArgs e)
{
List<string> holderIdentificationId = new List<string>();
if ( CliAcctHolderGrid.RowSel >= 1 )
{
foreach(C1.Win.C1FlexGrid.Row dr in CliAcctHolderGrid.Rows.Selected)
{
holderIdentificationId.Add( (string)dr["HolderIdentifierId"] );
}
}
}

Don't let me add in a listbox more items than shown in the combobox

I have a combobox with the items
1
2
3
...
40
,if I chose the value 4 then I should be able to add in my listbox no more than 4 values.This is what i was thinking of but isn't working.
public Form1()
{
InitializeComponent();
}
private void add_Click(object sender, EventArgs e)
{
int allowedItemsCount = 0;
Int32.TryParse(comboBox1.SelectedText, out allowedItemsCount);
int currentItemsCount = listBox1.Items.Count;
if (currentItemsCount < allowedItemsCount)
{
listBox1.Items.Add(textBox1.Text);
}
}
private void delete_Click(object sender, EventArgs e)
{
if (listBox1.SelectedItems.Count != 0)
{
while (listBox1.SelectedIndex != -1)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int x = 0;
Int32.TryParse(comboBox1.SelectedText, out x);
int count = listBox1.Items.Count;
if (count > x)
{
listBox1.Items.Clear();
int difference = count - x;
for (int i = 0; i < difference; i++)
{
listBox1.Items.RemoveAt(listBox1.Items.Count - 1);
}
}
}
}
Here is the full code you asked for but is not working...now the add button is not working.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int x = 0;
Int32.TryParse(comboBox1.SelectedText, out x);
int count = listBox1.Items.Count;
if (count > x)
{
listBox1.Items.Clear();
int difference = count - x;
for(int i = 0 ; i < difference ; i++)
{
listBox1.Items.RemoveAt(listBox1.Items.Count-1);
}
}
}
Update
As per your comment, write this code in your add button click event
int allowedItemsCount = 0;
Int32.TryParse(comboBox1.SelectedText, out allowedItemsCount);
int currentItemsCount = listBox1.Items.Count;
if(currentItemsCount < allowedItemsCount)
{
listBox1.Items.Add(textBox1.Text); // I assume your textbox id is TextBox1
}
For removing extra items:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox1.TrimExcess(Int32.Parse(comboBox1.SelectedText));
//If you control the items on the combo so you know for sure the parse will work, ignore the tryparse
}
And for adding items to the ListBox (and control it does not have to much items:
private int addToListBox(object item)
{
if(listbox1.items.count>=Int32.Parse(comboBox1.SelectedText)) return -1;
listbox1.items.add(item);
return 0;
}
And when you need to add to that listbox use addToListBoxand not listbox1.items.add
This will check if you can add line to listbox1 and if its changed number of line it will delete it.
Adding is made by button.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int x = 0;
Int32.TryParse(comboBox1.SelectedItem.ToString(), out x);
int count = listBox1.Items.Count;
while(count > x){
listBox1.Items.RemoveAt(count - 1);
count = listBox1.Items.Count;
}
}
private void button2_Click(object sender, EventArgs e)
{
int x = 0;
Int32.TryParse(comboBox1.SelectedItem.ToString(), out x);
if (listBox1.Items.Count < x)
{
listBox1.Items.Add(x); //add whatever you want
}
}

how to store multiple selected listbox items in a session and view on another form?

The thing I want to do is that i want to select 1,2 or 3 items from the listbox and save them into a session and then display them all on another form in a listbox.
Here's my code!
This is my first post on stack overflow, so no hate please <3
//WebForm1
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lstProducts.Items.Add("Soap");
lstProducts.Items.Add("Schampoo");
lstProducts.Items.Add("Conditioner");
}
}
protected void cmdBuy_Click(object sender, EventArgs e)
{
string[] products = new string[3];
for (int i = 0; i < lstProducts.Items.Count; ++i)
{
if (lstProducts.Items[i].Selected)
products[i] = lstProducts.Items[i].Text;
else
products[i] = "0";
}
Session["Cart"] = products;
}
protected void cmdCart_Click(object sender, EventArgs e)
{
if (Session["Cart"] != null)
{
Response.Redirect("WebForm2.aspx");
}
}
}
//WebForm2
protected void Page_Load(object sender, EventArgs e)
{
string[] products = (string[])Session["Cart"];
for (int i = 0; i < 3; ++i)
{
if (products[i] != "0")
{
lstCart.Items.Add(products[i]);
}
}
}
}
}
The thing is that I only get the last selected item to display in the listbox on form2???
Try this
To store all items of the of the list box, you can add that items in array as:
string[] a = new string[]{"item 1","item 2","item 3"};
Session["values"] = a;
And in the next page, you can retrieve it like this.
string[] a = (string[])Session["values"]
EDIT #1
your case you can do like
ArrayList al = new ArrayList();
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected == true)
{
al.Add(ListBox1.Items[i].Value);
}
}
Session["selectedValues"] = al;
now you can use this sessiom variable in another page, but don't forget to cast in ArrayList type of object.

Find the highest number in array , using while or for?

Please a i have a Questions , I need find the higghest value in array. To the array will people write name (textbox1) and money (texbox2). I have 2 buttons first button is save to the array and second write the name with the biggest money.
Code for save:
string[] name = new string[50];
int items = 0;
int[] money = new int[50];
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
Convert.ToInt32(textBox2.Text);
}
catch
{
name[items] = textBox1.Text;
money[items] = Int32.Parse(textBox2.Text);
items++;
}
}
And to the button2 need search the biggest value and write name! Please help me
private void button2_Click(object sender, EventArgs e)
{
int maxIndex = 0;
for(int i = 0; i < 50; i++)
{
if (money[i] > money[maxIndex])
maxIndex = i;
}
MessageBox.Show(name[maxIndex] + " has biggest value " + money[maxIndex]);
}
To get the Max int from your array you can use IEnumerable.Max:
money.Max();
But there could be more than one name with the same high money value, perhaps you need to handle this also, I think Dictionary would be your best option
private Dictionary<string, int> Names = new Dictionary<string, int>();
private void button1_Click(object sender, EventArgs e)
{
int value = 0;
if (int.TryParse(textBox2.Text, out value))
{
if (!Names.ContainsKey(textBox1.Text))
{
Names.Add(textBox1.Text, value);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
if (Names.Any())
{
int maxMoney = Names.Max(v => v.Value);
var names = Names.Where(k => k.Value.Equals(maxMoney));
foreach (var name in names)
{
// Names with the highest money value
}
}
}

Rearrange the number in a Generic List

I am not sure if the title of my question is in sync with the content of my question.
I have a dropdown list with the datasource being list List<int> numbers;
On a Delete button click the numbers from the dropdown get deleted and and on Add Button click numbers are added to the dropdown.
Now if have 1, 2, 3 in my dropdown and if I delete 1, I want the number 2 and 3 to be replaced by 1 and 2. I am not sure how to do this. Can someone please help me?
if(!IsPostBack)
{
List<int> numbers = new List<int>();
Session["data"] = numbers;
rest of other code....
}
protected void btnAddDetail_Click(object sender, EventArgs e)
{
numbers = (List<int>)Session["data"];
if (numbers == null) { numbers = new List<int>(); }
if (numbers.Count!=0)
{
int max = numbers.Max();
numbers.Add(max + 1);
drpdown1.DataSource = numbers;
drpdown1.DataBind();
drpdown1.SelectedValue = numbers.Max().ToString();
Session["data"] = numbers;
}
}
protected void btnDelete(object sender, EventArgs e)
{
numbers = (List<int>)Session["data"];
int detailIndex = numbers.FindIndex(c => c == Convert.ToInt32 (deleteNo));
numbers.RemoveAt(detailIndex);
drpdown1.DataSource = numbers;
drpdown1.DataBind();
Session["data"] = numbers;
}
Now when I rebind the dropdown list I want the numbers 2 and 3 to be replaced by 1 and 2.
If I understand what you are trying to do, try using an index array:
put these two controls in the aspx page:
<asp:DropDownList ID="ddlNumbers" runat="server" />
<asp:Button id="btnRemove" runat="server" />
put this code in your code-behind:
public ArrayList Numbers
{
get
{
return (ArrayList)this.ViewState["Numbers"];
}
set
{
this.ViewState["Numbers"] = value;
}
}
protected override void OnInit(EventArgs e)
{
this.btnRemove.Click += new EventHandler(btnRemove_Click);
base.OnInit(e);
}
void btnRemove_Click(object sender, EventArgs e)
{
this.Numbers.RemoveAt(Convert.ToInt32(this.ddlNumbers.SelectedValue));
this.BindList();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.Numbers = new ArrayList();
for (int i = 0; i < 10; i++)
{
this.Numbers.Add(i);
}
this.BindList();
}
}
private void BindList()
{
this.ddlNumbers.Items.Clear();
for (int i = 0; i < this.Numbers.Count; i++)
{
this.ddlNumbers.Items.Add((i + 1).ToString());
}
}

Categories

Resources