I am wondering what I am doing wrong, as I am trying to make a menu, in which it needs to calculate three different courses together in predefined values, however I seem to be doing the conversions wrong, as I cant seem to make it work right.
int a, b, c;
textBox4.Text = (a + b + c).ToString();
if (comboBox1.Text == "Tzatziki")
{
a = 35;
}
else if (comboBox1.Text == "Carpaccio")
{
a = 40;
}
else if (comboBox1.Text == "Bruscetta")
{
a = 30;
}
else if (comboBox1.Text == "Shrimp Cocktail")
{
a = 40;
}
if (comboBox2.Text == "Sirloin")
{
b = 225;
}
else if (comboBox2.Text == "Lamb")
{
b = 195;
}
else if (comboBox2.Text == "Salmon")
{
b = 170;
}
else if (comboBox2.Text == "Veggy")
{
b = 100;
}
if (comboBox1.Text == "Chocolate Cake")
{
c = 45;
}
else if (comboBox1.Text == "Pancakes")
{
c = 35;
}
else if (comboBox1.Text == "Waffles")
{
c = 40;
}
else if (comboBox1.Text == "Sundae")
{
c = 38;
}
}
Would appreciate the advice, kinda new to C# programming :)
Give value to your variables and initialize them:
int a = 0, b = 0, c = 0;
And I think you need to move this line end of your if / else-if statements:
textBox4.Text = (a + b + c).ToString();
Before you can convert an integer, to its textual counterpart, you need to give it an initial value.
The reason why you get zero is because after you initialize variables (with zero), you immediately use them. In that sence the if-then-else patterns are useless.
A possible fix is to use switch-case patterns to ease coding and after you calculated a, b and c, set the text value:
int a = 0, b = 0, c = 0;
switch(comboBox1.Text) {
case "Tzatziki" :
a = 35;
break;
case "Carpaccio" :
a = 40;
break;
case "Bruscetta" :
a = 30;
break;
case "Shrimp Cocktail" :
a = 40;
break;
}
switch(comboBox2.Text) {
case "Sirloin" :
b = 225;
break;
case "Lamb" :
b = 195;
break;
case "Salmon" :
b = 170;
break;
case "Veggy" :
b = 100;
break;
}
switch(comboBox3.Text) {
case "Chocolate Cake" :
c = 45;
break;
case "Pancakes" :
c = 35;
break;
case "Waffles" :
c = 40;
break;
case "Sundae" :
c = 38;
break;
}
textBox4.Text = (a + b + c).ToString();
You probably made even a mistake for c, since you used ComboBox1 again (I expect this should be ComboBox3. By using a switch-case structure, the code becomes more elegant.
A final problem with the code is what to do if no value has been entered in the combobox (or a different one from what you have checked). In the case I wrote above, the values will remain 0 (only the values corresponding to the ComboBox that has an unknown value). You can add a defaut : case in order to decide what to do then.
Related
I am trying to make a slot machine game in c# where if all 3 slots match, the user wins 2x their bet and if 2 of the slots match, then the user wins their bet back and if none of the slots match then the user losses their bet but sometimes when there are 2 slots that match, the user doesnt win their money back and sometimes when there are no slots that match, the user wins their money back and sometimes when the user gets 3 matches, they win their money back but not double. How do I also make it so that if all 3 slots have a jackpot image, the user wins 5x their bet
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlTypes;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace Slot_Machine
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Random rnd = new Random();
int a, b, c, move, wins,losses, bid;
int balance = 100;
private void QuitBtn_Click(object sender, EventArgs e)
{
this.Close();
}
private void ResetBtn_Click(object sender, EventArgs e)
{
Application.Restart();
}
void Before_Game_Result()
{
bid = Convert.ToInt32(BidAmountTxt.Text);
balance = balance - bid;
BalanceLbl.Text = "Balance: $" + Convert.ToString(balance);
}
void Game_Result()
{
if (System.Convert.ToInt32(a) == b && System.Convert.ToInt32(b) == c)
{
wins++;
WinLbl.Text = "Wins: " + wins;
bid = Convert.ToInt32(BidAmountTxt.Text);
balance = balance + (bid * 2);
BalanceLbl.Text = "Balance: $" + Convert.ToString(balance);
BidBtn.Enabled = true;
BidAmountTxt.Enabled = true;
BidAmountTxt.BackColor = Color.White;
}
else
{
if ((System.Convert.ToInt32(a) == b && System.Convert.ToInt32(b) != c) || (System.Convert.ToInt32(a) == c && System.Convert.ToInt32(b) != c) ||
(System.Convert.ToInt32(b) == c && System.Convert.ToInt32(a) != c))
{
wins++;
WinLbl.Text = "Wins: " + wins;
bid = Convert.ToInt32(BidAmountTxt.Text);
balance = balance + bid;
BalanceLbl.Text = "Balance: $" + Convert.ToString(balance);
BidBtn.Enabled = true;
BidAmountTxt.Enabled = true;
BidAmountTxt.BackColor = Color.White;
}
else
{
losses++;
LossesLbl.Text = "Losses: " + losses;
BalanceLbl.Text = "Balance: $" + Convert.ToString(balance);
BidBtn.Enabled = true;
BidAmountTxt.Enabled = true;
BidAmountTxt.BackColor = Color.White;
}
}
if (balance <= 0)
{
MessageBox.Show("You don't have any money!! SCRAM");
this.Close();
}
}
private void BidBtn_Click(object sender, EventArgs e)
{
if(BidAmountTxt.Text =="")
{
MessageBox.Show("input a bid first!!");
}
else
{
int x = Convert.ToInt32(BidAmountTxt.Text);
bool success = false;
if(x <= balance)
{
success = true;
}
if(success)
{
Before_Game_Result();
timer1.Enabled = true;
BidAmountTxt.Enabled = false;
BidBtn.Enabled = false;
BidAmountTxt.BackColor = Color.Black;
}
else
{
MessageBox.Show("INVALID - please enter a bid lower or equal to your balance");
BidAmountTxt.Clear();
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
move++;
if (move < 30)
{
a = rnd.Next(5);
b = rnd.Next(5);
c = rnd.Next(5);
switch(a)
{
case 0:
Slot1.Image = Properties.Resources.basketball3;
break;
case 1:
Slot1.Image = Properties.Resources.soccer_ball2;
break;
case 2:
Slot1.Image = Properties.Resources.volleyball;
break;
case 3:
Slot1.Image = Properties.Resources.hockey_puck;
break;
case 4:
Slot1.Image = Properties.Resources.Jackpot;
break;
}
switch (b)
{
case 0:
Slot2.Image = Properties.Resources.basketball3;
break;
case 1:
Slot2.Image = Properties.Resources.soccer_ball2;
break;
case 2:
Slot2.Image = Properties.Resources.volleyball;
break;
case 3:
Slot2.Image = Properties.Resources.hockey_puck;
break;
case 4:
Slot2.Image = Properties.Resources.Jackpot;
break;
}
switch (c)
{
case 0:
Slot3.Image = Properties.Resources.basketball3;
break;
case 1:
Slot3.Image = Properties.Resources.soccer_ball2;
break;
case 2:
Slot3.Image = Properties.Resources.volleyball;
break;
case 3:
Slot3.Image = Properties.Resources.hockey_puck;
break;
case 4:
Slot3.Image = Properties.Resources.Jackpot;
break;
}
}
else
{
timer1.Enabled = false;
move = 0;
Game_Result();
}
}
}
}
The problem is probably you are ignoring one valid return value of rnd.Next(5). Ie, you ignore, that Next may also return 0. Thus, if for one of a, b or c you have a value of 0, you don't change the image. Thus on the screen you may see a winning situation where there is none or vice versa ...
So change your switches for a, b and c to
switch(a) {
case 0: ...
case 1: ...
case 2: ...
...
}
to also deal with a value of 0. And either introduce an additional symbol or change your random function to rnd.Next(4) to return values from 0..3
But there are also some other remarks about your code
System.Convert.ToInt32(a) == b does not make any sense, as all of a, b and c are already integers. You can simply compare if (a == b)
When checking your "2-matches" winning situation (System.Convert.ToInt32(a) == b && System.Convert.ToInt32(b) != c) you don't need to check the inequality of b and c because at this position in the code you already know, that not all three values are equal (because otherwise your code would have entered the a==b && b==c branch. Thus if a == b, c must be different. No need for checking it again. Same of course for all other "2-matches" checks.
i want make math operation with one button and textbox , when type on textbox operation like 25+5+-10+7 and press on button show me result
i am trying do this but i can not.
in this code i make for each char in textbox and i want store 25 in firstvalue parameter and + in op parameter and 5 in secondvalue parameter
and make operation 25+5=30 and store it in firstvalue parameter and store +- in op , 10 in secondvalue and make operation ,store it in firstvalue ....and so on until length of string .
but i do not know where and how store second value
private void button1_Click(object sender, EventArgs e)
{
string allValue = textBox1.Text;
int firstValue=0;
int secondValue=0;
string first = string.Empty;
string op = string.Empty;
foreach (char item in allValue)
{
if (char.IsNumber(item))
{
first += item;
}
else if(item=='+' || item=='-' || item=='*' || item=='/')
{
firstValue = Int32.Parse(first);
op += item;
first = "";
switch (op)
{
case "+":
firstValue = firstValue + secondValue;
break;
case "-":
firstValue = firstValue - secondValue;
break;
case "+-":
firstValue = firstValue +- secondValue;
break;
case "*":
firstValue = firstValue * secondValue;
break;
case "/":
firstValue = firstValue / secondValue;
break;
}
}
}
MessageBox.Show(firstValue.ToString());
}
If you want to parse the string from beginning to end and do the calculations during the parsing, then you can't do the calculation between two values until you have both operands. That means that you have to wait until you find the second operator (or the end of the string) until you can do the calculation for the first operator.
When you parse the number, put that in the secondValue variable. If it's the first value then just move it to firstValue and continue to get another value, otherwise do the calculation with the two values that you have according to the previous operator.
To include the last calculation you can loop one step more than the last character in the string. That allows your loop to run one more time after the last value, and you can do that last calculation. It requires a few more checks though, so that you don't actually try to read from that position outside the string.
To handle negative values you should include that in the value, not using a +- operator. Otherwise you would also need --, *- and /- operators.
Example (not tested):
private void button1_Click(object sender, EventArgs e) {
string allValue = textBox1.Text;
int firstValue = 0;
int secondValue = 0;
string second = string.Empty;
string op = string.Empty;
for (int i = 0; i <= allValue.Length; i++) {
if (i < allValue.Length && (Char.IsNumber(allValue[i]) || (char == '-' && second.Length == 0))) {
second += allValue[i];
} else if (i == allValue.Length || "+-*/".indexOf(allValue[i]) != -1) {
secondValue = Int32.Parse(second);
if (op.Length > 0) {
switch (op) {
case "+": firstValue += secondValue; break;
case "-": firstValue -= secondValue; break;
case "*": firstValue *= secondValue; break;
case "/": firstValue /= secondValue; break;
}
} else {
firstValue = secondValue;
}
if (i < allValue.Length) {
op = allValue[i].ToString();
}
second = "";
} else {
// illegal formula
}
}
MessageBox.Show(firstValue.ToString());
}
Note: This calculates strictly from left to right and does not handle priority, e.g. 1+2*3 is evaluated as (1+2)*3, not the 1+(2*3) that is normally used.
You can use the NCalc library. You would use it like this:
NCalc.Expression e = new NCalc.Expression(textBox1.Text);
double value = e.Evaluate();
// Next, do something with your evaluated value.
Would this work for you? As mentioned in earlier answers this does not pay regard to the order of operations and there is a lot of other error checking that should likely be done.
using System.Text.RegularExpressions;
private void button1_Click(object sender, EventArgs e)
{
MatchCollection values = Regex.Matches(textBox1.Text, "[0-9]+");
MatchCollection operations = Regex.Matches(textBox1.Text, #"[\/\*\+-]");
if (values.Count == operations.Count + 1)
{
int total = int.Parse(values[0].Value);
if (operations.Count > 0)
{
for (int index = 1; index < values.Count; index++)
{
int value = int.Parse(values[index].Value);
switch (operations[index - 1].Value)
{
case "+":
total += value;
break;
case "-":
total -= value;
break;
case "/":
total /= value;
break;
case "*":
total *= value;
break;
}
}
}
MessageBox.Show(total.ToString());
}
}
Doing all the things from scratch is not easy. In my simplified answer I'll presume your operators are only +-*/ (including their precedence).
Reference: How to evaluate an infix expression in just one scan using stacks?
First you should parse the input string... My way would be
public enum TokenType
{
Operator,
Operand
}
public class Token
{
public string Value { get; set; }
public TokenType Type { get; set; }
public override string ToString()
{
return Type + " " + Value;
}
}
IEnumerable<Token> Parse(string input)
{
return Regex.Matches(input, #"(?<value>\d+)|(?<type>[\+\-\*\/\(\)])").Cast<Match>()
.Select(m => m.Groups["value"].Success
? new Token() { Value = m.Groups["value"].Value, Type = TokenType.Operand }
: new Token() { Value = m.Groups["type"].Value, Type = TokenType.Operator });
}
This Parse method will return all the operators and operands in your expression.
The next step would be to evaluate the expression using a stack
Token Exec(Stack<Token> operatorStack, Stack<Token> operandStack)
{
var op = operatorStack.Pop();
var t1 = operandStack.Pop();
var t2 = operandStack.Pop();
switch (op.Value)
{
case "+": return new Token() { Value = (int.Parse(t1.Value) + int.Parse(t2.Value)).ToString(), Type = TokenType.Operand };
case "-": return new Token() { Value = (int.Parse(t2.Value) - int.Parse(t1.Value)).ToString(), Type = TokenType.Operand };
case "*": return new Token() { Value = (int.Parse(t1.Value) * int.Parse(t2.Value)).ToString(), Type = TokenType.Operand };
case "/": return new Token() { Value = (int.Parse(t2.Value) / int.Parse(t1.Value)).ToString(), Type = TokenType.Operand };
}
return null;
}
int Eval(string input)
{
var tokens = Parse(input);
var operatorStack = new Stack<Token>();
var operandStack = new Stack<Token>();
var precedence = new Dictionary<string, int>() { { "+", 0 }, { "-", 0 }, { "*", 1 }, { "/", 1 } };
foreach (var token in tokens)
{
if (token.Type == TokenType.Operand) operandStack.Push(token);
if (token.Type == TokenType.Operator)
{
while (operatorStack.Count > 0 && precedence[operatorStack.Peek().Value] >= precedence[token.Value])
{
operandStack.Push(Exec(operatorStack, operandStack));
}
operatorStack.Push(token);
}
}
while(operatorStack.Count>0)
{
operandStack.Push(Exec(operatorStack, operandStack));
}
return int.Parse(operandStack.Pop().Value);
}
You can test your code now.
int r1 = Eval("25 - 5");
int r2 = Eval("25 - 5*2");
int r3 = Eval("25 - 5 + 3*2");
int r4 = Eval("25/5 + 7*3");
int r5 = Eval("25/5 + 7*3 + 2");
int r6 = Eval("25/5 + 7*3 * 2");
I want to assign a data to an int volunteerEducation if I Checked radio button to save it to the database!
int volunteerEducation;
switch (volunteerEducation)
case radioButton9.Checked:
volunteerEducation= 9;
break;
case radioButton10.Checked:
volunteerEducation = 10;
break;
try this...
foreach(Control c in this.Controls)
{
if(c is RadioButton)
{
RadioButton rbtn = (RadioButton)c;
// now here you can use if else statements or Switch Statement
}
}
switch allows you to check for one of several values on a single variable. if statements allow arbitrary conditions, which can check a single variable or many. Hence, switch is unsuited for what you are trying to do.
There are a few obvious options:
Use a series of if ... else if statements
Use some fancier construct such as a lookup dictionary mapping to methods (Dictionary<RadioButton,Func<>> might be a good place to start), though if you are unfamiliar with such constructs as switch I'd strongly recommend against that (it's the old "you have to learn to walk before you start to run")
Iterate over the radio buttons and do your magic inside a loop
Such a loop would look like:
foreach (Control c in this.Controls)
{
RadioButton rbtn = c as RadioButton;
if(rbtn != null)
{
// ... your code to work with 'rbtn' goes here ...
if (rbtn.Checked)
{
// ...
}
}
}
Using as rather than is followed by an explicit cast is idiomatic, and also saves you a cast; null checking is almost certainly faster, and certainly not slower, than casting. The as operator gives you null if the given value cannot be cast to the given type. In the case where the value of the variable you are working can be changed from another thread (not likely in this case, but possible in other cases), it also saves you the headache of a nigh-impossible-to-reproduce bug when something changes the value of the variable out under your feet.
A set of if statements to do the same thing would be along the lines of
if (radioButton9.Checked)
{
// do something
}
else if (radioButton10.Checked)
{
// do something
}
Using else if mimics the behavior of a switch ... case ... break construct. If they are independent, simply omit the else. (Then, I'd suggest adding a blank line between the end of the if statement-block and the next if statement, for readability.)
I found the Answer and it's not really different from thus answers.
the Point is to declare int. e.g int volunteerEducation = 0; then you have to use if ... else if.
private void updateButton_Click(object sender, EventArgs e)
{
try
{
string volunteerID = updtIDTextBox.Text;
string volunteerName = updtNameTextBox.Text;
string volunteerZone = updtZoneTextBox.Text;
string volunteerStreet = updtStreetTextBox.Text;
int volunteerSex = updtMaleRadioButton.Checked ? 0 : 1;
DateTime volunteerBirthday = updtBirthdayDateTimePicker.Value;
if (updtHomePhoneTextBox.Text == "")
updtHomePhoneTextBox.Text = "0";
int volunteerHomePhone = int.Parse(updtHomePhoneTextBox.Text);
if (updtWorkPhoneTextBox.Text == "")
updtWorkPhoneTextBox.Text = "0";
int volunteerWorkPhone = int.Parse(updtWorkPhoneTextBox.Text);
if (updtMobile1TextBox.Text == "")
updtMobile1TextBox.Text = "0";
int volunteerMobile1 = int.Parse(updtMobile1TextBox.Text);
if (updtMobile2TextBox.Text == "")
updtMobile2TextBox.Text = "0";
int volunteerMobile2 = int.Parse(updtMobile2TextBox.Text);
string volunteerEmail = updtEmailTextBox.Text;
string volunteerJob = updtJobTextBox.Text;
string volunteerAffiliation = updtAffiliationTextBox.Text;
//The solution start from here
int volunteerEducation = 0;
if (radioButton10.Checked)
volunteerEducation = 1;
else if (radioButton11.Checked)
volunteerEducation = 2;
else if (radioButton12.Checked)
volunteerEducation = 3;
else if (radioButton14.Checked)
volunteerEducation = 5;
else if (radioButton15.Checked)
volunteerEducation = 6;
else if (radioButton16.Checked)
volunteerEducation = 7;
else if (radioButton17.Checked)
volunteerEducation = 8;
else if (radioButton18.Checked)
volunteerEducation = 9;
//end solution
string volunteerEducationPlace = addEducationPlaceTextBox.Text;
string volunteerEducationDepartmen = addEducationDepartmentTextBox.Text;
//same above
int volunteerInteresting = 0;
if (updtIntrstMdcnRadioButton.Checked)
volunteerInteresting = 1;
else if (updtIntrstSosclRadioButton.Checked)
volunteerInteresting = 2;
else if (updtIntrstLrnRadioButton.Checked)
volunteerInteresting = 3;
else if (updtIntrstTrnRadioButton.Checked)
volunteerInteresting = 4;
//end
string volunteerNotes = addNotesTextBox.Text;
int x = dataGridViewX1.SelectedRows[0].Index;
UpdateVolunteer(volunteerID, volunteerName, volunteerZone, volunteerStreet, volunteerSex, volunteerBirthday, volunteerHomePhone, volunteerWorkPhone, volunteerMobile1, volunteerMobile2, volunteerEmail, volunteerJob, volunteerAffiliation, volunteerEducation, volunteerEducationPlace, volunteerEducationDepartmen, volunteerInteresting, volunteerNotes);
showVolunteers(x);
updtIDTextBox.Text = "";
updtNameTextBox.Text = "";
updtZoneTextBox.Text = "";
updtStreetTextBox.Text = "";
updtBirthdayDateTimePicker.Value = DateTime.Now;
updtHomePhoneTextBox.Text = "";
updtWorkPhoneTextBox.Text = "";
updtMobile1TextBox.Text = "";
updtMobile2TextBox.Text = "";
updtEmailTextBox.Text = "";
updtJobTextBox.Text = "";
updtAffiliationTextBox.Text = "";
updtEducationPlaceTextBox.Text = "";
updtEducationDepartmentTextBox.Text = "";
updtNotesTextBox.Text = "";
}
catch (SqlException sql)
{
MessageBoxEx.Show(sql.Message);
}
}
I'm geting a StackOverflowException. Somehow, posting here seemed appropriate.
I'm using Windows Forms in a C# application. This application is intended to run on Linux, FreeBSD and Mac-OS, so I can't use WPF, so please don't suggest it.
My guess is that I'm missing a nuance of WinForms, but I cant seem to figure out what.
The ComboBox is generated by the GUI form builder in VS 2010.
The specific lines of code that are throwing the error are here:
if(cur_num_is_valid)
{
cbx_material_num.Text = num;
}
else
{
num = "0";
//I only have one of the following two at a time. Both overflow
cbx_material_num.SelectedIndex = 0;
cbx_material_num.Text = "0";
}
Since the code is somewhat complex, here's the whole function code. 'cbx_' indicates a combo box. 'txtb_' is a text box.
private void cbx_material_numobj_SelectedIndexChanged(object sender, EventArgs e)
{
string obj = cbx_material_obj.Text;
string num = cbx_material_num.Text;
int selnum = 0;
int n = 0;
//do we need to recreate the numbers array?
bool cur_num_is_valid = false;
cbx_material_num.Items.Clear();
if(obj != lastobj)
{
n = m_demo.get_object_modifiers(obj);
for(int i = 0; i <= n; i++)
{
string s = i.ToString();
if(s == num && i < n) cur_num_is_valid = true;
cbx_material_num.Items.Add(s);
}
}
if(cur_num_is_valid)
{
cbx_material_num.Text = num;
}
else
{
num = "0";
//Overflow here:
cbx_material_num.SelectedIndex = 0;
}
try
{
selnum = int.Parse(num);
}
catch(Exception)
{
MessageBox.Show("Error, second select menu after 'object modifiers' must be a number, not '"+num+"'.");
cbx_material_num.Text="0";
return;
}
if(selnum >= n)
{
txtb_material_param1.Text = "0";
txtb_material_param2.Text = "0";
txtb_material_param3.Text = "0";
txtb_material_param4.Text = "0";
}
else
{
MaterialFace face;
MaterialParameter parameter;
int typeid;
object paramdata;
m_demo.get_object_modifiers_material(obj, selnum, out face, out parameter, out typeid, out paramdata);
cbx_material_face.Text = face.ToString();
cbx_material_paramtype.Text = parameter.ToString();
switch(typeid)
{
case 0:
txtb_material_param1.Text = ((float)paramdata).ToString();
cbx_material_datatype.Text = "float";
goto case -1;
case 1:
float[] parsf = ((float[])paramdata);
txtb_material_param1.Text = parsf[0].ToString();
txtb_material_param2.Text = parsf[1].ToString();
txtb_material_param3.Text = parsf[2].ToString();
txtb_material_param4.Text = parsf[3].ToString();
cbx_material_datatype.Text = "float[]";
break;
case 2:
txtb_material_param1.Text = ((int)paramdata).ToString();
cbx_material_datatype.Text = "int";
goto case -1;
case 3:
int[] parsi = ((int[])paramdata);
txtb_material_param1.Text = parsi[0].ToString();
txtb_material_param2.Text = parsi[1].ToString();
txtb_material_param3.Text = parsi[2].ToString();
txtb_material_param4.Text = parsi[3].ToString();
cbx_material_datatype.Text = "int[]";
break;
case -1: //can't actuall be returned, used to 'blank' the last three as '0'
txtb_material_param2.Text = "0";
txtb_material_param2.Text = "0";
txtb_material_param3.Text = "0";
break;
case 4:
OpenTK.Graphics.Color4 paramc = ((OpenTK.Graphics.Color4)paramdata);
txtb_material_param1.Text = paramc.R.ToString();
txtb_material_param2.Text = paramc.G.ToString();
txtb_material_param3.Text = paramc.B.ToString();
txtb_material_param4.Text = paramc.A.ToString();
cbx_material_datatype.Text = "Color4";
break;
default: //5
Vector4 paramv = ((Vector4)paramdata);
txtb_material_param1.Text = paramv.X.ToString();
txtb_material_param2.Text = paramv.Y.ToString();
txtb_material_param3.Text = paramv.Z.ToString();
txtb_material_param4.Text = paramv.W.ToString();
cbx_material_datatype.Text = "Vector4";
break;
}
}
}
You need to check that the SelectedIndex isn't already 0 before you try to set it:
if (cbx_material_num.SelectedIndex != 0){
cbx_material_num.SelectedIndex = 0;
}
Otherwise you're re-firing the event every time through.
I think that whenever you set this cbx_material_num.SelectedIndex = 0; within the EventHandler you invoke your
cbx_material_numobj_SelectedIndexChanged(object sender, EventArgs e)
Each call invokes another eventHandler so the stack fills up after some time.
Basically, the fact that it is called SelectedIndexChanged doesn't mean that the value has to be different from the previous one but that the value is set through its setter.
i realize that it does not read inside if (berjaya[23].Equals(70)) as if it was not equal to 70. but when i tried to show berjaya[23] using MessageBox, it do appear 70.
my first guess is casting. i tried int value = (int)(berjaya[23]); and my next plan is try do if(value == 70) but it says string cannot be convert to int.
is there any other way for the (berjaya[23].Equals(70)) be read?
===EDIT===
i should casting the data split by this way:
int.TryParse(berjaya[23], out value1);
then to change the picture, i used this:
if (value1 == 301)
{
Bitmap abc = (Bitmap)System.Drawing.Bitmap.FromFile("C:\\Users\\HDAdmin\\Pictures\\HospitalIcon\\web\\web2\\images\\a3_01.gif");
pictureBox1.Image = abc;
}
Try this, but set the image names
Bitmap abc = (Bitmap)System.Drawing.Bitmap.FromFile("C:\\Users\\HDAdmin\\Pictures\\HospitalIcon\\fafa\\images\\a3_00.gif");
if (berjaya[23].Equals(70))
{
abc = (Bitmap)System.Drawing.Bitmap.FromFile("C:\\Users\\HDAdmin\\Pictures\\HospitalIcon\\fafa\\images\\a3_01.gif");
}
myPicturebox.Image = abc;
You must add first a Empty bitmap.
like:
Bitmap abc;
Or you can assign value for that. Then in if/else you change into:
abc = (Bitmap)System.Drawing.Bitmap.FromFile("C:\\Users\\HDAdmin\\Pictures\\HospitalIcon\\fafa\\images\\a3_01.gif");
Additonal:
Ops, sorry Miss/Mr/Ms (What must I say?) Sara Brown, That will be a much complicated. add this function
public int value(string num){
string a = num.split("");//If this function true?
int res = 0;
for(var b = 0; b<a.Length; b++){
res = res*10;
switch(a[b]){
case "0":
break;
case "1":
res += 1;
break;
case "2":
res += 2;
break;
case "3":
res += 3;
break;
case "4":
res += 4;
break;
case "5":
res += 5;
break;
case "6":
res += 6;
break;
case "7":
res += 7;
break;
case "8":
res += 8;
break;
case "9":
res += 9;
break;
}
}
return res;
}
the add this code
Bitmap abc;
if(value(Berjaya[23])==70){
abc = (Bitmap)System.Drawing.Bitmap.FromFile("C:\\Users\\HDAdmin\\Pictures\\HospitalIcon\\fafa\\images\\a3_01.gif");
}