"Select all" in all textboxes - c#

private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
textBox1.SelectAll();
}
This works but I have 6 textBoxes. Is there any easier way instead of adding event listeners for each and every textbox? Or a shorthand or something?
Thanks.

Add the same event handler to each and have ((TextBox)sender).SelectAll() to ensure the one that is clicked on is highlighted.
If you're looking for something more generic create a derived class of TextBox containing the same.

Related

Repeat same steps on every textChanged event

I have in my project 5 text boxes.
Every TextBox should accept only digits.
For that I created a function which takes not prepared text and returns the proper one.
Now I'm wondering if there is any simpler way to perform this action on every TextBox, on every TextChanged event without repeating almost same code?
private void TextGoldPack_TextChanged(object sender, EventArgs e)
{
(sender as TextBox).Text = Only_digits((sender as TextBox).Text);
}
private void TextGoldTake_TextChanged(object sender, EventArgs e)
{
//repeat here and on every _TextChanged event
}
If I'm understanding you correctly, just because it's named TextGoldTake_TextChanged, doesn't mean that's the only textbox that can use that code. On the events tab, you can set the TextChanged function for all you textboxes to lead to that function. If it helps, rename it something that doesn't sound textbox-specific such as TextChanged.
Change all the TextBoxes to refer this method upon TextChanged.
Use the sender property to get the actual caller TextBox.

Method for Focus Change

Is there a method which initiates on focus change and can be overridden?
My goal is for the program to fetch closest data automatically from database to input fields whenever user changes his focus/presses enter or tab when on corresponding field. I'm still looking for a way to do this when user selects an item by mouse.
I'm aware that this could be implemented on mouse click but I refuse to believe that there is not a general method for focus change.
What about something like this:
foreach(Control ctrl in this.Controls)
{
ctrl.Enter += new EventHandler(Focus_Changed); // Your method to fire
}
Iterate through all controls and add a enter-event. Bind this handler to your method.
Edit:
Just in case you are wondering why "Enter" and not "LostFocus" or something like that: From my knowledge not every control got focus-events. As I've seen so far "Enter" is presented for all. Maybe there are exceptions. Should be checked out...
You could use Control.Enter event and Control.Leave event for that purpose.
See on MSDN Control.Enter and
Control.Leave.
textBox1.Enter += textBox1_Enter;
textBox1.Leave += textBox1_Leave;
private void textBox1_Enter(object sender, System.EventArgs e)
{
// the control got focus
}
private void textBox1_Leave(object sender, System.EventArgs e)
{
// the control lost focus
}

Pass name of button to click event

I am generating a number of buttons with similar or identical content and was hoping to use the names they are given to differentiate them. As the program is creating the buttons dynamically I can't create a separate event for them all and instead need to be able to grab the name to be able to know which button triggered the event.
Is there a way to pass through the name of a button to the click event it initiates? the sender object seems to contain the content but not the name.
It is for a event like this:
private void Button_Click(object sender, RoutedEventArgs e)
{
//getname of button
Canvas.Children.Remove(//name of button\\)
}
Far as I know, WPF does not even assign anything to the Name property automatically - that's just for developers to assign so we can reference the control.
However, you should be able to just pass in the sender to the remove method since it accepts a UIElement argument which Button is derived from.
Canvas.Children.Remove((Button)sender);
I'm not familiar with WPF. If this answer appears to be a junk, I'll delete it.
In ASP.Net, we can cast to a button to get the sender's information. Something like this -
private void Button_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
button.Name
}
Perhaps getting at the control's name will work for you
private void button1_Click(object sender, RoutedEventArgs e)
{
Control control = (Control)sender;
Canvas.Children.Remove(control.Name);
}

How to call an event handler from one control to the another control?

In Visual C# Form Application, When I Click on the button I want to add to the other controls(like listboxes,labels,textboxes) in same form.
How do I do this?
I have no idea what "to come to the other controls" might mean. But the event handlers in your Form derived class is the switchboard. Implement the button's Click event and have it do whatever you want done with any other controls. A trivial example:
private void button1_Click(object sender, EventArgs e) {
label1.Text = "You clicked the button!";
}
In the form designer, add an event handler to the button's Click event.
The form designer will give you a new method like this; add your code into this method:
private void button_Click(object sender, EventArgs e)
{
// Write some code that uses list boxes, labels, text boxes etc.
}
You question is somewhat unclear, but if you simply want to access other controls on the form, just go ahead and do so:
private void YourButton_Click(object sender, EventArgs e)
{
string someValue = yourTextBox.Text;
// do something with the value
}
If you want to add one event handler to many controls, you can do it.
Just go to properties of control you wish to subscribe, find appropriate event from list (ex: onClick) and choise your existed handler.
But this method will be sutable if events compotable.
Describe your task more detail.

Calling my own event code and custom event code in c#

I have made a WinForms application with a custom richtextbox control.
I am referring it as a Windows control (a dll file) in my project. In my program for the richtextbox I have written functionality inside it's textchanged event.
I want to do additional work only after the textchanged event is fired or in other ways once the text is added to the textbox. Something like I want to call a function foo() in the text_changedevent. It only calls foo and fails to process the underlying textchanged event.
Any way in C# I can make it process the internal textchanged event first, and then look into my text changed event?
think of the scenario I have written the code for mytextbox_textchanged
private void txt_code_TextChanged(object sender, EventArgs e)
{
//some code which will always be called whenever textchanged event occurs.
}
Now I inherit this control in my project say MyApp1. Here I have a label where I want to display the number of lines contained inside my textbox. So I would write
private void my_inherited_txt_code_TextChanged(object sender, EventArgs e)
{
//code to update the label with my_inherited_txt_code.lines.length
}
so my problem was, I first wanted the txt_code_TextChanged event to be called and then do the code written inside my_inherited_txt_code_TextChanged. Which was solved by writing
private void my_inherited_txt_code_TextChanged(object sender, EventArgs e)
{
base.OnTextChanged(e);
MessageBox.Show("foo");
}
Do you mean:
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
// your code here...
}
I cant see why you shouldnt be able to call a method from the text_changed event?
Do you get any errors or what happens exactly?

Categories