textbox text is checked only once - c#

I have 2 panels. If textboxes are empty, there is a message. My problem is, say I get to second panel then go back and delete texts then go to second panel again, it does not show message. Is there a way to check the texts every time?
private void bttn_Next_Click(object sender, EventArgs e)
{
if (txt_FirstName.Text == " " || txt_LastName.Text == " " ||
txt_Email.Text == " " || txt_Contact.Text == " " ||
txt_HouseNumber.Text == " " || txt_Street.Text == " " ||
txt_Barangay.Text == " " || txt_Municipality.Text == "")
{
MessageBox.Show("Please complete all required fields.", "Message");
}
else
{
panel1.Hide();
panel2.Hide();
panel3.Show();
}
}

The correct way to check for empty string is as follows
private void bttn_Next_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txt_FirstName.Text) || string.IsNullOrWhiteSpace(txt_LastName.Text) ||
string.IsNullOrWhiteSpace(txt_Email.Text) || string.IsNullOrWhiteSpace(txt_Contact.Text) ||
string.IsNullOrWhiteSpace(txt_HouseNumber.Text) || string.IsNullOrWhiteSpace(txt_Street.Text) ||
string.IsNullOrWhiteSpace(txt_Barangay.Text) || string.IsNullOrWhiteSpace(txt_Municipality.Text))
{
MessageBox.Show("Please complete all required fields.", "Message");
}
else
{
panel1.Hide();
panel2.Hide();
panel3.Show();
}
}

Related

How to save data from textboxes by linking comboboxes and checkboxes into richtextbox c#

I am creating a scheduler where data I entered below on two textboxes have to appear according to the Day or week and Time values. I've tried with the following code, but didn't work.
private void btnEnterData_click(object sender, System.EventArgs e)
{
if (((cmbDayWeek.SelectedIndex == 0) && (chb910.Checked == true)))
{
rtb1.Text = (tbxMedNumAppn.Text + tbxDocSpecAppn.Text);
}
else if (((cmbDayWeek.SelectedIndex == 0) && (chb1011.Checked == true)))
{
rtb7.Text = (tbxMedNumAppn.Text + tbxDocSpecAppn.Text);
}
return;
//and codes all the way down
}
private void btnSavePatient_Click(object sender, EventArgs e)
{
if (((cmbDayWeek.SelectedIndex == 0) && (chb910.Checked == true)))
{
rtb1.Show();
}
if (((cmbDayWeek.SelectedIndex == 0) && (chb1011.Checked == true)))
{
rtb2.Show();
}
// codes all the way down
}
P.S. I'm assuming that I have to use If/Else statement, but I can't get the logic fully :(
Image of the scheduler:

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;
}
}

Validating Textboxes and skipping Validations on form close

