I just wanted to add 4 values (100,200,300,400) for 4 buttons and display that in a list and get the total by pressing a another button
Here is the example
As I understand you have got four buttons where each of them has some value as text (100, 200, 300, 400), right? If any of them is clicked the value of the button is added to the list view.
Once the "sum" button is clicked all values from the list are summed and the total value is displayed.
First of all you need to create an event for each button click and add it's Text to the list.
private void button1_Click(object sender, EventArgs e)
{
**yourList**.Items.Add(button1.Text);
}
Then for the "SUM" button action you need to add all yourList values and display
private void sumButton_Click(object sender, EventArgs e)
{
int sum = 0;
foreach (ListViewItem item in listView1.Items)
{
//make sure the text is a number
if (int.TryParse(item.Text, out int result))
{
sum += result;
}
}
MessageBox.Show(sum.ToString());
}
you can try this function in button_click
public int Sum(){
int num1 = int.Parse(button1.Text);
int num2 = int.Parse(button2.Text);
int num3 = int.Parse(button3.Text);
int num4 = int.Parse(button4.Text);
int sum = num1+num2+num3+num4;
return sum;
}
Related
So I have 2 list boxes within my form. Listbox1 contains different types of items that have a price and Listbox2 contains how much of that item you want to purchase. How do I update my price label so when I select both options from each list box it updates the label and gives me a price. Here's an example to help you better understand.
I select the $1.50 Chocolate Chip Cookie item in my ListBox1 and in ListBox2 I select the 1 Dozen Cookie item. So I would want my priceLabel to update to $18.00. How would I do this?
As of now I have tried creating some code in the listBox1_SelectedIndexChanged method but I am returned these 3 following values... $0.00...$2.00...$4.00
Here's my code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
double index = listBox1.SelectedIndex;
double index2 = listBox2.SelectedIndex;
double total = index * index2;
label9.Text = total.ToString("C");
}
private void label5_Click(object sender, EventArgs e)
{
}
private void label9_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
const int ESTIMATED_ARRIVAL = 3;
label10.Text = monthCalendar1.SelectionStart.AddDays(ESTIMATED_ARRIVAL).ToShortDateString();
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
In listBox1_SelectedIndexChanged(object sender, EventArgs e) you use listBox1.SelectedIndex; and listBox2.SelectedIndex;, if you refer to ListBox.SelectedIndex Property
ListBox.SelectedIndex Property
Gets or sets the zero-based index of the currently selected item in a
ListBox.
Property Value
Int32
A zero-based index of the currently selected item. A value of negative one (-1) is returned if no item is selected.
it just return index of selected item, so for your purpose you must get value of selected item.
I hope this code be a good guide for you:
Add handler of SelectedIndexChanged event of both list boxes to this method:
private void ListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.listBox1.SelectedIndex > -1 && this.listBox2.SelectedIndex > -1)//You can set default SelectedIndex for list boxes and remove this
{
string s1 = this.listBox1.Items[this.listBox1.SelectedIndex].ToString();
string s2 = this.listBox2.Items[this.listBox2.SelectedIndex].ToString();
//Now we extracting the number from string
//NOTE this is a simple implementation. You must change it as your needs.
//for example
//s1 = $1.50 Chocolate Chip Cookie
//s2 = 1 Dozen Cookie
int index = s1.IndexOf(' ');//get the index of first space after 1.50 (Number) in s1
s1 = s1.Substring(1, index);
index = s2.IndexOf(' ');//get the index of first space after 1 (Number) in s2
s2 = s2.Substring(0, index);
if (double.TryParse(s1, out double p1) && double.TryParse(s2, out double p2))
{
const int DOZEN = 12;
double result = p1 * (p2 * DOZEN);
//or
//remove const int DOZEN = 12; and simply
//double result = p1 * (p2 * 12);
this.label9.Text = result.ToString("C");
}
else
{
MessageBox.Show("Can not parse double values.");
}
}
}
I am trying to create comboBox items with unique number sequencer.
With each click on button comboBox items should be added like this;
Item 1
Item 2
Item 3
...
If I modify The name of Item 1; Next click has to name new item as Item 1.
I am unable to create this basic sequencer and require your help.
private void addNew_Click(object sender, EventArgs e)
{
//textBox1.Enabled = true;
//textBox1.Focus();
comboBox1.Items.Add("item", i);
}
define counter on the scope of your form, under form constructor, for example. Like this:
int counter = 1;
public Form1()
{
InitializeComponent();
}
in your method for adding items add item with counter value and then increment counter.
private void addNew_Click(object sender, EventArgs e)
{
//textBox1.Enabled = true;
//textBox1.Focus();
comboBox1.Items.Add("item " + counter.ToString());
counter++;
}
I need to displaying items of the 4th column in a textbox row by row after clicking 'next' button. When the user clicks next, the next row should be displayed until the last row.
Below is the code I have so far, that only displays the last row when button 'next' is clicked. I cannot seem to get the loop right.
public partial class RecordingView : Form
{
public RecordingView()
{
InitializeComponent();
}
public void UpdateListView(ListView listView1)
{
foreach (ListViewItem item in listView1.Items)
{
this.listView1.Items.Add((ListViewItem)item.Clone());
}
}
private void RecordingView_Load(object sender, EventArgs e)
{
//Add column header
listView1.Columns.Add("#", 60);
listView1.Columns.Add("Start time", 100);
listView1.Columns.Add("End time", 100);
listView1.Columns.Add("Duration", 100);
listView1.Columns.Add("Text", 350);
}
private void btnNext_Click(object sender, EventArgs e)
{
for (int i = 0; i < listView1.Items.Count; i++)
{
string line = listView1.Items[i].SubItems[4].Text; //picks list item
rtbCurrentLine.Text = line;
}
}
}
Below is my user interface
user interface
kindly help me figure out where I have gone wrong.
I suggest this, declare a global variable that will get your index of next or previous click:
int intIndex = -1;
Then on your btnNext click event handler, increment it per click:
private void btnNext_Click(object sender, EventArgs e)
{
if (intIndex <= listView1.Items.Count)
{
intIndex++;
string line = listView1.Items[intIndex].SubItems[4].Text;
rtbCurrentLine.Text = line;
}
}
I am new to C#. I need to link two number boxes to a single Vscrollbar. The scrollbar button is in the middle and set to zero, as the scroll button moves up or down the numbers change accordingly. I need a minus sign for below zero.
TIA.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
vScrollBar1.Value = vScrollBar1.Maximum / 2;
ChangeTextBoxValues(vScrollBar1.Maximum / 2, vScrollBar1.Value);
}
private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
int avg = vScrollBar1.Maximum / 2;
ChangeTextBoxValues(avg, e.NewValue);
}
void ChangeTextBoxValues(int AvgValue, int NewValue)
{
textBox1.Text = (AvgValue - NewValue).ToString();
textBox2.Text = (NewValue - AvgValue).ToString();
}
}
Once you have the value you can do some maths with it to populate your textboxes.
private void vScrollBar1_ValueChanged(object sender, EventArgs e)
{
string value = vScrollBar1.Value.ToString();
}
Guys im currently making a simple Ordering form, i'm almost done i just need to validate some things, i want to make my Price and Qty multiply after i enter/change the value on Qty without pressing any buttons
for example Price = 10 Qty = Total = 10
if i put a 2 the Total should automatically change to 20 without pressing any buttons
private void btnAddOrder_Click(object sender, EventArgs e)
{
//Get the txtbox then add to dgv
int qty = int.Parse(txtPrice.Text);
int price = int.Parse(txtQty.Text);
txtTotal.Text = (qty * price).ToString();
dgvOrder.Rows.Add(txtItemCode.Text, txtDescription.Text, txtPrice.Text, txtQty.Text,txtTotal.Text);
}
right now that's my code for those 3 txtbox
You can implement TextBox.TextChanged Event for Quantity textbox.
In your Constructor:
MyTextBox.TextChanged += new TextChangedEventHandler( TextChanged );
And Then this Method:
private void TextChanged(object Sender, TextChangedEventArgs e)
{
//Do your stuff here
}