Use enter key to send text to a rich text box - c#

I am currently using winforms and I want to send the text from a textbox to a rich text box by pressing enter. Here is the code I currently have
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
richTextBox1.Text = textBox1.Text;
e.Handled = true;
}
}
What am I missing? Thanks in advance.

Did you subscribe to the event KeyDown? You can add a break point in textBox1_KeyDown to check it.
If the break point is not triggered, you can delete textBox1_KeyDown and double click the Button to re-generate it. Then will subscribe to the event "KeyDown" automatically.
Another way is that you can subscribe to events through code.
public Form1()
{
InitializeComponent();
textBox1.KeyDown += textBox1_KeyDown;
}

Related

How to change text of Label while typing in TextBox

Please I'm new to C#, I created a textBox and a label. What i am expecting is, if I type a value into the textBox, I want it to display on the label and if I change the value it should also change immediately on the label.
it work with the code below and i press enter key
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
label1.Text = textBox1.Text;
}
}
But I want it without press Enter/Return Key on keyboard.
Thanks for understanding
This works for VisualStudio
Select your TextBox in the Designer, go to the it's properties and click on the events (teh icon with the lightning). Then make a double click on the event that is called: TextChanged.
This creates a new function, that will always be called when the text of your TextBox changes. Insert following code into the function:
private void textBox1_TextChanged(object sender, EventArgs e)
{
TextBox tb = sender as TextBox;
label1.Text = tb.Text;
}
That's it.
label.DataBindings.Add("Text", textBox, "Text");
textbox KeyDown/Up/Press events may help.
For example
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
label1.Text += e.KeyData.ToString();
}

issue with textbox event handlers

I am still learning how to do with event handler. What I want is: When I click the txtMonday to get focused, then I click the remove button to clear this selected textbox. Problem is: when I click the remove button for the selected textbox, all the unselected textboxes are clear. I only want to remove the selected textbox. How to solve this problem? Your code example much appreciated. Thanks! I am using WPF and C#.
private void btnRemoveClick(object sender, RoutedEventArgs e)
{
TextBox text = new TextBox();
text.GotFocus += new RoutedEventHandler(txtMonday_GotFocus);
txtMonday.Clear();
text.GotFocus += new RoutedEventHandler(txtTuesday_GotFocus);
txtTuesday.Clear();
}
private void txtMonday_GotFocus(object sender, RoutedEventArgs e)
{
}
private void txtTuesday_GotFocus(object sender, RoutedEventArgs e)
{
}
This should do what you want. I suggest you do some more studying about C# though, as your code shows some fundamental misunderstandings.
//you'll need a variable to store the last focused textbox.
TextBox txtLast;
public MainWindow()
{
InitializeComponent();
//add an event for all the textboxes so that you can track when one of them gets focus.
txtSunday.GotFocus += txt_GotFocus;
txtMonday.GotFocus += txt_GotFocus;
txtTuesday.GotFocus += txt_GotFocus;
txtWednesday.GotFocus += txt_GotFocus;
txtThursday.GotFocus += txt_GotFocus;
txtFriday.GotFocus += txt_GotFocus;
txtSaturday.GotFocus += txt_GotFocus;
//default to clearing sunday to avoid exception
//you could also let it clear a new TextBox(), but this is wasteful. Ideally,
//you would handle this case gracefully with an if statement, but I will leave that
//as an exercise to the reader.
txtLast = txtSunday;
}
private void txt_GotFocus(object sender, RoutedEventArgs e)
{
//whenever you click a textbox, this event gets called.
//e.source is the textbox, but since it is is just an "Object" we need to cast it to a TextBox
txtLast = e.Source as TextBox;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//this will clear the textbox which last had focus. If you click a button, the current textbox loses focus.
txtLast.Clear();
}

WPF event for clicking textboxes in C#

How can I raise an event while clicking a textbox? I'm having trouble finding references for events for WPF in C#.
The idea is to have textboxes fire an event when clicked. For example, let's say as soon as I click a textbox, notepad is executed.
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
hello = Process.Start("notepad");
}
private void Click(object sender, MouseButtonEventArgs e)
{
/* if (e.LeftButton == MouseButtonState.Pressed)
{
hello = Process.Start(#"notepad");
}*/
}
For text events use TextInput event and read entered character from e.Text
private void yourTextBox_TextInput(object sender, TextCompositionEventArgs e)
{
if (e.Text == "K")
{
}
}
for mouse events use MouseDown/MouseUp
Sometimes MouseDown/MouseUp won't work on TextBox, then use this one:
http://msdn.microsoft.com/en-us/library/system.windows.uielement.previewmouseup.aspx
MouseLeftButtonDown event can be raised when clicking on textbox. This event will not fire by default on textbox. We need to use UIElement.AddHandler().
For e.g:
XAML:
<Textbox X:Name="Name_Textbox" MouseLeftButtonDown="Name_Textbox_MouseLeftButtonDown"/>
In the class Constructor:
TextBox_Name.AddHandler(FrameworkElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler("Name_Textbox_MouseLeftButtonDown"), true);
Add Event in class file:
private void Name_Textbox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// Your logic on textbox click
}

How can I override the default button on a form when focus is in a TextBox

I have a winform form which has typical OK and Cancel buttons.
The OK button is set as the default button. When the user presses the ENTER key, the form is closed.
The form also has a text box which has a Search button beside it. The text box allows the user to enter search text and the Search button initiates the search function.
I would like the user to be able to press the ENTER key when the text box has the input focus and have the search function activate.
The problem is that the form is grabbing the ENTER key event before the text box's handler gets it.
EDIT: I've tried using the following event handlers, but they never get hit, and the form closes when the Enter key is hit:
private void txtFilter_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Search();
e.Handled = true;
}
}
private void txtFilter_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Search();
e.Handled = true;
}
}
I've also tried enabling the KeyPreview property on the form and implementing a KeyDown event handler for the form, but the Enter key never seems to cause any of these to be hit.
What is the best way to accomplish this?
Try handling the Enter and Leave events of the TextBox to clear out your form's AcceptButton property:
private void txtFilter_Enter(object sender, EventArgs e) {
this.AcceptButton = null;
}
private void txtFilter_Leave(object sender, EventArgs e) {
this.AcceptButton = closeButton;
}
Then you can just process your KeyUp event as your want:
private void txtFilter_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Search();
}
}
Add a KeyDown event handler to the textBox and then add this to it
if (e.KeyCode == Keys.Enter)
{
btnSearch.PerformClick();
}

C# Key Press; display key in Label

I have a form (Form1) and a label (lblTest)
What code do I need to insert so when any key is pressed, the key is displayed in the label? This event should take place when the form is selected
E.g. if the user presses g, a g is displayed in the label.
I have tried some code in the Form_KeyDown event, but I can't get it to work.
I'm currently looking at this.
You need to add
form1.KeyPreview = true;
(or set in the designer)
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx
Try the below:
...
myForm.KeyPreview = true;
...
private void CommsTesterUI_KeyDown(object sender, KeyEventArgs e)
{
label1.Text = e.KeyCode.ToString();
}
Try with this code if you need to build a string:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
lblControl.Text += (char) e.KeyCode;
}
else, if you need to show only the button pressed:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
lblControl.Text = ((char) e.KeyCode).ToString();
}
Obiosly, the focus must be on the form.

Categories

Resources