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.
Related
Is it possible to know if any of the textbox values have changed in the application.
I have around 30 textboxes and I want to run a part of code only if, any of the textboxes value has changed out of the 30. Is there a way I can know that.
Each text box will raise an event TextChanged when it's contents have changed. However, that requires you to subscribe to each and every event.
The good news is that you can subscribe to the event with the same method multiple times. The handler has a parameter sender which you can use to determine which of your 30 text boxes has actually raised the event.
You can also use the GotFocus and LostFocus events to keep track of actual changes. You would need to store the original value on GotFocus and then compare to the current value on LostFocus. This gets round the problem of two TextChanged events cancelling each other out.
You can assign an event handler to each of the TextBox's TextChanged events. All of them can be assigned to the same event handler in code. Then you'll know when the text changes. You can set a boolean flag field in your class to record that a change occurred.
This is perhaps on the rough and ready side, but I did it this way.
In the constructor, I created
bool bChanged = false;
In the TextChanged event handler of each control (actually same for each), I put
bChanged = true;
When appropriate, I could do some processing, and set bChanged back to false.
You can also just do this:
In your Constructor:
MyTextBox.TextChanged += new TextChangedEventHandler( TextChanged );
And Then this Method:
private void TextChanged(object Sender, TextChangedEventArgs e){
//Do something
}
try this. Add this code to the load/constructor. no need to specify the event in the XAML explicitly
this.AddHandler(TextBox.TextChangedEvent, new TextChangedEventHandler(TextChanged));
private void TextChanged(object Sender, TextChangedEventArgs e)
{
//ToDO (use sender to identify the actuale text from where it fired }
}
What condition is satisfied when the amount of text in the text box is equal to 10? Currently, the event is firing on every keystroke.
private void textBox1_TextChanged(object sender, EventArgs e)
{
if(textbox1.Length == 10)
{
}
}
I tried this and it is not working because this event is triggered each time I press a button on the keyboard.
That doesn't matter. Your code wouldn't even compile as TextBox has no property Length, though it does have a Text property, which is a string, which has a Length property.
Be aware that there are other ways to change the text (i.e., pasting text in) which will fire TextChanged only once. Also be careful as you may not want to trigger that code if the user presses the backspace or delete keys. Performing actions in textboxes like this is often a bit trickier than one would expect.
You're 99% there, you're missing on thing, the Text property.
if(textbox1.Text.Length == 10)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.TextLength==10)
{
//read DB and display stuff
textBox1.Clear();
}
}
And just like other answerers I'm curious since your current code won't even compile. And if you write it in VS 2010/2012, the autocomplete should already offered the TextLength
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.
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.
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?