Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have 2 combobox and 7 textboxes.
The logic if the user provides input as 0 in all the textboxes and has selected the Leave or Holiday in the combox box then the condition should satisfy.
If they selected someother item in the combo box apart from the Holiday/Leave. It should throw the else part.
I have written below condition in c# but eventhough all the conditions are satisfied the code executes only the else condition.
if (((comboBox3.SelectedText == "Leave") ||
(comboBox3.SelectedText == "Holiday")) &&
textBox2.Text != "0" &&
textBox3.Text != "0" &&
textBox4.Text != "0" &&
textBox5.Text != "0" &&
textBox6.Text != "0" &&
textBox7.Text != "0" &&
textBox10.Text != "0")
{
MessageBox.Show("Sucess");
}
else
{
MessageBox.Show("Select Leave/Holiday since all the provided Data is 0");
}
EDIT:
As per the request, the output is:
State
0
0
0
0
0
0
0
Try this
if (((comboBox3.SelectedText == "Leave") ||
comboBox3.SelectedText == "Holiday")) &&
textBox2.Text == "0" &&
textBox3.Text == "0" &&
textBox4.Text == "0" &&
textBox5.Text == "0" &&
textBox6.Text == "0" &&
textBox7.Text == "0" &&
textBox10.Text == "0")
{
MessageBox.Show("Sucess");
}
else
{
MessageBox.Show("Select Leave/Holiday since all the provided Data is 0");
}
Replace your code with this and add the result from the Output window/MessageBox to your question above
if (((comboBox3.SelectedText == "Leave") ||
comboBox3.SelectedText == "Holiday")) &&
textBox2.Text == "0" &&
textBox3.Text == "0" &&
textBox4.Text == "0" &&
textBox5.Text == "0" &&
textBox6.Text == "0" &&
textBox7.Text == "0" &&
textBox10.Text == "0")
{
MessageBox.Show("Success");
}
else
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("State");
sb.AppendLine(comboBox3.SelectedText);
sb.AppendLine(textBox2.Text);
sb.AppendLine(textBox3.Text);
sb.AppendLine(textBox4.Text);
sb.AppendLine(textBox5.Text);
sb.AppendLine(textBox6.Text);
sb.AppendLine(textBox7.Text);
sb.AppendLine(textBox10.Text);
Console.WriteLine(sb.ToString());
MessageBox.Show(sb.ToString());
}
Ok. You're using combobox1.SelectedText, which is empty!
Try using combobox1.Text instead ...
if (((comboBox3.Text == "Leave") ||
comboBox3.Text == "Holiday")) &&
textBox2.Text == "0" &&
textBox3.Text == "0" &&
textBox4.Text == "0" &&
textBox5.Text == "0" &&
textBox6.Text == "0" &&
textBox7.Text == "0" &&
textBox10.Text == "0")
{
MessageBox.Show("Success");
}
else
{
MessageBox.Show("Select Leave/Holiday since all the provided Data is 0");
}
As mentioned by the user "dasblinkenlight", your code, in the actual state, translate as:
"If the user select Leave or Holiday and ALL inputs are DIFFERENT than 0, then 'Success'."
But your business logic state that "If the user select Leave or Holiday and ALL inputs are EQUAL than 0, then 'Success'".
Just change the "!=" operator for the "==" operator and you are fine.
Related
if ((ddl1.SelectedValue == "1") || (ddl1.SelectedValue == "100"))
{
if (Fee > 0)
{
Messages += " [Please the check fee and ddl1 type selected] ";
}
}
else if ((ddl1.SelectedValue == "2") || (ddl1.SelectedValue == "200"))
{
if (Fee == 0)
{
Messages += " [Please the check fee and ddl1 type selected] ";
}
}
//How to re-write the same logic in the most concise way?
if (((ddl1.SelectedValue == "1" || ddl1.SelectedValue == "100") && Fee > 0) ||
((ddl1.SelectedValue == "2" || ddl1.SelectedValue == "200") && Fee == 0 ))
{
Messages += " [Please the check fee and ddl1 type selected] ";
}
The innermost the parentheses are optional and for clarify to the reader and not compilation.
If you are absolutely certain that SelectedValue will never be anything other than 1, 100, 2 or 200, you can further consolidate (ddl1.SelectedValue == "1" || ddl1.SelectedValue == "100") to ddl1.SelectedValue.StartsWith("1"). If selected values contains other values or is total domain is subject to change, StartsWith becomes fragile.
string[] ValidValues = {"1", "100", "2", "200"};
if(Fee >= 0){
Messages += Array.IndexOf(ValidValues, ddl1.SelectedValue) == -1 ? "" : " [Please the check fee and ddl1 type selected] ";
}
I have 1 button that will execute a stored procedure based off which combobox has data in it. Are multiple if statements the best course for me to account for each combobox scenario? I currently have my code like so - which works, but is a bit slow. Is there a better way to write this syntax using C# and VS2013?
private void btn1_Click()
{
if (cbo1.Text.ToString() == "" && cbo2.Text.ToString() == "" && cbo3.Text.ToString() == "" && cbo4.Text.ToString() == "" )
{
MessageBox.Show("You failed to make a selection.");
return;
}
if (cbo1.Text.ToString() != "" && cbo2.Text.ToString() == "" && cbo3.Text.ToString() == "" && cbo4.Text.ToString() == "" )
{
//Go route1
}
if (cbo1.Text.ToString() == "" && cbo2.Text.ToString() 1= "" && cbo3.Text.ToString() == "" && cbo4.Text.ToString() == "" )
{
//Go route2
}
if (cbo1.Text.ToString() == "" && cbo2.Text.ToString() == "" && cbo3.Text.ToString() != "" && cbo4.Text.ToString() == "" )
{
//Go route3
}
if (cbo1.Text.ToString() == "" && cbo2.Text.ToString() == "" && cbo3.Text.ToString() == "" && cbo4.Text.ToString() != "" )
{
//Go route4
}
}
EDIT
#MethodMan --> is this how you would go about setting up the check?
var comboBoxes = this.Controls
.OfType<ComboBox>()
.Where(x => x.Name.StartsWith("comboBox"));
foreach(var cmbBox in comboBoxes)
{
(string.IsNullOrEmpty(cmbBox.Text)) || if (cmbBox.SelectedIndex == -1)
{
//How to find which combobox is cmbBox
}
}
my suggestion will be something like this:
private void btn1_Click()
{
int data = 0;
if(cbo1.Text.ToString() != "")
data+=1;
if(cbo2.Text.ToString() != "")
data+=2;
if(cbo3.Text.ToString() != "")
data+=4;
if(cbo4.Text.ToString() != "")
data+=8;
switch(data)
{
case 1:
//Go route1
break;
case 2:
//Go route2
break;
case 4:
//Go route3
break;
case 8:
//Go route4
break;
default:
MessageBox.Show("You failed to make a selection.");
break;
}
}
i`m not sure it will do the job you want but it is a lot faster and this way you can check wich combo the user selected and wich combo he did not
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a string such as "4 == 5 || 3 == 3 || 2 == 1" in c#.
How can I get this string to if statement,
actually i want to build if statement programmatically.
This is a simple implementation that can be extended. This is all based on your statement that you are sure of the format. You can extend this add multiple operators:
private static bool? EvaluateBooleanExpression(string expression)
{
var stack = new Stack<string>();
var elements = expression.Split(new string[] { " " },
StringSplitOptions.RemoveEmptyEntries);
bool? expressionResult = null;
for (int i = 0; i < elements.Length; i++)
{
string item = elements[i];
bool isLast = i == elements.Length - 1;
if (item == "||" || item == "&&" || isLast)
{
if (isLast)
stack.Push(item);
int v2 = Int32.Parse(stack.Pop());
string op = stack.Pop();
int v1 = Int32.Parse(stack.Pop());
bool localResult = false;
if (op == "==")
localResult = v1 == v2;
else if (op == "!=")
localResult = v1 != v2;
if (stack.Count > 0 && (stack.Peek() == "||" || stack.Peek() == "&&"))
item = stack.Pop();
if (expressionResult == null)
expressionResult = localResult;
else if (item == "||")
{
expressionResult = expressionResult.Value || localResult;
// no need to check further if one result is true
if (expressionResult.Value)
break;
}
else if (item == "&&")
{
expressionResult = expressionResult.Value && localResult;
}
if (!isLast)
stack.Push(item);
}
else
{
stack.Push(item);
}
}
return expressionResult;
}
Usage:
static void Main(string[] args)
{
string expression1 = "4 == 5 || 3 == 6 || 2 == 1";
string expression2 = "4 == 4 && 5 == 5 && 7 == 7";
string expression3 = "4 == 4 || 5 == 5 && 3 == 7";
Console.WriteLine(expression1 + " Evaluates to: " + EvaluateBooleanExpression(expression1).Value);
Console.WriteLine(expression2 + " Evaluates to: " + EvaluateBooleanExpression(expression2).Value);
Console.WriteLine(expression3 + " Evaluates to: " + EvaluateBooleanExpression(expression3).Value);
}
Prints:
4 == 5 || 3 == 6 || 2 == 1 Evaluates to: False
4 == 4 && 5 == 5 && 7 == 7 Evaluates to: True
4 == 4 || 5 == 5 && 3 == 7 Evaluates to: True
So I need to do so in the "Year", you can enter the date only.
To pop up a warning something.
Here's the code, if the user enters an empty value, you need to add?
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "" || textBox6.Text == "")
{
MessageBox.Show("Не заполнены все поля!");
return;
}
And why in the "Year" the whole date? Plug the NumericUpDown to the limitations of values and the whole business.)))
Put together textBox dateTimePicker, further:
string date=dateTimePicker1.Value.ToString ("dd.mm.yy")
I need to show a pop up alert when some fileds have got not null values. I Used code piece like below , but not working correctly..Could you pls let me know the corrcet syntax for doing that?
if (txtSearchName.Text != "" || cmbSearchOaO.SelectedItem.Text != "" || cmbVessel.SelectedItem.Text != "" || cmbSearchApplicationType.SelectedItem.Text != "" || cmbSearchHull.SelectedItem.Text != "" || cmbSearchCategory.SelectedItem.Text != "" || cmbSearchHazardCategory.SelectedItem.Text != "")
{
ScriptManager.RegisterClientScriptBlock(btApplySearch, btApplySearch, "<script> alert('Inserted successfully');</script>", true);
}
Here btApplySearch is the buttonID used in aspx markup.
Try this:
if (txtSearchName.Text != "" || cmbSearchOaO.SelectedItem.Text != "" || cmbVessel.SelectedItem.Text != "" || cmbSearchApplicationType.SelectedItem.Text != "" || cmbSearchHull.SelectedItem.Text != "" || cmbSearchCategory.SelectedItem.Text != "" || cmbSearchHazardCategory.SelectedItem.Text != "")
{
RegisterDOMReadyScript("alert message", "alert('Message Here');");
}
Hope Its Helpful.
if not using Update panel then use below code
Page.ClientScript.RegisterStartupScript(this.GetType(),"Msg",alert("Javascript message"), true);
else
ScriptManager.RegisterStartupScript(this.GetType(),"Msg",alert("Javascript message"), true);
try the following code inside the if :
if (txtSearchName.Text != "" || cmbSearchOaO.SelectedItem.Text != "" || cmbVessel.SelectedItem.Text != "" || cmbSearchApplicationType.SelectedItem.Text != "" || cmbSearchHull.SelectedItem.Text != "" || cmbSearchCategory.SelectedItem.Text != "" || cmbSearchHazardCategory.SelectedItem.Text != "")
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "err", "alert('Message you want write');", true);
}
RegisterStartupScript has 5 parameters we used.
this:: referers to the control,
this.GetType() is used to get the type of control,
err is a string key.
alert('mesage') whatever you want to write,
true:: do you want to add script block in code or not if yes the true otherwise false
Hope this will work for you