Clearing output if input is changed - winforms

I have two text boxes on my form. The first text box calculates an amount when it is clicked and the amount total is displayed in the second text box. I'm trying to create an event handler that will clear the text in the second text box if the value entered in the first is changed.
For example, You can only go on the ride if you are 5 years old. The user enters 5 in 1st text box. The second text box displays "You can ride!". Now if the user changes the number 5, I want the "You can ride!" to be cleared.
I don't have any code, because I'm not sure which text box you create an event handler for, the one that takes input or the one that displays the output. Does anyone have any suggestions or what I can search on Microsoft's page to know more about clearing? The only thing I was about to find so far was this example: textBoxName.Clear(); but I'm not sure where to put that. Any suggestions? Thank you.

Yes, you can use TextBox.Clear Method to clear the text in a TextBox.
The following is a simple example to determine whether the value in TextBox1 is "5" and modify the value in TextBox2 in real time.
The demo uses Int32.TryParse Method to detect if the value in TextBox1 is a number.
public Form1()
{
InitializeComponent();
textBox1.TextChanged += textBox1_TextChanged;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
int number;
bool success = Int32.TryParse(textBox1.Text, out number);
// input is a number
if (success)
{
// if input is 5
if (number == 5)
{
textBox2.Text = "You can ride!";
}
// else clear TextBox2
else
{
textBox2.Clear();
}
}
// input is not a number, re-input
else if (!string.IsNullOrEmpty(textBox1.Text))
{
// Clear wrong input
textBox1.Clear();
MessageBox.Show("Please input a number!");
}
// input is empty
else
{
textBox2.Clear();
}
}

Related

how to use textChanged event when from barcode reader

I am using a Barcode reader to get bar code from products .
i get the code in a textfield . and i put textchanged event on that textbox .
but the problem is that when bar code puts it value ( for example if bar code is 5 digit ) then textchanged event is fired five times .
how to get ride of this thing ???
You should be able to program your Barcode reader to output a prefix character and a suffix character (one output before the scanned value and one afterwards). Let's say that you set it up to output an asterisk (*) before the scanned data value and output a carriage return (CR) afterwards. Attach a handler to the TextBox.PreviewTextInput Event and listen out for the asterisk character:
private void PreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (e.Text == "*")
{
e.Handled = true;
// Data input has started
}
}
You can use this to pop up a message saying 'Scanning...', or anything else that you require. Next, attach a handler to the TextBox.KeyUp Event and listen out for the Enter key:
private void KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
string scannedValue = ScanTextBox.Text.Replace("*", string.Empty);
// Do something with scannedValue
}
}
Now the scannedValue variable should contain the scanned barcode value.

textbox validated method does not work while assigning value to textbox

I am using c#.net 2.0 winforms. I use errorprovider control in my form to validate a textbox. While I programatically assign value to that textbox. textbox validated method does not take the value from the textbox or considers it a blank value. How can I validate my textbox by without entering value in the textbox. Here is the code
private void textBox6_Validated(object sender, EventArgs e)
{
bTest6 = txtRegExPinIsValid(textBox6.Text);
if (bTest6)
{
this.errorProvider1.SetError(textBox6, "");
}
else
{
this.errorProvider1.SetError(textBox6, "This field must contain Exactly 6 digits");
}
}
private bool txtRegExPinIsValid(string textToValidate)
{
Regex TheRegExpression;
string TheTextToValidate;
string TheRegExTest = #"^\d{6}$";
TheTextToValidate = textToValidate;
TheRegExpression = new Regex(TheRegExTest);
// test text with expression
if (TheRegExpression.IsMatch(TheTextToValidate))
{
return true;
}
else
{
return false;
}
}
While performing update operation I fill the textbox with values from the ms access table. If the value is correct, just leave it otherwise I have to update it. Please help me. Thanks in advance
I would recommend placing the validation code in a separate method. Call that method from both the Validated event and the location in your code where you need to programatically validate, as shown below:
// Call this from wherever you need to validate a TextBox
void PerformValidation(TextBox textBox)
{
bTest6 = txtRegExPinIsValid(textBox6.Text);
if (bTest6)
{
this.errorProvider1.SetError(textBox6, "");
}
else
{
this.errorProvider1.SetError(textBox6, "This field must contain Exactly 6 digits");
}
}
private void textBox6_Validated(object sender, EventArgs e)
{
PerformValidation(textBox6);
}

