How to show listbox added items count in Label? - c#

private void button5_Click(object sender, EventArgs e)
{
int gTotal = 1;
for (int gCount = 0; gCount < listBox3.Items.Count - 1; gCount++)
gTotal += (listBox3.Items.Add(gCount));
label1.Text = gTotal.ToString();
}
Hey guys i'm not entirely sure of how to use Listboxes, but my question is how can I display the numbers that populate in my list box 3 to go into the label?

private void button5_Click(object sender, EventArgs e)
{
int gTotal = 1;
var collection = listBox3.Items.Cast<String>().ToList();
for (int gCount = 0; gCount < collection.Count - 1; gCount++)
{
int item;
if (int.TryParse(collection[gCount], out item)
{
gTotal += item;
}
}
label1.Text = gTotal.ToString();
}
Cast the items to a List then make sure that items are ints, if so add them up.

Parse your items.
private void button5_Click(object sender, EventArgs e)
{
int gTotal = 1;
for (int gCount = 0; gCount < listBox3.Items.Count; gCount++)
gTotal += int.Parse(listBox3.Items[gCount].ToString());
// assuming all items in the listbox is an int.
label1.Text = gTotal.ToString();
}
What trying to do

Related

Incrementing Text in Circular Progress Bar

private void _btnOK_Click(object sender, EventArgs e)
{
_label1.Hide();
_label2.Hide();
_label3.Hide();
for(int i = 1; i <= 100; i++)
{
Thread.Sleep(5);
_circularprogressbar.Value = i;
_circularprogressbar.Update();
}
}
private void LoadingScreen_Load(object sender, EventArgs e)
{
_circularprogressbar.Value = 0;
_circularprogressbar.Minimum = 0;
_circularprogressbar.Maximum = 100;
}
}
}
This is my code. What i want to do is, i want to have a text inside the progress bar that shows the percentage of the progress from 1 to 100 percent.
what can i add to my code?
thank you
Here is what i would do:
private void _btnOK_Click(object sender, EventArgs e)
{
_label1.Hide();
_label2.Hide();
_label3.Hide();
for(int i = 1; i <= 100; i++)
{
_circularprogressbar.Value = i;
_percent_lable_name.Text = string.Format("{0}%", _circularprogressbar.Value);
_circularprogressbar.Update();
}
}
private void LoadingScreen_Load(object sender, EventArgs e)
{
_circularprogressbar.Value = 0;
_circularprogressbar.Minimum = 0;
_circularprogressbar.Maximum = 100;
}
}
See if that helps you!
Thanks
Techcraft7 :)
That Thread.Sleep(5) is blocking your entire UI thread. If you want to have your UI responsive, while the progress takes place, you need to make a separate thread for it. Something like this:
private void _btnOK_Click(object sender, EventArgs e)
{
_label1.Hide();
_label2.Hide();
_label3.Hide();
Task.Factory.StartNew(() =>
{
for (int i = 1; i <= 100; i++)
{
Thread.Sleep(5);
Invoke((Action)(() =>
{
_circularprogressbar.Value = i;
_circularprogressbar.Update();
}));
}
});
}
Note that you will need t use Invoke to BeginInvoke to access UI components from inside that thread.

Add string to same textBox instead of current string

