Wanting to change a label using a combo box selection - c#

I'm making a program that lets the user select a score/grade from a combobox and using a button click will calculate the answer for the user. However for some reason when I press calculate button the label text doesn't change at all. Also sorry if the codes messy or looks wrong as I'm still trying to learn.
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace btec_to_ucas
{
public partial class Form1 : Form
{
//These are the values i want displayed on the label
int PPP = 48;
int MPP = 64;
int MMP = 80;
int MMM = 96;
int MMD = 112;
int DDM = 128;
int DDD = 144;
public Form1()
{
InitializeComponent();
}
// below i want whenever the button is pressed it will take the selected answer and display the int onto the label
private void button1_Click(object sender, EventArgs e)
{
{
switch (comboBox1.SelectedIndex)
{
case 0:
if (comboBox1.SelectedIndex == PPP)
{
label1.Text = "48";
}
break;
case 1:
if (comboBox1.SelectedIndex == MPP)
{
label1.Text = "64";
}
break;
case 2:
if (comboBox1.SelectedIndex == MMP)
{
label1.Text = "96";
}
break;
}
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
} ```

Your switch statement should be like this:
switch (comboBox1.SelectedIndex)
{
case 0:
label1.Text = PPP.ToString();
break;
case 1:
label1.Text = MPP.ToString();
break;
...

Related

How to change label text and color when a condition is met? (C#)

I am making a password generator and on websites when you enter certain conditions are met the strength of the password changes how can I change the color and text of the label when the password strength is >= 8, <8<10, >12?
Here is the Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Password_Generator
{
public partial class PassGen : Form
{
int currentPasswordLength = 0;
Random Character = new Random();
private void PasswordGenerator(int PasswordLength)
{
String validChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!##$&?";
String randomPassword = "";
//25
for(int i = 0; i < PasswordLength; i++)
{
int randomNum = Character.Next(0, validChars.Length);
randomPassword += validChars[randomNum];
}
Password.Text = randomPassword;
}
public PassGen()
{
InitializeComponent();
PasswordLengthSlider.Minimum = 5;
PasswordLengthSlider.Maximum = 22;
PasswordGenerator(5);
}
private void Label1_Click(object sender, EventArgs e)
{
}
private void Copy_Click(object sender, EventArgs e)
{
Clipboard.SetText(Password.Text);
}
//52
private void PasswordLength_Click(object sender, EventArgs e)
{
}
private void PasswordLengthSlider_Scroll(object sender, EventArgs e)
{
PasswordLength.Text = "Password Length:" + " " + PasswordLengthSlider.Value.ToString();
currentPasswordLength = PasswordLengthSlider.Value;
PasswordGenerator(currentPasswordLength);
}
private void pswdStrengthTest()
{
if (currentPasswordLength <= 8)
{
pswdStrength.Text = "weak";
pswdStrength.ForeColor = Color.Red;
} else if (currentPasswordLength<= 9)
{
pswdStrength.Text = "ok";
pswdStrength.ForeColor = Color.Blue;
}
}
//78
private void pswdStrength_Click(object sender, EventArgs e)
{
}
}
}
If anyone could help me with this it would be greatly appreciated. This is based off a tutorial I found on YouTube. I'm not sure what the video is called but if it helps I could search for it and update my posting.
Try this:
Password.TextChanged += (s1, e1) =>
{
if (Password.Text.Length > 10)
pswdStrength.ForeColor = Color.Green
else if (Password.Text.Length > 8)
pswdStrength.ForeColor = Color.Blue
else
pswdStrength.ForeColor = Color.Red
};
Your code looks like a windows form application.
If you have for example one objetc txt_password, check to code some of these events:
TextChanged: this occurs when your textbox has been changed
Others events could be:
KeyPress or KeyDown

Is it necessary to use switch cases in each widget function of windows forms if we have different cases to consider?

I am making a simple Unit Converter windows form application in c#.net, as it is a unit converter, I added a drop-down list or combo box to select "mass", "length", "temperature", etc. To add functionality to those combo box items, do I have to use a switch case for that particular selection in each and every widget function(i.e. Calculate Button, Combo Box, Radio buttons, etc.) or is there a way to get this done at once? Below is the incomplete code I have written so far in the visual studio. I have used switch cases in Calculate Button and Combo Box Selector for now (marked with comments).
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace UnitConverter
{
public partial class formUnitConverter : Form
{
public formUnitConverter()
{
InitializeComponent();
}
private void FormUnitConverter_Load(object sender, EventArgs e)
{
}
private void BtnCalculate_Click(object sender, EventArgs e) //Calculate Button
{
if (isNum(textBox1.Text))
{
switch (comboBox1.SelectedItem)
{
case "Mass":
{
if (radioButton1.Checked)
{
double res = Convert.ToDouble(textBox1.Text) * 2.20462262185;
textBox2.Text = Math.Round(res, 4).ToString();
}
if (radioButton2.Checked)
{
double res = Convert.ToDouble(textBox1.Text) / 2.20462262185;
textBox2.Text = Math.Round(res, 4).ToString();
}
break;
}
}
label5.Text = "";
}
else
{
label5.Text = "Plese enter numbers only.";
}
}
private void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
if(radioButton1.Checked)
{
label3.Text = "kg";
label4.Text = "lbs";
textBox2.Text = "";
}
}
private void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
if (radioButton2.Checked)
{
label3.Text = "lbs";
label4.Text = "kg";
textBox2.Text = "";
}
}
public bool isNum(string s)
{
return double.TryParse(s, out double i);
}
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e) //Combo Box Selector
{
switch (comboBox1.SelectedItem)
{
case "Mass":
{
label3.Text = "kg";
label4.Text = "lbs";
break;
}
case "Temperature":
{
label3.Text = "F";
label4.Text = "C";
break;
}
}
}
}
}

facing problem in c# calculator operator pressed it shows calculates result while pressing another operator

I am new to c# programming.I am building a simple calculator but when i add two numbers and press operator(+) button it adds two numbers but when i again press another operator it just adds the previous value like 2+2= 4 and if i press (-) operator after that it just shows result = 8 , please help me to get out of this.
thanks.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Calculator
{
public partial class Form1 : Form
{
double value = 0;
string operation = "";
bool opt_pressed = false;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if ((txtMain.Text == "0") || (opt_pressed))
txtMain.Clear();
Button a = (Button)sender;
opt_pressed = false;
if (a.Text == ".")
{
if (!txtMain.Text.Contains("."))
txtMain.Text = txtMain.Text + a.Text;
}
else
{
txtMain.Text = txtMain.Text + a.Text;
}
}
private void txtMain_TextChanged(object sender, EventArgs e)
{
}
private void button10_Click(object sender, EventArgs e)
{
txtMain.Clear();
value = 0;
label1.Text = "";
}
private void button11_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
if (value != 0)
{
equal.PerformClick();
operation = b.Text;
label1.Text = value + " " + operation;
opt_pressed = true;
}
else
{ operation = b.Text;
value = double.Parse(txtMain.Text);
label1.Text = value + " " + operation;
opt_pressed = true;
}
}
private void button16_Click(object sender, EventArgs e)
{
switch (operation)
{
case "+":
txtMain.Text = (value + double.Parse(txtMain.Text)).ToString();
break;
case "-":
txtMain.Text = (value - double.Parse(txtMain.Text)).ToString();
break;
case "/":
txtMain.Text = (value / double.Parse(txtMain.Text)).ToString();
break;
case "*":
txtMain.Text = (value * double.Parse(txtMain.Text)).ToString();
break;
}
value = double.Parse(txtMain.Text);
label1.Text = "";
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, KeyPressEventArgs e)
{
Button a = (Button)sender;
button1.PerformClick();
operation = a.Text;
}
}
}

How do I make my picture box move using the arrow keys?

I have created an animation using C# in Visual Studio, within a pictureBox using multiple images of pacman. I am now attempting to get the pictureBox to move around the form (up, down, right and left) by using the arrow keys but I cannot seem to get it working. Can anyone help me figure out why the pictureBox won't move?
Thanks
Here is my code so far:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Week9
{
public partial class Pacman : Form
{
private Image[] pacmanImage = new Image[4];
private int currentMouthPosition = 0;
private int xPosition = 0;
private int yPosition = 0;
// The index of the current frame.
private int FrameNum = 0;
public Pacman()
{
InitializeComponent();
this.KeyDown +=newSystem.Windows.Forms.KeyEventHandler(this.Pacman_KeyDown);
}
private void picFrame_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawImage(pacmanImage[currentMouthPosition], xPosition, yPosition, 32, 32);
}
// Load the images.
private void Pacman_Load(object sender, EventArgs e)
{
pacmanImage[0] = Image.FromFile("..\\..\\pac32_left_close.png");
pacmanImage[1] = Image.FromFile("..\\..\\pac32_left_open.png");
pacmanImage[2] = Image.FromFile("..\\..\\pac32_left_wide.png");
pacmanImage[3] = Image.FromFile("..\\..\\pac32_left_widest.png");
// Display the first frame.
picFrame.Image = pacmanImage[FrameNum];
}
// This is where I have tried to get the pictureBox to move
private void Pacman_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Left:
picFrame.Left -= 5;
break;
case Keys.Right:
picFrame.Left += 5;
break;
case Keys.Up:
picFrame.Top -= 5;
break;
case Keys.Down:
picFrame.Top += 5;
break;
}
}
// Display the next image.
private void tmrNextFrame_Tick(object sender, EventArgs e)
{
FrameNum = ++FrameNum % pacmanImage.Length;
picFrame.Image = pacmanImage[FrameNum];
}
private void btnStartStop_Click_1(object sender, EventArgs e)
{
tmrNextFrame.Enabled = !tmrNextFrame.Enabled;
if (tmrNextFrame.Enabled) btnStartStop.Text = "Stop";
else btnStartStop.Text = "Start";
}
}
}
private void Pacman_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Left:
mouthcurrentposition = 0;
picFrame.yPosition -= 10;
picFrame.Left(); //This should be a method that will draw your pacman
break;
case Keys.Right:
mouthcurrentposition = 1;
picFrame.yPosition += 10;
picFrame.Right();
break;
case Keys.Up:
mouthcurrentposition = 2;
picFrame.yPosition -= 10;
picFrame.Up();
break;
case Keys.Down:
mouthcurrentposition = 3;
picFrame.yPosition += 10;
picFrame.Down();
break;
}
}
pictureBox1.Location = new Point(x, y);
you can write some codes like this in your form key press event or keydown or keyup events. get the current picture box location and add up or Reduce the position.
you can use this code to get the current position :
pictureBox1.Location.X; //to get X
pictureBox1.Location.Y; // To get Y
I know i'm super hella late but what you missed was this.KeyDown += Pacman_KeyDown on your main form

C# Changing txtBox BackColor by Length

I want to write a simple "registration/login" program, just for me, just for fun.
I want to change the color of the TxtBox where a user types their name. When txtBox.Length<4 it should change its background to red.
I don't know why my code below isn't working. When I solidly change a text in txtBox properties to more than 5, it's blue at the start but doesn't change afterwards.
My code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _4Fun
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
if (regTxtBoxName.TextLength<4) {
regTxtBoxName.BackColor = Color.Red;
}
else{
regTxtBoxName.BackColor = Color.DarkBlue;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void regBtn_Click(object sender, EventArgs e)
{
if (regTxtBoxName.TextLength < 4)
{
txtBoxStatus.Text = "Choose a name with minimal length 5. "; // Urobit txtboxname a pass v registru Ĩervene pozadie ak x<4
}
else {
txtBoxStatus.Text = "Your account has been successfully created.";
string name = regTxtBoxName.Text;
}
if (regTxtBoxPass.TextLength < 4)
{
txtBoxStatus.Text = txtBoxStatus.Text + "Choose password with minimal length 5. ";
}
else {
txtBoxStatus.Text = "Your account has been successfully created.";
string pass = regTxtBoxPass.Text;
}
}
}
}
Your code is setting the color in the constructor of the form and then you don't change it. You need to register to TextChanged event on your TextBox to change the colour while your application is running based on how many characters there are in your Textbox.
Handle your text box TextChanged event, and place this code there, not in constructor:
if (regTxtBoxName.TextLength<4) {
regTxtBoxName.BackColor = Color.Red;
}
else{
regTxtBoxName.BackColor = Color.DarkBlue;
}
You can do this in Textbox TextChanged event.
Here is code
void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.TextLength<4)
{
textBox1.BackColor = Color.Red;
}
else
{
textBox1.BackColor = Color.DarkBlue;
}
}
When you enter a text in text box TextChanged event will call. check this link http://www.dotnetperls.com/textchanged
You may want to change the regBtn_Click method to regBtnTextChanged. By doing so, then the color of the textBox will change on run time.
So the code would be:
private void regBtnTextChanged(object sender, EventArgs e)
{
if (regTxtBoxName.TextLength<4) {
regTxtBoxName.BackColor = Color.Red;
}
else{
regTxtBoxName.BackColor = Color.DarkBlue;
}
}

Categories