textbox sends enter key for each number entered - c#-4.0

Im using a textbox to accept barcode input and then the enter key to do stuff with that input. But its sending the enter key once for each number entered. Is there a way to have the enter key sent only once? Or maybe supress the other enters?
private void txtBscanned_KeyDown(object sender, KeyEventArgs e)
{
this.txtBscanned.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OnKeyDownHandler);
}
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show(txtBscanned.Text);
}
}

Why are you assigning a keydown event on the textbox itself? You should assign the event on the constructor or using the designer.
public Form1()
{
InitializeComponent();
txtBscanned.KeyDown += new KeyEventHandler(this.OnKeyDownHandler);
}

Related

Excel AddIn Event: How to display a dialog asking the user whether the workbook should be saved?

Before the user is allowed to save an Excel workbook that is "invalid", I would like to inform them that they have managed to damage it and give them the opportunity to abandon all modifications.
By pressing a few times the [TAB] key, I was able to generate most of the code below:
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace WithBeforeSave
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.WorkbookBeforeSave += new Excel.AppEvents_WorkbookBeforeSaveEventHandler(Application_WorkbookBeforeSave);
}
void Application_WorkbookBeforeSave(Excel.Workbook Wb, bool SaveAsUI, ref bool Cancel)
{
MessageBox.Show("How do I put the proper dialog here?");
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
}
}
Therefore, all I need is the C# equivalent to this:
This example prompts the user for a yes or no response before saving the workbook.
TIA
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace WithBeforeSave
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.WorkbookBeforeSave += new Excel.AppEvents_WorkbookBeforeSaveEventHandler(Application_WorkbookBeforeSave);
}
void Application_WorkbookBeforeSave(Excel.Workbook Wb, bool SaveAsUI, ref bool Cancel)
{
if (DialogResult.No == MessageBox.Show("Are you sure you want to save the DEFECTIVE, INVALID workbook?", "Example", MessageBoxButtons.YesNo))
{
Cancel = true;
MessageBox.Show("Save is canceled.");
}
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
}
}
That's it. I found the answer here:
This example prompts the user for a yes or no response before saving any workbook.

RichEditBox How to know Which line the cursor is in

How can i know If user click Enter in keyboard in RichEditBox?
this code does not work
private void Editor_KeyDown(object sender, KeyRoutedEventArgs e)
{
var dia = new MessageDialog(e.Key + "");
dia.ShowAsync();
}
also This code does not work
private void Editor_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.OriginalKey == (VirtualKey)(char)13)
{
NumberEditor.Text += Convert.ToString(_LineNum) + Environment.NewLine;
++_LineNum;
}
}
How can i get line of row in RichEditBox and how can i change text from code in RichEditBox?
I want to make an Editor and any help is appreciated.
Regards
Look this method:
private void ChangeLine()
{
var textRange = MyRichEditBox.Document.GetRange(MyRichEditBox.Document.Selection.StartPosition, MyRichEditBox.Document.Selection.StartPosition);
textRange.Expand(TextRangeUnit.Line);
//Change line size.
textRange.CharacterFormat.Size = 30;
//Center the paragraph
textRange.ParagraphFormat.Alignment = ParagraphAlignment.Center;
//this will change text of the range
textRange.Text = "My new text";
}
From Document you have to get ITextRange. After that you can expand it to TextRangeUnit.Line and get whole line. Now you can change the style and text of the line.
Try this code for KeyDownEvent:
public sealed partial class TextEditPage : Page
{
private readonly KeyEventHandler _keyDownHandler;
//Constructor
public TextEditPage()
{
this.InitializeComponent();
this.Unloaded += OnUnloaded;
//Add keydown event
this._keyDownHandler = OnKeyDown;
RtfBox.AddHandler(KeyDownEvent, this._keyDownHandler, true);
}
private void OnKeyDown(KeyRoutedEventArgs e)
{
//enter your code here
}
private void OnUnloaded(object sender, RoutedEventArgs routedEventArgs)
{
this.RemoveHandler(KeyDownEvent, _keyDownHandler);
}
}
Don't forget to remove handler in Unloaded.

c# Avoid beep on KeyDown press?

I'm having a problem removing the beep sound when pressing the Enter key in a textbox.
This doesn't work:
private void textBox3_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
button1.PerformClick();
}
}
I found an answer!
private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
//Pressing ENTER
if (e.KeyCode == Keys.Enter)
{
button1.Select();
e.SuppressKeyPress = true;
SendKeys.Send("{ENTER}");
}
}

How to create Right Click and Middle Click event on custom hyperlink button?

I am creating my Custom hyperlink button deriving from Silverlight HyperlinkButton I want to create Right Click and Middle click event on it. Can some help me please.
Thanks,
Gobind
I would add a couple of events (like MiddleClick and RightClick), then handle the MouseUp (or MouseDown, if you want to intercept on the down), then fire one of the two events depending on the details of the MouseUp event. For example:
public MyControl()
{
InitializeComponent();
MouseUp += OnMouseUp;
}
void OnMouseUp(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Middle)
{
OnMiddleClick(e);
e.Handled = true;
return;
}
if (e.ChangedButton == MouseButton.Right)
{
OnRightClick(e);
e.Handled = true;
return;
}
}
public event MouseButtonEventHandler RightClick;
protected virtual void OnRightClick(MouseButtonEventArgs e)
{
var handler = RightClick;
if (handler != null) handler(this, e);
}
public event MouseButtonEventHandler MiddleClick;
protected virtual void OnMiddleClick(MouseButtonEventArgs e)
{
var handler = MiddleClick;
if (handler != null) handler(this, e);
}

C# read the text of dynamically created text box from other methode

If I created a textbox dynamically like this:
private void Form1_Load(object sender, EventArgs e)
{
TextBox tb=new TextBox();
...
this.Controls.Add(tb);
}
and if I have a button on my form and I want to read the text of the textbox from within the button click event handler
private void button1_Click(object sender, EventArgs e)
{
if(**tb.text**=="something") do something;
}
The problem is that I can't find the textbox control in the button handler.
Thank you in advance
you have to declare texbox out of the method,it has to be global. then you can reach textbox object
TextBox tb;
private void Form1_Load(object sender, EventArgs e)
{
tb=new TextBox();
...
this.Controls.Add(tb);
}
Declare TextBox as your Form class private member.
You could iterate through the Controls collection of an enumerable object like a TabControl, assuming this is a Windows form. Here's some from a project I'm working on, adapted for TextBox:
foreach (TabPage t in tcTabs.TabPages)
{
foreach (Control c in t.Controls)
{
MessageBox.Show(c.Name); // Just shows the control's name.
if (c is TextBox) // Is this a textbox?
{
if (c.Name == "txtOne") // Does it have a particular name?
{
TextBox tb = (TextBox) c; // Cast the Control as a TextBox
tb.Text = "test"; // Then you can access the TextBox control's properties
}
}
}
}
private TextBox tb = new TextBox();
private void Form1_Load(object sender, EventArgs e)
{
this.Controls.Add(tb);
}

Resources