I want the MessageBox in this code to be run only 1 time. I want it to be shown only one time. How can I do it?
if(textBox1.Text != "")
{
listBox1.Items.Add(textBox1.Text);
textBox1.Clear();
}
else
{
DialogResult click = MessageBox.Show("Info Screen", "TextBox is Empty. Do you want to add a
blank space?", MessageBoxButtons.OKCancel);
if(click==DialogResult.OK)
{
listBox1.Items.Add(textBox1.Text);
}
}
Make a boolean and use an if statement and inside that if statement make the boolean false and show your message box
bool boolMessage = true;
if(boolMessage == true)
{
MessageBox.Show("One time");
boolMessage = false;
}
Create a global variable of type boolean and save the mark if the message has been shown. Then in your code add
Global:
bool isFirstTime = true;
In your code:
if (isFirstTime) {
DialogResult click = MessageBox.Show("Info Screen", "TextBox is Empty. Do you want to add a
blank space?", MessageBoxButtons.OKCancel);
isFirstTime = false;
}
I think adding a boolean flag in your case is just code pollution. You can solve this problem the other way. => When do you want the MessageBox to be displayed? When the textbox content equals "" and you want this to run only once. Why not to check if there is already an "" within the listbox before displaying the messagebox? Something like this:
else
{
if(listbox1.Items.IndexOf("") > -1)
return;
// continue with displaying the messagebox
}
Try using a boolean and use an if to make sure that the code runs only if the Boolean is true, then, inside the if just set the Boolean to false.
You can try also using a function and calling it once.
I am not sure if I answered your question.
Related
I am making a simple hangman style game in a C# form. I have it set as textBox1_TextChanged. Except everytime the user backspaces their guess letter it takes in blank space. How can I make it so after the message saying right/wrong it clears the space. I am getting annoyed at it telling the user they made a wrong guess after they backspace. This is my first post on this forum so sorry if the code text is weird. I just want the program to clear the text in the textBox after they guess.
UPDATE: Added suggested information. Now it does everything it is supposed to do. Except it pops up a windows saying " was found in the target word". This happens if guessLetter == null || guessLetter == correct || guessLetter == false.
private void textBox1_TextChanged(object sender, EventArgs e)
{
string guessLetter = textBox1.Text;
//textBox1.ReadOnly = true;
if (targetWord == null)
{
MessageBox.Show("Please start a new game.");
textBox1.Text = ("");
}
else
{
if (targetWord.Contains(guessLetter))
{
MessageBox.Show(guessLetter + " was found in the word");
}
else
{
MessageBox.Show(guessLetter + " was not found in the word");
incorrectGuessCtr++;
textBox3.Text = incorrectGuessCtr.ToString();
}
textBox1.Text = ("");
}
}
Don't only check if the targetWord is null, but also the guessLetter. You'd better use string.IsNullOrEmpty too, since it also checks if the string is empty:
if (!string.IsNullOrEmpty(targetWord) && !string.IsNullOrEmpty(guessLetter))
{
...
}
I guess you should also check if there is exactly one letter entered. That would mean this additional check:
if (guessLetter.Length == 1)
{
...
}
You will enter this event when you write code that changes Text property in textbox. I mean this.
textBox3.Text = incorrectGuessCtr.ToString();
Put something in function arguments or set some flags so that you can identify whether the event is called from user input or your clearing the text.
Just check how many times this function is called when user press backspace. You will get the idea.
I have a modeless UI that adds a userID to a list that allows or removes access to parts of a program. When I click the modify button everything works as it should. Suppose I close the dialog and realize "Wait, forgot to do X". When I reopen the dialog box, perform my work and click Modify, the value for adding the userID is still available to the program even though the textbox is blank.
It's happening somewhere in the following code.
public static void checkSame()
{
int count = 0;
bool test = false;
while (linesPerm.Length >= count && tbPermValue != "")
{
if (linesPerm.Length >= count)
{
test = linesPerm.Contains(tbPermValue);
count += (linesPerm.Length + 1);
if (test == true)
{
DialogResult dr = MessageBox.Show("The UserID " + tbPermValue +
" already exists in the Permissions column. "
+ Environment.NewLine + "Would you like to add the UserID" +
tbPermValue + " to the Permissions column anyway?",
"User Already Exists", MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
switch (dr)
{
case DialogResult.Yes:
break;
case DialogResult.No:
tbPermValue = "";
break;
}
}
}
else
{
MessageBox.Show("Do Nothing");
}
}
}
If the user selects No on the dialog box, the value of tbPermValue is not available to the program. If the user selects Yes then the value of tbPermValue persists even if the dialog box is closed and reopened. I have tried to clear the textbox value like so.
tbUserName.Text = "";
tbUserName.Clear();
and several other ways. tbUserName value is being cleared from the textbox, but not from the code above. I get the value of tbPermValue like this.
public static void addPerm(System.Windows.Forms.Form targetForm)
{
foreach (Control C in targetForm.Controls)
{
if (C.GetType() == typeof(TextBox))
{
if (C.Text != "")
{
tbPermValue = C.Text;
}
}
}
}
This is a modeless dialog box owned by it's parent.
Can anyone point me in a direction that would remove access to tbPermValue to the DialogResult portion of the first code box after the button is clicked. I can't lose it completely because tbPermValue is used in other code down the line.
EDIT: Ok. I just tested this and the value is being held in memory. I have a dialog Form1 that has a button that opens dialog StartHere. On StartHere there is a button that opens Permissions. StartHere owns Permissions so that when I close StartHere, Permissions and all other child forms of StartHere will close. These are all modeless dialogs. My variable tbPermValue is being held in memory way back to Form1. The value is not being disposed when I close the dialog StartHere. I'm going to go back and research Garbage Collection at the advice of Eric below. Thank you Eric. I'll delete the question or at least post a new better question once I find out the rules for this process. Thank You.
Edit 2: Here is the code you asked for γηράσκωδ'αείπολλάδιδασκόμε
private void bModify_Click(object sender, EventArgs e)
{
WidgetLogic.addPerm(this);
WidgetLogic.checkSame();
WidgetLogic.writePerm(this);
WidgetLogic.writeAdmin(this);
WidgetLogic.writeDetailer(this);
tbUserName.Clear();
}
As noted above I have tried numerous ways to clear tbUserName to no avail.
Don't use tbPermValue but instead use the textbox directly:
while (linesPerm.Length >= count && tbUserName.Text != "")
EDIT
Change the code in addPerm to this, and you are done :):
public static void addPerm(System.Windows.Forms.Form targetForm)
{
foreach (Control C in targetForm.Controls)
{
if (C.GetType() == typeof(TextBox))
{
tbPermValue = C.Text;
}
}
}
You don't need the switch (dr) in checkSame()
I see that you say you have tried setting the following in the "yes" part of your switch statement:
tbUserName.Text = "";
tbUserName.Clear();
But in your "no" part, you don't set tbUserName, but instead you set the variable tbPermValue. From what I can tell, you should also be setting
tbPermValue = "";
in your "yes" part as well to clear that variable, or even just move it out of the switch and have it do that before the dialog closes since you would be setting it in all of the possible switch cases anyways.
I am trying to create some validation for a form I have. There are two text boxes and two radio buttons on the form. My logic for this validation I know is a little rusty at the moment so any suggestions would be great.
Here is the code for what I have so far:
Keep in mind that the int errors is a public variable in the class
Start Button code:
private void btnStart_Click(object sender, EventArgs e)
{
errors = validateForm();
//Here I want the user to be able to fix any errors where I am little
stuck on that logic at the moment
//validate the form
while (errors > 0)
{
validateForm();
errors = validateForm();
}
}
ValidateForm Method:
private int validateForm()
{
errors = 0;
//check the form if there are any unentered values
if (txtDest.Text == "")
{
errors++;
}
if (txtExt.Text == "")
{
errors++;
}
if (validateRadioBtns() == true)
{
errors++;
}
return errors;
}
ValidateRadioBtns Method:
private Boolean validateRadioBtns()
{
//flag - false: selected, true: none selected
Boolean blnFlag = false;
//both of the radio buttons are unchecked
if (radAll.Checked == false && radOther.Checked == false)
{
blnFlag = true;
}
//check if there is a value entered in the text box if other is checked
else if(radOther.Checked == true && txtExt.Text == "")
{
blnFlag = true;
}
return blnFlag;
}
Overall I feel like this can somehow be more stream lined which I am fairly stuck on.
Any suggestions would be greatly appreciated since I know this is such a nooby question.
Well first since you have said that you want to validate for non-entered values, did you consider white spaces as an entry? since someone can just press space and then your validation would pass.
Aside from that, you might want to indicate which textbox they did not fill out or which group they did not click, it seems like you are using web forms so here is a walkthrough http://msdn.microsoft.com/en-us/library/vstudio/a0z2h4sw(v=vs.100).aspx.
If you are using windows forms you can use this walkthrough http://msdn.microsoft.com/en-us/library/ms229603(v=vs.110).aspx.
If you need to keep the existing logic, I would suggest extracting the repeating logic into separate functions and temporary btnFlag is not necessary also as you can return true and return false at the end.
private Boolean validateRadioBtns()
{
if (radAll.Checked == false && radOther.Checked == false)
return true;
else if(radOther.Checked == true && txtExt.Text.Trim().Length == 0 ) //just a quick sample of how you can trim the spaces and check for length
return true;
return false;
}
See the documentation for the validation patterns. You have chosen the explicit validation strategy, for which you would use the ContainerControl.ValidateChildren method and either perform your "Start" action or not.
Windows Forms has dedicated events for validation that allow you to react accordingly for each of your controls. You'll use Control.Validating and Control.Validated events.
So, unless ValidateChildren returns true, you don't need to initiate your "Start" action, i.e. the logic would become trivial.
P.S. you also probably don't need the errors variable as a class member since you return it from your validation function. For showing the error, prefer the "Tell, Don't Ask" idea by separating the error visualization in a separate component.
I have a case where a textbox is validated to make sure it is not blank. However, there are some edge cases where the value should actually be blank. A Nullable<bool> blankText = null variable is set in the constructor. I'm using this code to validate and double check if the value is actually OK to be blank by confirming this with the user:
private void text_Validating(object sender, CancelEventArgs e)
{
if (string.IsNullOrWhiteSpace(text.Text))
{
do
{
if (blankText.HasValue && !blankText.Value)
{
errorProvider.SetError(text, "Blank or whitespace!");
e.Cancel = true;
break;
}
else if (blankText.HasValue && BlankText.Value)
{
errorProvider.SetError(text, "");
e.Cancel = false;
break;
}
else
{
DialogResult result = MessageBox.Show(this, "Field is blank, or contains only whitespace.\nAre you sure you want to use a blank/whitespace?", String.Empty, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
blankText = true;
else
blankText = false;
}
}
while (true);
}
else
{
e.Cancel = false;
errorProvider.SetError(text, "");
}
}
Even if the value for blankText = null it will still set the errorProvider error and fail validation. The MessageBox dialog is never displayed. I know that according to the documentation Microsoft states the following:
Do not attempt to set focus from within the Enter, GotFocus, Leave, LostFocus, Validating, or Validated event handlers. Doing so can cause your application or the operating system to stop responding. Source
Obviously when the MessageBox is displayed the control will implicitly lose focus...so maybe that's why this strange behavior is occurring. Can anyone confirm this? Any suggestions on a better way to handle this case?
In response to #adriano-repetti comment and some additional testing I removed the prompt from the validation event. The full solution I used was to create an additional property of bool? that can be checked if blank values are allowed, disallowed, or undefined. If either disallowed or undefined the validation fails if the value is blank. If the validation fails due to a blank value and the new IsBlankValueAllowed property a prompt is displayed to ask the user to confirm this behavior. I decided to use a Property instead of data within a Tag property since it felt less 'hacked'.
From my code behind, in my update method, I have to ask the user if he wants to overide some particular value. If so, overide, if not, continue with the saving without saving this value.
In my aspx I have this javascript function:
function ConfirmationBox(msg) {
var ovd = document.getElementById("hdnOveride"); //gets a HiddenField
if (confirm(msg) == true) {
ovd.value = "1";
return true;
}
else {
ovd.value = "0";
return false;
}
}
From codebehind, I call this function. And then I check the value of my HiddenField "hdnoveride". If its 1 I save, otherwise I don't.
System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Test", "javascript:ConfirmationBox('overide?');", true);
if (hdnOveride.Value == "1"){
//Save the value
}
The problem is that my code doesn't wait for the user to answer the confirm box before it continues. I have tried using Thread.Sleep() while hdnOveride is not set like this:
while (hdnOveride.Value == "notset") {
System.Threading.Thread.Sleep(500);
}
But it just stops everything, so the popup box never shows when I do this.
How can I tell the system to wait for an answer before continuing with the code?
Thanks!
Ok then, I used Panel instead to ask the question to my users as suggested here: https://stackoverflow.com/a/7677000/454157