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();
}
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!
Hi guys actually first I wanted to do this loop.
Process p = Process.GetProcessesByName("etcProgram.bin")[0];
foreach (System.Diagnostics.ProcessModule moz in p.Modules)
if (csh.Text == "csh" || bin.Text == "bin")
{
if (moz.FileName.IndexOf("csh") != -1)
{
csh.Text = moz.BaseAddress.ToString();
}
if (moz.FileName.IndexOf("bin") != -1)
{
bin.Text = moz.BaseAddress.ToString();
}
}
else
{
!!!!!! return to "if" until "if code" happens !!!!!!
}
But my poor code knowledge can't come through this problem. So I wrote nearly same thinh with timer. Then I wrote this code.
private void tmrActive_Tick(object sender, EventArgs e)
{
try
{
Process p = Process.GetProcessesByName("Wolfteam.bin")[0];
foreach (System.Diagnostics.ProcessModule moz in p.Modules)
if (csh.Text == "csh" || bin.Text == "bin")
{
if (moz.FileName.IndexOf("csh") != -1)
{
csh.Text = moz.BaseAddress.ToString();
}
if (moz.FileName.IndexOf("bin") != -1)
{
bin.Text = moz.BaseAddress.ToString();
}
}
else
{
tmrActive.Stop();
MessageBox.Show("It's stopped");
}
}
But I saw that MessageBox appears 5-6 times when I started this.And I dont know why. So I dont feel very safe about use this code.
1- Do you know what's the problem with that timer. Shouldn't this messagebox appear once?
2- Can you help me about the code without timer.Is there anyway to do it?
You mean something like...
foreach (System.Diagnostics.ProcessModule moz in p.Modules)
{
bool breakloop = false;
while (!breakloop)
{
if (csh.Text == "csh" || bin.Text == "bin")
{
if (moz.FileName.IndexOf("csh") != -1)
csh.Text = moz.BaseAddress.ToString();
if (moz.FileName.IndexOf("bin") != -1)
bin.Text = moz.BaseAddress.ToString();
breakloop = true;
}
}
}
You can simply use break statement in order to stop a loop.
foreach (System.Diagnostics.ProcessModule moz in p.Modules)
{
if (csh.Text == "csh" || bin.Text == "bin")
{
if (moz.FileName.IndexOf("csh") != -1)
{
csh.Text = moz.BaseAddress.ToString();
}
if (moz.FileName.IndexOf("bin") != -1)
{
bin.Text = moz.BaseAddress.ToString();
}
break;
}
}
Hope this helps.
You could use this whole code as a Recursive function with a Specific condition to stop the condition whenever you want like this
foreach (System.Diagnostics.ProcessModule moz in p.Modules) { looping () {
bool breakloop = false;
while (!breakloop)
{
if (csh.Text == "csh" || bin.Text == "bin")
{
if (moz.FileName.IndexOf("csh") != -1)
csh.Text = moz.BaseAddress.ToString();
if (moz.FileName.IndexOf("bin") != -1)
bin.Text = moz.BaseAddress.ToString();
breakloop = true;
looping();
}
}
I am using TreeListView with:
this.tlv.CheckBoxes = true;
this.tlv.TriStateCheckBoxes = true;
this.tlv.HierarchicalCheckboxes = true;
Hierarchical with tristate works well, except one: the user can set the CheckState.Indeterminate by clicking the mouse, and I don't need it. For this I use 2 delegate that are not working correctly. How to make that work?
this.tvl.CheckStateGetter = delegate(object rowObject)
{
if (((ModelData)rowObject).IsChecked == true)
{
return CheckState.Checked;
}
else
{
if (((ModelData)rowObject).IsChecked == false)
{
return CheckState.Unchecked;
}
else
{
return CheckState.Indeterminate;
}
}
};
this.tvl.CheckStatePutter = delegate(object rowObject, CheckState newValue)
{
if (((ModelData)rowObject).Child.Count > 0)
{
if ((((ModelData)rowObject).Child.Where(x => x.IsChecked != null).Any(x => (bool)x.IsChecked) &&
((ModelData)rowObject).Child.Where(x => x.IsChecked != null).Any(x => !(bool)x.IsChecked)) ||
(((ModelData)rowObject).Child.Any(x => x.IsChecked == null)))
{
((ModelData)rowObject).IsChecked = null;
return CheckState.Indeterminate;
}
else
{
if (((ModelData)rowObject).Child.Where(x => x.IsChecked != null).All(x => (bool)x.IsChecked))
{
((ModelData)rowObject).IsChecked = true;
return CheckState.Checked;
}
else
{
((ModelData)rowObject).IsChecked = false;
return CheckState.Unchecked;
}
}
}
else
{
((ModelData)rowObject).IsChecked = (newValue == CheckState.Checked) ? true : false;
return newValue;
}
};
According to the documentation, "CheckStateGetters" are not allowed in the treelistview.
From the webpage:
One major problem is that we don’t know the checkedness of all the
subitems. When an ObjectListView has a CheckStateGetter installed, the
only way we can know if an item is checked is by calling the
CheckStateGetter on that item. We can’t reason about what is checked
or unchecked – we always have to ask. In our disk browser example, we
would have to ask all 700,000 items if it was checked. That’s never
going to work, so with hierarchical checkboxes, we don’t allow
CheckStateGetters to be installed.
http://objectlistview.sourceforge.net/cs/blog7.html
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.
After reading this question, it has become apparent to me that I am writing my event incorrectly. However, I have no idea how I am going to be able to re-write what I have written using Object sender. My event adds the text from selected checkboxes to a two-dimensional list (report), and the order in report must be the same as the order of the selected checkboxes. Also, no more than two checkboxes can be selected at a time. Here is the event:
void checkedListBox_ItemCheck(CheckedListBox chkdlstbx, ItemCheckEventArgs e)
{
int index = Convert.ToInt32(chkdlstbx.Tag);
if ((chkdlstbx.CheckedItems.Count == 0) && (e.CurrentValue == CheckState.Unchecked))
{
Var.report[index].Add(chkdlstbx.Text);
}
if ((chkdlstbx.CheckedItems.Count == 1) && (e.CurrentValue == CheckState.Checked))
{
Var.report[index].RemoveAt(0);
}
if ((chkdlstbx.CheckedItems.Count == 1) && (e.CurrentValue == CheckState.Unchecked))
{
if (chkdlstbx.SelectedIndex < chkdlstbx.CheckedIndices[0])
{
Var.report[index].Insert(0, chkdlstbx.Text);
}
else
{
Var.report[index].Add(chkdlstbx.Text);
}
}
if ((chkdlstbx.CheckedItems.Count == 2) && (e.CurrentValue == CheckState.Checked))
{
if (chkdlstbx.SelectedIndex == chkdlstbx.CheckedIndices[0])
{
Var.report[index].RemoveAt(0);
}
else
{
Var.report[index].RemoveAt(1);
}
}
if ((chkdlstbx.CheckedItems.Count == 2) && (e.CurrentValue == CheckState.Unchecked))
{
e.NewValue = CheckState.Unchecked;
}
updateReport();
}
It is being called by this line:
chkdlstbx.ItemCheck += new ItemCheckEventHandler(checkedListBox_ItemCheck);
If anyone could help me re-write my event using object, that'd be awesome. I'm not really sure how else I would go about solving this problem!
This should suffice:
void checkedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
{
CheckedListBox chkdlstbx = sender as CheckedListBox;
if (chkdlstbx == null)
{
throw new InvalidArgumentException();
}
....
}