Getting same numbers within two event methods on windows form

I have created a from which takes the user input and compares it to 100 randomly generated numbers. Once they click the guess button the result is shown. To aid the user I am supposed to give them a hint when they hover the mouse over a label, the hint should be either 3 higher or 3 lower than the actual number. I cannot figure out how to get the random number generated when the user hits the guess button to equal the number as the hover event. Sorry for all the code, any help would be appreciated.
Here is how I am creating my random number:
public partial class Form1 : Form
{
int[] rndArray = new int[100];
public void getNumbers()
{
Random random = new Random();
for (int x = 0; x < rndArray.Length; x++)
{
rndArray[x] = random.Next(1, 100);
}
}
Here is the guss button event:
private void Guess_Click(object sender, EventArgs e)
{
getNumbers();
for (int x = 0; x < rndArray.Length; x++)
{
if (Convert.ToInt32(textBox1.Text) == rndArray[x])
{
result.Text = "You Win!";
correct.Text = "Correct: ";
}
else
{
result.Text = "Sorry - you loose; the number is: " + rndArray[x];
incorrect.Text = "Incorrect: ";
}
}
And the mouse hover event:
private void mouseHere_MouseHover(object sender, EventArgs e)
{
getNumbers();
for (int x = 0; x < rndArray.Length; x++)
hint.Text = "It's not " + (rndArray[x] +- 3);
}
I see three possible issues.
First, at the beginning of your mouse hover and guess click functions, you call GetNumbers, which generates 100 numbers and assigns them to the array. You should only generate them once per game. I would recommend calling it once at the beginning of each game (perhaps in the FormShown or FormLoad event handlers), and not calling it again until the next game begins. Otherwise, the numbers will keep changing.
Second, inside your mouse hover function, you have a for loop that assigns the text to "hint" 100 times. The first 99 hints will probably not be accurate, as the end of your mouse hover event will display the hint for the very last number. You will need to identify which array element to give a hint for, and assign it to the appropriate hint display.
Third, the +- operator is not an actual operator in C#. If this is actually compiling and running, it's probably interpreting it in a manner such as hint.Text = "It's not " + (rndArray[x] + (-3); I would recommend using a Random object to generate a number, then using whether it's odd or even to determine whether to add or subtract. Just make sure you don't reassign the array.
EDIT: With regards to figuring out whether to add or subtract from the hint, make sure you don't randomly generate a hint each time you hover; do it once for that number and store the hint. Otherwise, hovering over it a few times will show both possible hints.

how can I know if backspace or delete was pressed in TextChanged event of textbox

I use the TextChanged event of a textbox.
for instance I have the text in the textbox:
"a b"- 2 spaces between a,b
I go in the middle of the 2 spaces, and hit backspace. I can not check by comparing the new text with the old which key was pressed (if backspace or delete was pressed, the newtext is the
same "a b"-1 space between letters.
How can I check which key was pressed?
Why do you care? If you're handling the TextChanged event, you're not supposed to care what about the text was changed, just the fact that it was.
If you need to care about what specifically was changed, you need to handle a lower-level event, like KeyDown:
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.Back || e.Key == Key.Delete)
{
// The user deleted a character
}
}
I know this is an old question, but maybe someone will find this helpful.
I used an integer, that counts length of current characters. It updates with the event.
Every time, the event is triggered, I check, if the length of the counter is longer than the current length of the text. If it is, we know, there was something deleted.
private int counter = 0;
private void TextChangedEvent(object sender, TextChangedEventArgs e){
if(counter > textbox.text){
counter = text.Length;
//deleted
}
else{
counter = text.Length;
//added}

How can a user enter a newline character in Silverlight?

I'm trying to get a screen in silverlight where the user can enter their own text and add line breaks as neccesary. The problem is that whenever they hit return inside of a text block, nothing happens. Is there some way around this?
Thanks
Nevermind, I figured out you needed to set the AcceptsReturn property to true.
[Edit: For whoever voted my answer down -- the question was "how do you capture enter in a text block". The text block element does not have an AcceptsReturn attribute.]
You should be able to trap for the Enter key and insert a newline character.
private string textBuffer = "";
private void TextBlock_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
textBuffer += Environment.NewLine;
}
else
{
textBuffer += e.Key.ToString();
}
Text.Text = textBuffer;
e.Handled = true;
}
About the answer from another Timothy, TextBlock is not for typing. Are you sure you really don't want to use a TextBox instead?

Resources