I have form which should change text inside textbox on button click. Text should be read from List, k.pojam. So on first click to the button text Box should show first element in list, second click second element etc. How to fix problem ?
//List<Karta> list; this list already has some number of Karta objects
int cardCounter=0;
private void btnNext_Click(object sender, EventArgs e)
{
int currentCounter = 0;
foreach (Karta k in list)
{
if(cardCounter==currentCounter)
{
txtBoxPojam.Text = k.Pojam;
txtBoxPojam.Show();
cardCounter++;
}
currentCounter++;
}
edit txtBoxPojam.Text += k.Pojam; // I need something like this, but instead of adding string I would like to write another string instead of current, because obviously txtBoxPojam.Text = k.Pojam; doesnt work?
int mainCounter = 0;
int totalItems = 0;
List<string> LstItems = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
LstItems.Add("Test Item 1");
LstItems.Add("Test Item 2");
LstItems.Add("Test Item 3");
LstItems.Add("Test Item 4");
LstItems.Add("Test Item 5");
totalItems = LstItems.Count();
}
private void btnNext_Click(object sender, EventArgs e)
{
try
{
if (mainCounter > totalItems)
{
//your implementation
return;
}
txtNext.Text = LstItems[mainCounter].ToString();
mainCounter++;
}
catch (Exception)
{
}
}
Instead of txtBoxPojam.Text = k.Pojam; working combination is txtBoxPojam.Text = string.Empty; and txtBoxPojam.Text += k.Pojam;
txtBoxPojam.Text = string.Empty;
int cardCounter=0;
private void btnNext_Click(object sender, EventArgs e)
{
int currentCounter = 0;
foreach (Karta k in list)
{
if(cardCounter==currentCounter)
{
txtBoxPojam.Text += k.Pojam;
txtBoxPojam.Show();
cardCounter++;
}
currentCounter++;
}

Put into array using for loop

I am thinking of finding an easy way to put numbers into an array using a for loop. I have made the easy design with text showing how the name system works!
My code is like this:
double[,] kast = new double[3, 8];
string[] navn = new string[8];
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSorter_Click(object sender, EventArgs e)
{
for(int i = 1; i > 4; i++)
{
for(int x = 1; i > 9; i++)
{
kast[i, x] = Convert.ToDouble(txtKast + i + x + .Text);
}
}
}
I don't know how I will make it work with the Convert.ToDouble part since "kast" is a double array.

autocalculate total value of items in listview when removing item using c#

I'm using a listview as a shopping cart. I need to know how to recalculate the total value of the cart when I remove an item.
Here is my code for adding to listview;
private void btnACart_Click(object sender, EventArgs e)
{
int value = 0;
for (int i = 0; i < lvCart.Items.Count; i++)
{
value += int.Parse(lvCart.Items[i].SubItems[1].Text);
}
rtbTcost.Text = value.ToString();
}
Here is my code for removing items:
private void btnRemoveItem_Click(object sender, EventArgs e)
{
int total = 0;
foreach (ListViewItem item in lvCart.Items)
{
if (lvCart.Items[0].Selected)
{
lvCart.Items.Remove(lvCart.SelectedItems[0]);
total += Convert.ToInt32(item.SubItems[1].Text);
}
}
rtbTcost.Text = total.ToString();
}
I want to recalculate the total value of items an item is removed. How should I do that?
Something like this
On the form level declare
private int _listTotal;
Adding - I think here you have some problems because you should add to total when you add the item
private void btnACart_Click(object sender, EventArgs e)
{
int value = 0;
for (int i = 0; i < lvCart.Items.Count; i++)
{
value += int.Parse(lvCart.Items[i].SubItems[1].Text);
}
// how about lvCart.Items.Add(<myVal>)...???
_listTotal += value; // and here add myVal
rtbTcost.Text = _listTotal.ToString();
}
Then when removing - you don't want to use any "for-loops" on mutating collection. But "while" works perfectly on mutations
private void btnRemoveItem_Click(object sender, EventArgs e)
{
int totalRemoved = 0;
while (lvCart.SelectedItems.Count > 0)
{
totalRemoved += Convert.ToInt32(lvCart.SelectedItems[0].SubItems[1].Text);
lvCart.Items.Remove(lvCart.SelectedItems[0]);
}
_listTotal -= totalRemoved;
rtbTcost.Text = _listTotal.ToString
}
Not tested but should work
You can't change the same list in foreach.
foreach (ListViewItem item in lvCart.Items)
{
if (lvCart.Items[0].Selected)
{
lvCart.Items.Remove(lvCart.SelectedItems[0]);
total += Convert.ToInt32(item.SubItems[1].Text);
}
}
The solution is create duplicate list and change it:
var newList = lvCart;
foreach (ListViewItem item in lvCart.Items)
{
if (lvCart.Items[0].Selected)
{
newList.Items.Remove(lvCart.SelectedItems[0]);
total += Convert.ToInt32(item.SubItems[1].Text);
}
}
private void btnRemoveItem_Click(object sender, EventArgs e)
{
int total = 0;
foreach (ListViewItem item in lvCart.Items)
{
if (lvCart.Items[0].Selected)
{
total+=(int)lvCart.SelectedItems[0].SubItems[1].Text;//Add to total while romving
lvCart.Items.Remove(lvCart.SelectedItems[0]);
//total += Convert.ToInt32(item.SubItems[1].Text);
}
}
rtbTcost.Text = total.ToString();
}

how to get the next coming seven days in my listbox and remove the others?

so far i did that but it doesnt work instead of getting me the specific date it erase all the list items please help
private void button1_Click_1(object sender, EventArgs e)
{
List<RentalCar> listBox1snew = new List<RentalCar>();
for (int i = 0; i < listBox1s.Count; i++)
{
if ((DateTime.Now.Day - listBox1s[i].WOF.Day) <= 7)
{
listBox1.Items.Insert(0, listBox1snew[i]);
}
}
listBox1.DataSource = listBox1snew;//add car to listbox
}
First of all your logic to check for the date should be:
DateTime.Now.Subtract(listBox1s[i].WOF).Days <= 7
You're setting the ListBox datasource to listBox1snew but you're never Adding anything to listBox1snew!
listBox1.Items.Insert(0, listBox1snew[i]);
This makes no sense. listBox1snew is empty. I believe you meant listBox1s[i].
listBox1.DataSource = listBox1snew;
This makes even less sense. First, you're inserting items in listBox1.Items and then you override the DataSource, effectively ignoring the Items. Also, you didn't modify listBox1snew in any way, so it will still be empty!
What I think you're after:
Copy elements from listBox1s to listBox1snew.
Set the DataSource to listBox1snew.
That would look like:
private void button1_Click_1(object sender, EventArgs e)
{
List<RentalCar> listBox1snew = new List<RentalCar>();
for (int i = 0; i < listBox1s.Count; i++)
{
if (DateTime.Now.Subtract(listBox1s[i].WOF).Days <= 7)
{
// Copy from listBox1s to listBox1snew
listBox1new.Add(listBox1s[i]);
}
}
// Use listBox1new as new data source
listBox1.DataSource = listBox1new;
}
Also, you could easily express this filter with LINQ:
private void button1_Click_1(object sender, EventArgs e)
{
listBox1.DataSource = listBox1s.Where(x => DateTime.Now.Subtract(x.WOF).Days <= 7).ToList();
}
You should subtract the two days first then find days difference.
private void button1_Click_1(object sender, EventArgs e)
{
List<RentalCar> listBox1snew = new List<RentalCar>();
for (int i = 0; i < listBox1s.Count; i++)
{
if ((DateTime.Now - listBox1s[i].WOF).Days <= 7)
{
listBox1snew .Items.Insert(0, listBox1s[i]);
}
}
listBox1.DataSource = listBox1snew;//add car to listbox
}
DateTime.Now.AddDays(-7) <= listBox1s[i].WOF

Categories