How to make label text(number) change by clicking checkbox? - c#

Teacher gave us an assignment. Checkboxes are lessons that students may choose and labels under them are the free spots left. Basically everytime a lesson(checkbox) selected the number connected to it at label should lessen minus 1. if person unchecks it, number should return to basic.
Sorry for my English, i hope it's understandable.

You can try to subscribe to the CheckedChanged event for each checkbox. And use Convert.ToInt32 Method to get the value in labels. Then judge the checkbox selected via swicth statement.
public Form1()
{
InitializeComponent();
checkBox1.CheckedChanged += checkBox_CheckedChanged;
checkBox2.CheckedChanged += checkBox_CheckedChanged;
checkBox3.CheckedChanged += checkBox_CheckedChanged;
}
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
if (((CheckBox)sender).Checked)
{
switch (((CheckBox)sender).Name)
{
case "checkBox1":
labelofcb1.Text = (Convert.ToInt32(labelofcb1.Text) + 1).ToString();
break;
case "checkBox2":
labelofcb2.Text = (Convert.ToInt32(labelofcb2.Text) + 1).ToString();
break;
case "checkBox3":
labelofcb3.Text = (Convert.ToInt32(labelofcb3.Text) + 1).ToString();
break;
}
}
else
{
switch (((CheckBox)sender).Name)
{
case "checkBox1":
labelofcb1.Text = (Convert.ToInt32(labelofcb1.Text) - 1).ToString();
break;
case "checkBox2":
labelofcb2.Text = (Convert.ToInt32(labelofcb2.Text) - 1).ToString();
break;
case "checkBox3":
labelofcb3.Text = (Convert.ToInt32(labelofcb3.Text) - 1).ToString();
break;
}
}
}

That's an example where I picked the checkedchanged property of each of the checkboxs and this is the function in each of them. you can change the initial value or the changed value as you want by changing the 0 or the ++ or the --. You just add the two if conditions from each function to your function and change the name in them to reflect the name of your label.
public partial class Form1 : Form
{
int counter=0;
public Form1()
{
InitializeComponent();
label1.Text = counter.ToString();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
counter++;
label1.Text = counter.ToString();
}
if (!checkBox1.Checked)
{
counter--;
label1.Text = counter.ToString();
}
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
if (checkBox3.Checked)
{
counter++;
label1.Text = counter.ToString();
}
if (!checkBox3.Checked)
{
counter--;
label1.Text = counter.ToString();
}
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked)
{
counter++;
label1.Text = counter.ToString();
}
if (!checkBox2.Checked)
{
counter--;
label1.Text = counter.ToString();
}
}
}

Related

i write a calculator.everything work,but when i want use dot '.' for Decimals its jump out.i cant understand whats wrong

public partial class Form1 : Form
{
double resualt,oprand1,oprand;
char oprand2;
private void Dot_Click(object sender, EventArgs e)
{
tb.Text += dot.Text;
}
private void B1_Click(object sender, EventArgs e)
{
tb.Text += b1.Text;
}
private void Sum_Click(object sender, EventArgs e)
{
oprand1 += double.Parse(tb.Text);
tb.Clear();
oprand2 = '+';
}
private void Rzat_Click(object sender, EventArgs e)
{
oprand = double.Parse(tb.Text);
switch(oprand2)
{
case '+':
{
resualt = oprand1 + oprand;
break;
}
case '-':
{
resualt = oprand1 - oprand;
break;
}
}
oprand1 = 0;
tb.Text = resualt.ToString();
}
i write code for all number and + - * / but i cant use dot'.'every thing work until i use for example 4.5+ and its jump out and give me a error!
Try using double.Parse(String, IFormatProvider) with InvariantCulture.
oprand1 += double.Parse(tb.Text, System.Globalization.CultureInfo.InvariantCulture);

How to dynamically add or remove textboxes?

I would like to make a tool on my form that would allow the user to add or remove textboxes using a [+] and [-] button. This should only be possible if the items "*.doc" or "*.docx" are selected in a ComboBox.
I have tried this for the .doc thingy:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (cmbExtension.Text)
{
case "Other...":
string extensionName = Interaction.InputBox("Enter the new extension's name (for example *.txt): ", "New extension!");
File.AppendAllText(strPath, "\n" + extensionName);
// string extensionFunction = Interaction.InputBox("Enter the type of file it's supposed to be (for example Microsoft Word 2016): ", "Give us an idea.");
cmbExtension.Items.Clear();
LoadLines();
break;
case "*.doc":
btnPlus.Show();
break;
case "*.docx":
btnPlus.Show();
break;
default:
btnPlus.Hide();
break;
}
// As well as using similar code in these things, now empty:
if (cmbExtension.Text == "Other...")
{
}
if (cmbExtension.Text == "*.doc" || cmbExtension.Text == "*.docx")
{
}
}
You can use a FlowLayoutPanel in which you add and remove the textboxes.
To add a TextBox to a FlowLayoutPanel (or any container control) use:
TextBox textBox = new TextBox();
this.flowLayoutPanel1.Controls.Add(textBox);
To remove the last added TextBox from FlowLayoutPanel (or any container control) use:
int count = this.flowLayoutPanel1.Controls.Count;
if (count > 0)
{
this.flowLayoutPanel1.Controls[count - 1].Dispose();
}
Hear is an simple example with:
A FlowLayoutPanel with FlowDirection set to TopDown
Two buttons named btnPlus and btnMinus
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
cmbExtension.Items.Add("*.doc");
cmbExtension.Items.Add("*.docx");
cmbExtension.Items.Add("Other...");
btnPlus.Hide();
btnMinus.Hide();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (cmbExtension.Text)
{
case "Other...":
// ...
btnPlus.Hide();
btnMinus.Hide();
break;
case "*.doc":
btnPlus.Show();
btnMinus.Show();
break;
case "*.docx":
btnPlus.Show();
btnMinus.Show();
break;
default:
btnPlus.Hide();
btnMinus.Hide();
break;
}
}
private void btnPlus_Click(object sender, EventArgs e)
{
TextBox textBox = new TextBox();
this.flowLayoutPanel1.Controls.Add(textBox);
}
private void btnMinus_Click(object sender, EventArgs e)
{
int count = this.flowLayoutPanel1.Controls.Count;
if (count > 0)
{
this.flowLayoutPanel1.Controls[count - 1].Dispose();
}
}
}
Snapshots:

