how to use textChanged event when from barcode reader - wpf

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.

Related

WPF - set text to Window as TextBox

i would like catch text in WPF Window. For example, application window will focus and I am going to write without focus in some textbox. My application has input from barcode reader. I want users to be able read a barcode without click to some textbox - for faster work. It is possible? I tried event PreviewKeyDown but application catch only first char. My barcodes are in format #12000012546 and barcode reader emulate numeric keyboard (Shift + num) - for example instead of char # KeyEventArgs return only "System". This is my code:
static string text = string.Empty;
private void MainWindow_OnPreviewKeyDown(object sender, KeyEventArgs e)
{
text += e.Key.ToString();
if (text.Length == 12)
{
MessageBox.Show(text)
text = null;
}
}
Thanks for advice
If your Barcode reader is really doing key presses (Shift + number) to do your # character, then check this out. This was tested on an Spanish Keyboard, so the # character is on my 3 key (Key.D3)
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
var regex = new Regex("(#)|([0-9])");
var keystr = e.Key.ToString();
if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
{
if (regex.Match(keystr).Success)
if (e.Key == Key.D3) textBox1.Text += "#";
}
else if (regex.Match(keystr).Success)
textBox1.Text += keystr.Replace("D","");
}

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

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}

Simulate Keyboard events on WPF Textbox

I am have textbox and 4 button in my page (A, B, Delete and Enter). If i click the button it has to send key event to the textbox.
Problem:
No action is happening on the textbox.
Code:
void buttonElement_Click(object sender, RoutedEventArgs e)
{
// create variable for holding string
String sendString = "";
// stop all event handling
e.Handled = true;
// set sendstring to key
sendString = ((Button)sender).CommandParameter.ToString();
// if something to send
if (!String.IsNullOrEmpty(sendString))
{
// if sending a string
if (sendString.Length > 1)
{
// add {}
sendString = "{" + sendString + "}";
}
// set keyboard focus
System.Windows.Input.Keyboard.Focus(this.txtSearch);
System.Windows.Forms.SendKeys.SendWait(sendString);
}
}
Geetha.
Daniel Rose is right. Wouldn't it be easier this way?
You take the Text property of the textbox and on the buttonclick you append the right character to this string of when the delete-button is pressed just erase the last character of this string.
Why are you trying to send a key event to the TextBox, instead of setting its Text property?

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