How to make so that you can only enter a date? - c#

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")

Related

IF condition AND / OR logic fails - c# [closed]

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.

How to enable the disabled button when the textbox is already filled with data

private void UserForm_Load(object sender, EventArgs e)
{
if (txtUsername.Text != "" || txtPassword.Text != "" || txtID.Text != "" || txtFirstName.Text != "" || txtMiddleName.Text != "" || txtLastName.Text != "" || txtDep.Text != "")
{
btnNext.Enabled = true;
}
else
{
btnNext.Enabled = false;
}
}
I was trying to make a form where the button is disabled and once the textboxes filled up with data, the button will be enabled.
The problem is, the button is disabled even the textboxes is already filled with data.
That is happening because you might be populating the control later and checking the text boxes first in the code.
The code to enable and disable the controls should be called after the controls get populated with the values. So, you just need to call your code at right time.
That's because you are not checking if you should enable the button OnChange of the text field each time. You are checking only on UserForm_Load(object sender, EventArgs e)
This is what you need:
private void textBox__TextChanged(object sender, EventArgs e)
{
if (txtUsername.Text != "" || txtPassword.Text != "" || txtID.Text != "" || txtFirstName.Text != "" || txtMiddleName.Text != "" || txtLastName.Text != "" || txtDep.Text != "")
{
btnNext.Enabled = true;
}
else
{
btnNext.Enabled = false;
}
}

1 Button 4 Comboboxes On Win Form

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

Checking if a textbox is "0" or null or empty

Im trying to see if a textbox is "0" or empty using the following code.
I hoped I could use the ! operator on IsNulorEmpty but seems it doesn't do what I want. Is there an easy way to do this on one line?
if (!String.IsNullOrEmpty(MyTextbox.Text) || MyTextbox.Text != "0")
{
//string is either null, empty or 0
}
else
{
//string has a value
}
The code is doing what you write, but you are confused about operator evaluations:
if (string.IsNullOrEmpty(MyTextbox.Text) || MyTextbox.Text == "0")
{
//string is either null, empty or 0
}
else
{
//string has a value
}
if (!(String.IsNullOrEmpty(MyTextbox.Text) || MyTextbox.Text == "0"))
{
//string has a value
}
else
{
//string is either null, empty or 0
}

Pop UP alert message from c# page

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

Categories