I have a list of YES/NO questions and each question has a radio button indicating the answer. When the user selects YES, a panel will be visible and it has textboxes inside it for the additional required input. When the user answers YES, they MUST fill in the textboxes that appear.
Currently I'm hard-coding it this way:
if (txtQ1Specify.Visible == true)
{
if (txtQ1Specify.Text.Length == 0)
{
lblError.Text = "Please answer all questions.";
}
}
if (txtQ2Specify.Visible == true)
{
if (txtQ2Specify.Text.Length == 0)
{
lblError.Text = "Please answer all questions.";
}
}
if (txtQ3Specify.Visible == true)
{
if (txtQ3Specify.Text.Length == 0)
{
lblError.Text = "Please answer all questions.";
}
}
if (txtQ4SpecifyCompany.Visible == true || txtQ4SpecifyRelative.Visible == true)
{
if (txtQ4SpecifyCompany.Text.Length == 0 || txtQ4SpecifyRelative.Text.Length == 0)
{
lblError.Text = "Please answer all questions.";
}
}
if (txtQ5SpecifyCompany.Visible == true || txtQ5SpecifyRelative.Visible == true)
{
if (txtQ5SpecifyCompany.Text.Length == 0 || txtQ5SpecifyRelative.Text.Length == 0)
{
lblError.Text = "Please answer all questions.";
}
}
if (txtQ6Specify.Visible == true)
{
if (txtQ6Specify.Text.Length == 0)
{
lblError.Text = "Please answer all questions.";
}
}
if (txtQ7Specify.Visible == true)
{
if (txtQ7Specify.Text.Length == 0)
{
lblError.Text = "Please answer all questions.";
}
}
After this checking I want to execute some code.
The page looks like this:
How can I check for textbox inputs based in visibility?
You could use LINQ to find out if there are any visible and empty TextBoxes like so:
var query =
from t in Page.Controls.OfType<TextBox>()
where t.Visible && t.Text == ""
select t;
bool hasUnanswered = query.Any();
This can be easily done on the client side .
First you need to identify which all text box are visible . For this you can use jquery Visible Selector
Now If more than one text box is visible . Show some message to the user and focus and highlight the text box which need to be filled .
$(document).ready(function(){
var visibleCount =$('input[type="text"]:visible').length;
if (visibleCount > 0)
{
// Add your logic here
}
});
I managed to do it using a very long if statement. Here goes nothing:
if ((pnlQ1Yes.Visible == true && txtQ1Specify.Text.Length == 0) ||
(pnlQ2Yes.Visible == true && txtQ2Specify.Text.Length == 0) ||
(pnlQ3Yes.Visible == true && txtQ3Specify.Text.Length == 0) ||
(pnlQ4Yes.Visible == true && (txtQ4SpecifyCompany.Text.Length == 0 || txtQ4SpecifyRelative.Text.Length == 0)) ||
(pnlQ5Yes.Visible == true && (txtQ5SpecifyCompany.Text.Length == 0 || txtQ5SpecifyRelative.Text.Length == 0)) ||
(pnlQ6Yes.Visible == true && txtQ6Specify.Text.Length == 0) ||
(pnlQ7Yes.Visible == true && txtQ7Specify.Text.Length == 0))
{
lblError.Text = "Please answer all questions.";
}
else
{
...
}
It first checks if the panel is visible, then it checks the textbox inside the panel for values. Then I repeat this kind of checking for the rest of the panels and textboxes.
Related
I'm trying to do waste collection program and these are part of codes. My problem is if the picturebox shows image that on second if statements (magazine), there is no problem. But if shows first image that on first if statements (newspaper) and if NewWaste(); gives magazine then there is a problem. Because it adds both of them to listbox but I don't see the second image on picturebox. How can I solve that?
private void NewWaste()
{
Image[] images = new Image[] { newspaper.Image, magazine.Image, glass.Image };
int wastes = rnd.Next(images.Length);
wastePictureBox.Image = images[wastes];
}
//(part of class)
public bool Add(Waste waste)
{
if (FilledVolume + waste.Volume <= Capacity)
return true;
else
return false;
}
private void addPaperWasteBtn_Click(object sender, EventArgs e)
{
if (paperWasteBox.Add(newspaper) == true && wastePictureBox.Image == newspaper.Image)
{
paperWasteListBox.Items.Add("Newspaper");
NewWasteImage();
}
if (paperWasteBox.Add(magazine) == true && wastePictureBox.Image == magazine.Image)
{
paperAtikListBox.Items.Add("Magazine");
NewWasteImage();
}
}
If you only want the second if statement to run if the first one didn't, then you want an else if statement before the second conditional check.
Change:
if (paperWasteBox.Add(newspaper) == true && wastePictureBox.Image == newspaper.Image)
{
paperWasteListBox.Items.Add("Newspaper");
NewWasteImage();
}
if (paperWasteBox.Add(magazine) == true && wastePictureBox.Image == magazine.Image)
{
paperAtikListBox.Items.Add("Magazine");
NewWasteImage();
}
To:
if (paperWasteBox.Add(newspaper) == true && wastePictureBox.Image == newspaper.Image)
{
paperWasteListBox.Items.Add("Newspaper");
NewWasteImage();
}
else if (paperWasteBox.Add(magazine) == true && wastePictureBox.Image == magazine.Image)
{
paperAtikListBox.Items.Add("Magazine");
NewWasteImage();
}
Notice the difference in the SIXTH line!
Is this wrong?
I always get "cb1 and firmware" even if my checkBox2 is checked. I also tried with just & instead of &&.
It was working fine before I had to add it into thread to get UI to update correctly.
private void MyWorkerThread2()
{
if (this.IsChecked(checkBox1) && (this.IsChecked(checkBox2) && (myString == "86.09.0000")))
{
MessageBox.Show("cb1 and firmware and cb2");
Prep.clean();
startedimage();
fscreate();
wipefiles();
}
else if ((this.IsChecked(checkBox1) && (myString == "86.09.0000")))
{
MessageBox.Show("cb1 and firmware");
Prep.clean();
startedimage();
wipefiles();
}
else if (myString == "86.09.0000")
{
MessageBox.Show("firmware");
if (myThread == null)
{
Prep.clean();
startedimage();
myThread = new Thread(MyWorkerThread);
myThread.IsBackground = true;
myThread.Start();
}
}
else
{
FactoryReset();
}
}
public delegate bool IsCheckedDelegate(CheckBox cb);
public bool IsChecked(CheckBox cb)
{
if (cb.InvokeRequired)
{
return (bool)cb.Invoke(new IsCheckedDelegate(IsChecked), new Object[] { cb });
}
else
{
return cb.Checked;
}
}
I always get "cb1 and firmware" even if my checkBox2 is checked.
The fact that checkBox2 is checked isn't going to change the fact that checkBox1 is also checked and so the first if statement succeeds. If checkBox1 wasn't checked, it would fall to the other sets.
It's not clear what you're trying to do here, but I would say the first two if statements need reversed.
It seems like you only want the first to be executed when checkBox2 is NOT checked.
Change:
if ((this.IsChecked(checkBox1) && (myString == "86.09.0000")))
To:
if ((this.IsChecked(checkBox1) && (!this.IsChecked(checkBox2) && (myString == "86.09.0000")))
There are four possibilities here:
none
cb1
cb2
cb1, cb2
Try reordering the code
if (this.IsChecked(checkBox1) && (this.IsChecked(checkBox2) && (myString == "86.09.0000")))
{
MessageBox.Show("cb1 and firmware and cb2");
Prep.clean();
startedimage();
fscreate();
wipefiles();
}
else if ((this.IsChecked(checkBox1) && (myString == "86.09.0000")))
{
MessageBox.Show("cb1 and firmware");
Prep.clean();
startedimage();
wipefiles();
}
I have an IF statement that is supposed to make sure the TextBox1.Text and TextBox2.Text do not match or are not blank. If they don't match or are not blank then it is supposed to assign the text in the boxes to two string variable. What I can't figure out is why when I leave the two textboxes blank the true statement still fires.
if ((tbStartBreak2.Text != tbEndBreak2.Text) || (tbStartBreak2.Text == "" && tbEndBreak2.Text == ""))
{
sb2 = tbStartBreak2.Text;
se2 = tbStartBreak2.Text;
}
There are two conditions in your if statement:
if ((tbStartBreak2.Text != tbEndBreak2.Text) || (tbStartBreak2.Text == "" && tbEndBreak2.Text == ""))
The first one checks to make sure they don't match (so, good). The second checks to make sure that they are blank (so, bad). You want this:
if ((tbStartBreak2.Text != tbEndBreak2.Text) || (tbStartBreak2.Text != "" && tbEndBreak2.Text != ""))
Also, what are you trying to do? The second condition is the only one you need if you really want them not to match OR not be blank - because the only time this will be false is if they are both blank.
You wrote "OR textbox are blank", you need "OR textbox are not blank"
if ((tbStartBreak2.Text != tbEndBreak2.Text) || (tbStartBreak2.Text != "" && tbEndBreak2.Text != ""))
{
sb2 = tbStartBreak2.Text;
se2 = tbStartBreak2.Text;
}
As a side note, I'd replace "" with string.Empty for readability.
if ((tbStartBreak2.Text != tbEndBreak2.Text) || (tbStartBreak2.Text != string.Empty && tbEndBreak2.Text != string.Empty))
{
sb2 = tbStartBreak2.Text;
se2 = tbStartBreak2.Text;
}
And for even more readability, you can extract these big conditions
if (TextboxesDoNotMatch() || TextboxesAreNotEmpty())
{
sb2 = tbStartBreak2.Text;
se2 = tbStartBreak2.Text;
}
private bool TextboxesDoNotMatch()
{
return tbStartBreak2.Text != tbEndBreak2.Text;
}
private bool TextboxesAreNotEmpty()
{
return tbStartBreak2.Text != string.Empty && tbEndBreak2.Text != string.Empty;
}
If you want it to return that they are NOT blank then you need to do this
(tbStartBreak2.Text != "" && tbEndBreak2.Text != "")
you have an OR between both conditions so when both are empty the second part will be true regardless the first part
(tbStartBreak2.Text == "" && tbEndBreak2.Text == "")
I have several panels on a form that I want to appear corresponding to a numericUpDown value.
(ie- panel 1 is visible when the value is 1, panels 1 and 2 are visible when the number is 2, panels 1 2 and 3 are visible when the value is 3, ect)
I am able to get the initial panel to function as expected with my existing code, but the subsequent ones are not appearing or disappearing as I intended. I'm not quite sure why. Is it because the value of the NUP is not updating when it is changed?
Code:
private void petNumNumericUpDown_ValueChanged(object sender, EventArgs e)
{
if ((petNumNumericUpDown.Value == 1) || (petNumNumericUpDown.Value == 2) ||(petNumNumericUpDown.Value == 3) || (petNumNumericUpDown.Value == 4) || (petNumNumericUpDown.Value == 5))
{
pet1Panel.Visible = true;
}
else
{
pet1Panel.Visible = false;
}
if((petNumNumericUpDown.Value == 2) || (petNumNumericUpDown.Value == 3) || (petNumNumericUpDown.Value == 4) || (petNumNumericUpDown.Value == 5))
{
pet2Panel.Visible = true;
}
else
{
pet2Panel.Visible = false;
}
}
I'm looking to have this continue up until 5. Any insight on what I'm doing wrong would be appreciated.
You can write simpler code to achieve your goal:
pet1Panel.Visible = (petNumNumericUpDown.Value >= 1);
pet2Panel.Visible = (petNumNumericUpDown.Value >= 2);
...
I am still in the process of learning Asp.Net. I have a question . I was using an if condition in which i was checking dataset values. It is throwing me an exception whenever it is checking the condition as the dataset has not got any value. How to overcome this.
Code for this is here:
DataSet ds = merchant.getIsactiveIsconfirmedforcancelaccount(merchantID);
if (_merchant.PackageID == (int)CommonHelper.Package.Free && _merchant.AccountStatus.Contains("Awaiting"))
{
spnMerchantActiveStatus.InnerHtml = ApplicationData.MSG_AWAITING_CONFIRMATION;
}
***else if ((ds.Tables[0].Rows[0]["IsConfirmed"]).ToString() == "True" && (ds.Tables[0].Rows[0]["Active"]).ToString() == "N")***
{
_merchant.AccountStatus = "Cancelled";
spnMerchantActiveStatus.InnerHtml = _merchant.AccountStatus;
}
else if(_merchant.PackageID != (int)CommonHelper.Package.Free && ds1.Tables[0].Rows.Count == 0 && (ds2.Tables[0].Rows[0]["ConfirmationSrc"]).ToString() == "Admin")
{
_merchant.AccountStatus = "Free Trial";
spnMerchantActiveStatus.InnerHtml = _merchant.AccountStatus;
}
else
spnMerchantActiveStatus.InnerHtml = _merchant.AccountStatus;
}
The exception here is "There is no row at position 0."
Seems like you do not have any rows in your tables[0]. You can add condition to check is rows > 0 and then continue with other conditions in the IF.
You are assuming you have rows in your DataSet.
Instead of
if ((ds.Tables[0].Rows[0]["IsConfirmed"]).ToString() == "True" &&
(ds.Tables[0].Rows[0]["Active"]).ToString() == "N")
you should do something like
if ((ds.Tables[0].Rows.Count() > 0) &&
(ds.Tables[0].Rows[0]["IsConfirmed"]).ToString() == "True" &&
(ds.Tables[0].Rows[0]["Active"]).ToString() == "N")
But you really need to do fuller error checking than just that one condition affectin you right now.
EDIT: If it's not obvious why the above works, read up on short-circuiting of logical operators: http://msdn.microsoft.com/en-us/library/2a723cdk(VS.71).aspx
Try changing your else statement :
else if (
(ds.Tables[0].Rows[0]["IsConfirmed"]).ToString() == "True" &&
(ds.Tables[0].Rows[0]["Active"]).ToString() == "N")
{
_merchant.AccountStatus = "Cancelled"; spnMerchantActiveStatus.InnerHtml = _merchant.AccountStatus;
}
To
if ((null != ds.Tables[0]) && ((ds.Tables[0].Rows[0]["IsConfirmed"]).ToString() == "True") && ((ds.Tables[0].Rows[0]["Active"]).ToString() == "N"))
So that you check that the data set is not null before you check conditions on it.
or (as other posters say) check that you actually have rows in the dataset.
ds.Tables[0].Rows.Count() != 0