Counter doesn't start on time

Ok, I'm trying to explain this on a simple example.
I want counter to have the value 0 at the beginning. label1 is invisible until I click on button1. My problem now is that when I click on button1 for the first time, 0 appears instead of 1. Meaning I need to click two times on button1, so that "1" appears. (I'm quite new to C#, so don't use jargon please =P)
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
label1.Visible = false;
}
int counter = 0;
private void button1_Click(object sender, EventArgs e)
{
label1.Visible = true;
label1.Text = "number " + counter;
counter++;
}
}
Look closely at your click method:
private void button1_Click(object sender, EventArgs e)
{
label1.Visible = true;
label1.Text = "number " + counter;
counter++;
}
You are first assigning the (current) value of counter to label1.Text and then increment it. Swap statements 2 and 3:
private void button1_Click(object sender, EventArgs e)
{
label1.Visible = true;
counter++;
label1.Text = "number " + counter;
}
Either:
Initialize counter to 1
Increment the counter before showing it
label1.Text = "number " + (++counter).ToString();
or
counter++;
label1.Text = "number " + counter.ToString();
Use (counter+1) as your value
label1.Text = "number " + (counter+1).ToString();

C# calculator, can't get the mathematical operators to come up in the text box

I got the calculator to function correctly but decided that the code was far too messy and I needed to tidy it up and find the best practice which I thought I found looking at some videos on youtube.
Unfortunately now I can't get the operators to display on the screen with this new method (an example would be if I press '6' the number 6 will come up but then if I press '+' nothing will happen and if I can't insert the operator then I can't check if the equal button is working)
Any help would be much appreciated the code is below.
Double value = 0;
String operation = "";
//bool op_pressed = false;
public MainForm()
{
InitializeComponent();
}
public void button_click(object sender, EventArgs e)
{
Button button = (Button)sender;
textBox1.Text = textBox1.Text + button.Text;
}
public void op_click(object sender, EventArgs e)
{
Button button = (Button)sender;
operation = button.Text;
value = Double.Parse(textBox1.Text);
//op_pressed = true;
}
public void ClearClick(object sender, EventArgs e)
{
textBox1.Text = "";
}
private void EqualClick(object sender, EventArgs e)
{
switch(operation)
{
case "+":
textBox1.Text = (value + Double.Parse(textBox1.Text)).ToString();
break;
case "-":
textBox1.Text = (value - Double.Parse(textBox1.Text)).ToString();
break;
case "/":
textBox1.Text = (value / Double.Parse(textBox1.Text)).ToString();
break;
case "*":
textBox1.Text = (value * Double.Parse(textBox1.Text)).ToString();
break;
default:
break;
}
//op_pressed = false;
}
Your "op_click" method is not adding the operator to the textbox1.text. That is why its not displaying.

why does the CLEAR button seem to disable my listbox label output?

When I run my program with a listbox everything works after selecting the items and pressing enter, but when I press the clear button and I select the items again and press enter nothing happens. I've tried the following for the clear button and they clear my label text and the selected listbox but I can no longer produce another output when I try pressing enter button again after selecting the items.
public partial class frmLabSix : Form
{
public string strCakes;
public int cakeCost;
public frmLabSix()
{
InitializeComponent();
}
private void lstCakes_SelectedIndexChanged(object sender, EventArgs e)
{
for (int index = 0; index < lstCakes.SelectedItems.Count; index++)
{
strCakes += Environment.NewLine + lstCakes.SelectedItems[index].ToString();
if (lstCakes.SelectedIndices[index] == 0) cakeCost += 18;
if (lstCakes.SelectedIndices[index] == 1) cakeCost += 25;
if (lstCakes.SelectedIndices[index] == 2) cakeCost += 40;
if (lstCakes.SelectedIndices[index] == 3) cakeCost += 30;
}
}
private void lblOrdered_Click(object sender, EventArgs e)
{
}
private void btnEnter_Click(object sender, EventArgs e)
{
double tax = 1.13;
lblOrdered.Text = "You have ordered: " + strCakes + '\n' + "Total Cost: " + (tax * cakeCost).ToString("C");
lblOrdered.Visible = true;
}
private void btnClear_Click(object sender, EventArgs e)
{
lstCakes.SelectedItems.Clear();
lblOrdered.Visible = false;
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
Can someone tell me why this is?
This line of code hides your label.
lblOrdered.Visible = false;
You make your label invisible on clear button click. Do you reset its visibility after?
I don't know, what happens in other part of your code, but it should probably be like this:
// if the label is not visible, the next line won't make it visible implicitly
lblOrdered.Text = ...
//you should set label's visibility explicitly
if (!lblOrdered.Visible)
lblOrdered.Visible = true;
Setting label's text doesn't make it visible. If you hide it with your own code, you should make label visible explicitly as well.

Categories

Resources