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());
}
}
Related
I have a basic form with a button and a textbox to insert a number
I want to keep the number inserted in the first index of the array, then if I put another number and I click the button, I want that number to be stored in the second index of the array and so on
I have to make a loop to increment the index but if I use the loop, the first number I put in the box, it's going to be stored in all the indexes of the array
Here's my code (doesn't work)
public partial class EXERCISES_ARRAYS : Form
{
int[] numbers = new int[5];
private void btnAdd_Click(object sender, EventArgs e)
{
int i = 0;
int insertedNumber = Convert.ToInt32(txtInsertedValue.Text);
for (int j = 0; j < 5; j++)
{
numbers[i] = insertedNumber;
i++;
if (i == 5)
{
btnAdd.Enabled = false;
}
}
}
}
You want a List, rather than an array. That will take care of all of this for you:
public partial class EXERCISES_ARRAYS : Form
{
List<int> numbers = new List<int>();
private void btnAdd_Click(object sender, EventArgs e)
{
numbers.Add(int.Parse(txtInsertedValue.Text));
}
}
I don't think you need to add a for loop for this. Try this
int[] numbers = new int[5];
int i = 0;
private void BtnAdd_Click(object sender, RoutedEventArgs e)
{
int insertedNumber = Convert.ToInt32(txtInsertedValue.Text);
numbers[i] = insertedNumber;
i++;
if (i == 5)
{
btnAdd.Enabled = false;
}
}
If I click on btn1 the contains of array 1 (generate) will change. What I want to do is: after clicking on btn2 (Reset btn) want to copy the elements of array 2 (n) to array1 (generate) but I'm getting this error:
Destination array was not long enough. Check destIndex and length, and the array's lower bounds.
public partial class Form1 : Form
{
string[] generate = new string[20] {"Name1","Name2","Name3","Name4","Name5","Name6","Name7","Name8","Name9","Name10","Name11","Name12","Name13","Name14","Name15","Name16","Name17","Name18","Name19","Name20" };
string[] n = new string[] { "Name1", "Name2", "Name3", "Name4", "Name5", "Name6", "Name7", "Name8", "Name9", "Name10", "Name11", "Name12", "Name13", "Name14", "Name15", "Name16", "Name17", "Name18", "Name19", "Name20" };
string name;
int Num;
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e) // START btn
{
Num = int.Parse(textBox1.Text) - 1;
if (Num < 20)
{
if (generate.Length > Num)
{
for (int i = Num; i >= 0; i--)
{
name = generate.ElementAt(i);
listBox1.Items.Add(name); // Print it to list1
generate = generate.Where(s => s != name).ToArray(); // Delete name after using
}
}
else MessageBox.Show("Sorry, remaining names =" + generate.Length);
listBox2.Items.Clear();
listBox2.Items.AddRange(generate);
}
else MessageBox.Show("Max name is 20!");
}
private void button2_Click(object sender, EventArgs e) // Reset btn
{
listBox1.Items.Clear();
listBox2.Items.Clear();
textBox1.Clear();
Array.Copy(n, 0, generate, 0, 20);
}
}
}
You can use LINQ to duplicate array.
var array2 = array1.ToList().ToArray()
If you use .Copy method make sure your range is correct
When you click button 1, the following line is creating a new array for the generate variable which is of size less than 20:
generate = generate.Where(s => s != name).ToArray(); // Delete name
So, when you call button 2, you are trying to copy from array n of size 20 to the array generate of size less than 20.
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.
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
}
}
}
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();
}