Following is the code snippet that I am using to validate my textboxes.
C# Code
private void TxtEmail_Validating(object sender, CancelEventArgs e)
{
string StrEmail = TxtEmail.Text.Trim();
if (StrEmail == "" || StrEmail.IndexOf("") > 0 || StrEmail.IndexOf('"') > 0 || StrEmail == " " || StrEmail.IndexOf(" ") > 0)
{
MessageBox.Show("Email Cannot be Left Blank");
TxtEmail.Select();
}
Regex r = new Regex(#"#.");
if (r.IsMatch(TxtEmail.Text))
{
AutoValidate = AutoValidate.Disable;
}
else
{
MessageBox.Show("Invalid Email Format");
TxtEmail.Clear();
TxtEmail.Focus();
}
}
private void TxtPassword_Validated(object sender, EventArgs e)
{
string StrPass=TxtPassword.Text;
if (StrPass == "" || StrPass.IndexOf("") > 0 || StrPass.IndexOf('"') > 0 || StrPass == " " || StrPass.IndexOf(" ") > 0)
{
MessageBox.Show("Invalid Password");
TxtPassword.Focus();
}
Regex r = new Regex(#"##");
if (r.IsMatch(TxtPassword.Text))
{
AutoValidate = AutoValidate.Disable;
}
else
{
MessageBox.Show("Invalid Password");
TxtPassword.Clear();
TxtPassword.Focus();
}
}
I am using AutoValidate feature to cancel validation if the parameters within the Regex match.But the problem is when I use AutoValidate or Set CauseValidation to false,the validation of TextBox is done the first time the form is loaded. But when I remove the text within the texbox and re-enter it, validation doesn't work the 2nd time and does not throw the message "Invalid Email"
In the case of Password Validation.Even after entering password as per the parameters within the Regex,it throws "Invalid Password" message.I saw questions here based on validation but none of them have satisfactory solution to my question.
Can anyone help me to rectify this error? I want to achieve Validation without ErrorProvider which is another option.

Write to text files

Totally new to this. Depending on which event button I click I need to write to the appropriate text file. However, the input data writes to the same text file. How do I specify the appropriate text file it writes to and is saved.
The coding, identical apart from the name of the text file, I'm currently using is:
private void btnItemAdd_Click(object sender, EventArgs e)
{
string sItem;
string sNumber;
if (rdoDrinks.Checked == false && rdoConfectionary.Checked == false)
{
//Message to remind user to select category
MessageBox.Show("Please select a category");
txtItem.Focus();
}
{
{
if ((rdoDrinks.Checked == true) && (txtItem.Text != "") && (txtItemNumber.Text == ""))
//Message to remind user to enter a number
MessageBox.Show("Please input a number");
txtItemNumber.Focus();
if ((txtItem.Text != "") && (txtItemNumber.Text != ""))
{
//add Item to end of list
lstItems.Items.Add(string.Format("{0, -15} {1, -20}", txtItem.Text, txtItemNumber.Text));
txtCount.Text = lstItems.Items.Count.ToString();
//set focus to the text box
txtItem.Focus();
StreamWriter sw = File.AppendText("Drinks.txt");
{
sItem = txtItem.Text;
sw.WriteLine(sItem);
sNumber = txtItemNumber.Text;
sw.WriteLine(sNumber);
}
MessageBox.Show("Details have been saved");
txtItem.Clear();
txtItemNumber.Clear();
sw.Close();
}
else if ((rdoConfectionary.Checked == true) && (txtItem.Text != "") && (txtItemNumber.Text == ""))
//Message to remind user to enter a number
MessageBox.Show("Please input a number");
txtItemNumber.Focus();
if ((txtItem.Text != "") && (txtItemNumber.Text != ""))
{
//add Item to end of list
lstItems.Items.Add(string.Format("{0, -15} {1, -20}", txtItem.Text, txtItemNumber.Text));
txtCount.Text = lstItems.Items.Count.ToString();
//set focus to the text box
txtItem.Focus();
StreamWriter sw = File.AppendText("Confectionary.txt");
{
sItem = txtItem.Text;
sw.WriteLine(sItem);
sNumber = txtItemNumber.Text;
sw.WriteLine(sNumber);
}
MessageBox.Show("Details have been saved");
txtItem.Clear();
txtItemNumber.Clear();
sw.Close();
}
}
}
}
You have several issues:
You are missing an else after the first if.
The code below can never end up being true.
The two if statement collide (the txtItemNumber.Text comparison is the problem). Hence you will never end up writing to Drinks.txt:
if ((rdoDrinks.Checked == true) && (txtItem.Text != "") && (txtItemNumber.Text == ""))
...
if ((txtItem.Text != "") && (txtItemNumber.Text != ""))

Avoid duplication of list items

I have a ListBox that contains items, which I have chosen from a ComboBox. When I chose an item from my ComboBox, it also removes that item in my ComboBox.
I have a button on my form, that let's me duplicate the selected item in my ListBox, giving me two of the same items in the ListBox.
I also have a button on my form, that let's me remove the selected item from my ListBox. If I remove an item from the ListBox, that item goes back to the ComboBox, but if I have duplicated the item and I remove both items from the ListBox, they both show up in the ComboBox.
I don't want it to be possible to have two of the same item in the ComboBox
Here's the code I have tried to use:
private void buttonRemove_Click(object sender, EventArgs e)
{
try
{
if (ComboBox.ToString().Contains("Chicken McNuggets"))
{
found = true;
}
if (!found)
{
ComboBox.Items.Add("Chicken McNuggets");
found = false;
}
ListBox.Items.Remove(ListBox.SelectedItem);
}
catch
{
MessageBox.Show(// Message);
}
}
This is my first time asking a question in here.
edit:
private void buttonRemove_Click(object sender, EventArgs e)
{
try
{
if ((String)listBox.SelectedItem == "Big Mac" || (String)listBox.SelectedItem == "Quarter Pounder" || (String)listBox.SelectedItem == "McFeast" || (String)listBox.SelectedItem == "Cheeseburger" || (String)listBox.SelectedItem == "Hamburger" || (String)listBox.SelectedItem == "Big Tasty Bacon" || (String)listBox.SelectedItem == "McChicken" || (String)listBox.SelectedItem == "Fillet-O-Fish" || (String)listBox.SelectedItem == "Chicken Nuggets")
{
comboBox.Items.Add(listBox.SelectedItem);
listBox.Items.Remove(listBox.SelectedItem);
}
else if ((String)listBox.SelectedItem == "BBQ dip" || (String)listBox.SelectedItem == "Cheddar dip" || (String)listBox.SelectedItem == "Gulerod" || (String)listBox.SelectedItem == "Hvidløgs dip" || (String)listBox.SelectedItem == "Karry dip" || (String)listBox.SelectedItem == "Ketchup" || (String)listBox.SelectedItem == "Pommes Frites Sauce" || (String)listBox.SelectedItem == "Sennep dip" || (String)listBox.SelectedItem == "Sursød dip" || (String)listBox.SelectedItem == "Æbler")
{
comboBox2.Items.Add(listBox.SelectedItem);
listBox.Items.Remove(listBox.SelectedItem);
}
else if ((String)listBox.SelectedItem == "Gulerodskage" || (String)listBox.SelectedItem == "Kanelsnegl" || (String)listBox.SelectedItem == "McDonut chokolade" || (String)listBox.SelectedItem == "Sundae m. chokoladesauce" || (String)listBox.SelectedItem == "McDonut sukkerovertræk" || (String)listBox.SelectedItem == "McFlurry Daim" || (String)listBox.SelectedItem == "McFlurry Smarties" || (String)listBox.SelectedItem == "Sundae m. jordbærdsauce" || (String)listBox.SelectedItem == "Sundae m. karamelsauce" || (String)listBox.SelectedItem == "Triple chokolade muffin" || (String)listBox.SelectedItem == "Vaffelis")
{
comboBox3.Items.Add(listBox.SelectedItem);
listBox.Items.Remove(listBox.SelectedItem);
}
else if (listBox.SelectedItem.ToString().Contains("Stor") || listBox.SelectedItem.ToString().Contains("Mellem") || listBox.SelectedItem.ToString().Contains("Lille") || listBox.SelectedItem.ToString().Contains("9") || listBox.SelectedItem.ToString().Contains("6") || listBox.SelectedItem.ToString().Contains("4"))
{
string objectToString = listBox.SelectedItem.ToString();
string[] ord = objektToString.Split(' ');
string selectedItem = listBox.SelectedItem;
var check = comboBox.Items.Cast<string>()
.ToList()
.FirstOrDefault(c => c.Contains(selectedItem));
if (check != null)
{
comboBox.Items.Add("Chicken McNuggets");
}
else
{
listBox.Items.Remove(selectedItem);
}
if (listBox.SelectedItem.ToString().Contains("Pommes Frites"))
comboBox2.Items.Add(ord[1] + " " + ord[2]);
else if (listBox.SelectedItem.ToString().Equals("Stor Coca-Cola") || listBox.SelectedItem.ToString().Equals("Mellem Coca-Cola") || listBox.SelectedItem.ToString().Equals("Lille Coca-Cola"))
comboBox4.Items.Add(ord[1]);
else if (listBox.SelectedItem.ToString().Contains("Milkshake"))
comboBox4.Items.Add(ord[1] + " " + ord[2] + " " + ord[3]);
else
comboBox4.Items.Add(ord[1] + " " + ord[2]);
listBox.Items.Remove(listBox.SelectedItem);
}
}
catch
{
MessageBox.Show(// Message);
}
}
We need to Cast to string the items from ComboBox so we can now easily check the ListBox.SelectedItem that already from ComboBox.Items.
private void buttonRemove_Click(object sender, EventArgs e)
{
try
{
string selectedItems = listBox1.SelectedItem.ToString();
var check = comboBox1.Items.Cast<string>()
.ToList()
.FirstOrDefault(c => c.Contains(selectedItems));
if (check != null)
{
}
else
{
comboBox.Items.Add("Chicken McNuggets");
listBox1.Items.Remove(selectedItems);
}
}
catch
{
//MessageBox.Show();
}
}

Categories