private void rateTextBox_TextChanged(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(pieceTextBox.Text))
{
}
else if (string.IsNullOrEmpty(rateReturnTextBox.Text))
{
}
else
{
int piece = Convert.ToInt32(pieceTextBox.Text);
int rate = Convert.ToInt32(rateTextBox.Text);
int netTotal = piece * rate;
netTotalBillTextBox.Text = netTotal.ToString();
}
}
//why dose not show the multiplication answer....where is the mistake?
I want this answer in netTotalBillTextBox .
Apparently, in the second condition shouldn't compare rateTextBox?
I assume it will always return string.Empty in rateReturnTextBox.
Related
Good day lads!
I've got a question.
I think I'll be saving loads of text if you would see my form, so here we go!
Form:
private void Form1_Load(object sender, EventArgs e)
{
/*
Held h1 = new Held("Tank", Lanes.Top);
Held h2 = new Held("ADC", Lanes.Bot);
Held h3 = new Held("Support", Lanes.Bot);
listBox1.Items.Add(h1);
listBox1.Items.Add(h2);
listBox1.Items.Add(h3);
*/
//Data koppelen
cbRol.DataSource = Enum.GetValues(typeof(Lanes));
}
private void btnAanmaken_Click(object sender, EventArgs e)
{
int getal;
if (CheckEmptyFields())
{
MessageBox.Show("Vul alle velden in!");
}
else
{
if (CheckMovementSpeedIsInt(out getal))
{
string naamHero = tbNaamHero.Text;
Lanes lane = ???
int mSpeedHero = getal;
Held nieuwHeld = new Held(naamHero, lane, getal);
}
}
}
private bool CheckMovementSpeedIsInt(out int getal)
{
return Int32.TryParse(tbMoveSpeed.Text, out getal);
}
private bool CheckEmptyFields()
{
return tbNaamHero.Text == null || tbMoveSpeed.Text == null || cbRol.SelectedItem == null;
}
Held:
class Held
{
private string Naam;
private Lanes Lane;
int MSpeed;
public Held(string aNaam, Lanes aLane, int aMSpeed)
{
this.Naam = aNaam;
this.Lane = aLane;
this.MSpeed = aMSpeed;
}
public override string ToString()
{
return this.Naam + " " + this.Lane.ToString();
}
}
}
Lanes:
enum Lanes
{
Top,
Mid,
Bot,
Jungle
}
Alright! So as you can see I have combined the enum with the ComboBox. I'd like to put the selected value (when the user has pressed the button "Aanmaken/Create") in the instance.
How am I able to convert the object (from ComboBox) to a type (Lanes)?
If I haven't clarified enough, just give me a heads up!
PS: The "???" in the code is the place where I'm not sure what to put since that's the question hehe.
Just use the following:
Lanes lane = (Lanes)cbRol.SelectedIndex;
This works due to enum is typeof int, so your Top is actually 0, and so on...
You can parse your Enum.Parse
Lanes lange = (Lanes) Enum.Parse(typeof(Lanes), cbRol.SelectedItem.ToString(), true);
This also works for index
Lanes lange = (Lanes) Enum.Parse(typeof(Lanes), cbRol.SelectedIndex.ToString(), true);
I'm trying to build a exam grader using C#. I'm new to this and don't know very much. What code would I use to add min and max buttons and to add a label stating whether it's a min or max?
private void btnAdd_Click(object sender, EventArgs e)
{
int points;
try
{
points = int.Parse(txtPoints.Text);
lstPoints.Items.Add(points);
txtPoints.Clear();
txtPoints.Focus();
if (lstPoints.Items.Count == 12)
{
txtPoints.Enabled = false;
btnAdd.Enabled = false;
}
if (lblResult.Text != "")
{
lblResult.Text = "";
}
}
catch
{
MessageBox.Show("Please enter only whole numbers");
txtPoints.Clear();
txtPoints.Focus();
}
}
private void btnAvg_Click(object sender, EventArgs e)
{
double total = 0;
for (int i = 0; i < lstPoints.Items.Count; i++)
{
total += (int)lstPoints.Items[i];
}
total /= lstPoints.Items.Count;
lblResult.Text = total.ToString();
}
private void btnClear_Click(object sender, EventArgs e)
{
lstPoints.Items.Clear();
txtPoints.Enabled = true;
btnAdd.Enabled = true;
}
}
}
hope this works
private void getMax()
{
int max=0;
for (int i = 0; i < lstPoints.Items.Count; i++)
{
if(max<(int)lstPoints.Items[i])
{
max=(int)lstPoints.Items[i];
}
}
lblResult.Text = max.ToString();
}
}
private void getMin()
{
int min=(int)lstPoints.Items[0];
for (int i = 1; i < lstPoints.Items.Count; i++)
{
if(min>(int)lstPoints.Items[i])
{
min=(int)lstPoints.Items[i];
}
}
lblResult.Text = min.ToString();
}
}
There are two possiblities as I see:
1) When you are writing this:
lstPoints.Items.Add(points);
Instead of adding to List(Of Integer) use SortedList. So the
list will always have the sorted result sets.
2) Use Array.Sort() to sort the records.
Once you have sorted records the first one is the minimum and the last one is the maximum (Assuming sorted in ascending order).
Take out two buttons and placed on the form, set Text Property from property window to Min and Max respectively and in event handler handle the Click event and pick the relevant resultset from lstPoints array.
Hope it helps!
private void txtrate_TextChanged(object sender, EventArgs e)
{
int a = Convert.ToInt32(txtqty.Text);
int b = Convert.ToInt32(txtrate.Text);
int c = a * b;
txttotal.Text = Convert.ToString(c);
//txttotal.Text = (Convert.ToInt32(txtqty.Text) * Convert.ToInt32(txtrate.Text)).ToString();
}
Well... this error occurs of txtqty.Text (or txtrate.Text for variable b) does not contain a valid number. So the user entered letters or other characters instead of 0 till 9.
You could try to validate your input first with TryParse:
private void txtrate_TextChanged(object sender, EventArgs e)
{
int a = Convert.ToInt32(txtqty.Text);
int b = Convert.ToInt32(txtrate.Text);
if (Int32.TryParse(txtqty.Text, out a) && (Int32.TryParse(txtrate.Text, out b)
{
int c = a * b;
txttotal.Text = c.ToString();
}
}
Yes, this is due to the value not contained only numbers, to avoid this situation, please setup the type of data you are trying to collect with the text fields, you can setup refer this post for a detailed walk through on how to do it.
Yes, I would suggest TryParsetoo :
private void txtrate_TextChanged(object sender, EventArgs e)
{
int number;
bool result = Int32.TryParse(txtqty.Text, out number);
if (result)
{
//Converting is Ok, proceed
}
else
{
//Convertign fail
txtqty.Focus();
MessageBox.Show("Please enter only number for this field");
return;
}
}
I would suggest making this for each textbox with setting the focus to the textbox with incorrect input and providing an useful message for the user. In my opinion this is a suitable and user-friendly way.
I have this program that I am working on for class, I think the problem is in my if statements. When I run the program and make my selection and click the total button and I get this as a display "for your appetizer, for your entree, and for dessert" in the order label and the price for the steak dinner in the order total label. I think I may have to use switch statements, but I'm not sure any suggestions would be of great help, thanks.
namespace Restaurant
{
public partial class frmRestaurant : Form
{
decimal AppetizerPrice = 0.0m;
decimal EntreePrice = 0.0m;
decimal DessertPrice = 0.0m;
decimal total = 0.0m;
string AppetizerOrder = "", EntreeOrder = "", DessertOrder = "", order = "";
public frmRestaurant()
{
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btnTotal_Click(object sender, EventArgs e)
{
CalculateTotal();
Order();
lblOrder.Text = order;
lblTotal.Text = total.ToString();
}
private void grpAppetizer_Enter(object sender, EventArgs e)
{
if (radCheeseSticks.Checked)
{
AppetizerPrice = 5.99m;
AppetizerOrder = "Cheese Sticks";
}
else if (radGarlicBread.Checked)
{
AppetizerPrice = 4.50m;
AppetizerOrder = "Garlic Bread";
}
else if (radChipsnSalsa.Checked)
{
AppetizerPrice = 3.50m;
AppetizerOrder = "Chips and Salsa";
}
}
private void grpEntree_Enter(object sender, EventArgs e)
{
if (radSteakDinner.Checked)
{
EntreePrice = 12.50m;
EntreeOrder = "Steak Dinner";
}
else if (radChickenParm.Checked)
{
EntreePrice = 10.99m;
EntreeOrder = "Chicken Parmigiana";
}
else if (radChipsnSalsa.Checked)
{
EntreePrice = 3.50m;
EntreeOrder = "Chips and Salsa";
}
}
private void grpDessert_Enter(object sender, EventArgs e)
{
if (radSteakDinner.Checked)
{
DessertPrice = 12.50m;
DessertOrder = "Steak Dinner";
}
else if (radChickenParm.Checked)
{
DessertPrice = 10.99m;
DessertOrder = "Chicken Parmigiana";
}
else if (radChipsnSalsa.Checked)
{
DessertPrice = 3.50m;
DessertOrder = "Chips and Salsa";
}
}
public decimal CalculateTotal()
{
total = AppetizerPrice + EntreePrice + DessertPrice;
return total;
}
public string Order()
{
order = AppetizerOrder + "for your appetizer," + EntreeOrder + "for your entree, and " + DessertOrder + "for dessert";
return order;
}
}
}
I think the GroupBox.Enter event is not useful for your use case. The enter event is invoked sometime during the control activation, but not when the value is changed.
One way to fix your problem is to set the appetizer/entree/dessert price and text only when the "Total" button is clicked. You do not need it before that in the current form right now.
Another way to fix it is to use the correct event: Just handle the RadioButton.CheckedChanged event for all of those radio buttons, for example:
private void radGarlicBread_CheckedChanged(object sender, EventArgs e)
{
if (radGarlicBread.Checked)
{
AppetizerPrice = 4.50m;
AppetizerOrder = "Garlic Bread";
}
}
First of all, you probably want to use String.Format(), because it'll look a bit cleaner than a bunch of concatenation--it'll also help you catch things like the lack of spaces here (your output seems like it would be, e.g. 'Chips and Salsafor your appetizer...')
It might also be better to just find whichever item is checked in the Order method rather than update it every time the user checks.
I'm not sure what is wrong, but maybe you can do a Debug.WriteLine() every time each of the Enter() methods is called to see what is going on.
For example:
Debug.WriteLine("grpDesert_Enter");
Debug.WriteLine(radSteakDinner.Checked);
Debug.WriteLine(radChickenPark.Checked);
Debug.WriteLine(radChipsnSalsa.Checked);
I want to add "," to after every group of 3 digits. Eg : when I type 3000000 the textbox will display 3,000,000 but the value still is 3000000.
I tried to use maskedtexbox, there is a drawback that the maskedtexbox displayed a number like _,__,__ .
Try adding this code to KeyUp event handler of your TextBox
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text))
{
System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US");
int valueBefore = Int32.Parse(textBox1.Text, System.Globalization.NumberStyles.AllowThousands);
textBox1.Text = String.Format(culture, "{0:N0}", valueBefore);
textBox1.Select(textBox1.Text.Length, 0);
}
}
Yes, it will change the value stored in a texbox, but whenever you need the actual number you can use the following line to get it from the text:
int integerValue = Int32.Parse(textBox1.Text, System.Globalization.NumberStyles.AllowThousands);
Of course do not forget to check that what the user inputs into the textbox is actually a valid integer number.
Use String.Format
int value = 300000
String.Format("{0:#,###0}", value);
// will return 300,000
http://msdn.microsoft.com/en-us/library/system.string.format.aspx
This may work fine for your scenario I hope.
private string text
{
get
{
return text;
}
set
{
try
{
string temp = string.Empty;
for (int i = 0; i < value.Length; i++)
{
int p = (int)value[i];
if (p >= 48 && p <= 57)
{
temp += value[i];
}
}
value = temp;
myTxt.Text = value;
}
catch
{
}
}
}
private void digitTextBox1_TextChanged(object sender, EventArgs e)
{
if (myTxt.Text == "")
return;
int n = myTxt.SelectionStart;
decimal text = Convert.ToDecimal(myTxt.Text);
myTxt.Text = String.Format("{0:#,###0}", text);
myTxt.SelectionStart = n + 1;
}
Here, myTxt = your Textbox. Set Textchanged event as given below and create a property text as in the post.
Hope it helps.
You could hook up to OnKeyUp event like this:
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (!(e.KeyCode == Keys.Back))
{
string text = textBox1.Text.Replace(",", "");
if (text.Length % 3 == 0)
{
textBox1.Text += ",";
textBox1.SelectionStart = textBox1.Text.Length;
}
}
}
Get Decimal Value Then set
DecimalValue.ToString("#,#");