onTextChanged Event for many Textboxes c# - c#

I am currently building a prototype for a new system screen, and i am using c# to build this.
The question i have is, i currently have 14 textboxes which are filled from a condition from a couple of other controls on the screen. these 14 textboxes all add up to a total shown in another textbox.
as these textboxes are editable (in case the client wishes to increase the value) (cant go into to much detail but they will) I need to have a firable ontextchange event for when the values change so the total box updates.
however i have a feeling there must be a way of not having to create 14 different events, is there a way that i can have 1 event which fires if any of the 14 text boxes are fired?
thanks
Alan

Yes, you can create a single event - and then subscribe all the text boxes to the same event handler in your class, which raises that single event.
If the clients will need to know which text box has changed, you'll need to think about how to best communicate that with them - you may want to create your own subclass of EventArgs. Otherwise, just a simple EventHandler or EventHandler<EventArgs> should work fine.

Yes, let's say your text boxes are called textBox1, textBox2, ... if you have automatically created method "textBox1_TextChanged" for event "TextChanged" of textBox1, you can easily use this method for all the other text boxes...
For example in Designer, just select yout "textBox2" component, go to Events tab on Properties Window and instead of creating new event by double clicking in the editable field next to the "TextChanged" value, just use the drop-down menu to select the "textBox1_TextChanged" method created (automatically) before.
If you need to decide which textBox raised the event, you can use the "sender" parameter of the method.

Related

Don't have Click event in TextBox in WPF C#

I just want to add simple thing, wherever i click on TextBox I want to delete text that is inside. The thing is I DON'T have in list of events, event called Click.. Is this even possible? Or just I need to install some add on. Buttons are fine, they have Click event.
MouseButtonLeftUp would work if you only are concerned with mouse clicks. With that said, what if someone tabs into the box or focus is entered by other means?
At that point you may want to look at the GotFocus event. Any time the TextBox receives focus, you can handle the event.
TextBox has events called MouseLeftButtonUp and MouseLeftButtonDown, you can use them.

Add Default Event Handler For All Selected Controls

I have a fair number of buttons on one form and I want to add separate click (default) event handler for each. If I double click one control it adds the empty click event handler to the code behind.
Is there anyway for me to select, say 20, buttons and add a separate default handler for each?
I have tried Shift double click, Alt double click Ctrl double click but they all do the same, add one event handler and assign it to all the controls.
Edit
I get that I could have the same event handler for all the buttons, work out which one was click etc etc. This is not what I am asking. I just wanted a quick way to add the empty click handler for all selected buttons.
Sorry. Misunderstood the question, so removing the old answer as it's not really applicable.
My advice would be open the Form cs file in a text editor, find the region where the control/event bindings occur and copy, paste a particular Button line updating the event and id after.
Pretty laborious still.

SelectedIndexChanges fires before TextChanged event

I have a grid which contains multiple columns/rows with ASP TextBox controls. In addition to this, above the grid I have a radio button list which contains years.
The user can enter numbers into the TextBoxes and then click an item on the RadioButton list to save the information then switch years or click an update button to just save the information and remain viewing the data.
If I click the 'save' button the textchanged event handler fires and I know what rows on my grid had something changed, then I update my DB and then get the data again to display to the user.
If however I click the radiobuttonlist to switch years the SelectedIndexChanged event fires but the TextChanged event handler does not run because the save and get data runs first, rebinding the grid and eventhandler.
This appears to me to be something to do with the way events run in .net, does anyone know how can I get the textchanged event handler to run first when clicking a radio button list?
I'm using VS2005, .Net 2.5, ASP.Net, C#
Thanks in advance
Your question is not clear. Anyway i can understand that you have problem with the event handlers and how they are called.
I want to know what is the need of calling the textchanged event and SelectedIndexChanged. Even they are not called your data would be get saved perfectly.

C#: click (where applicable) versus validating event

I am recently working on windows forms with visual C# and I have a bunch of radio buttons grouped together.
I needed to call some methods if the radio button is clicked and also do some validation.
So I have two methods,
public void doSomeStuff()
public bool valRadioButton1()
I can call doSomeStuff() in the click event and the latter in the validating event of the radiobutton but I could also just call both in either the click event or the validating event.
My question is that are there any advantages and disadvantages as to what event I would use to call these? Or is there any particular way is more efficient. Right now it seems that both events would do the exact same thing so why use one or another or both.
Radio buttons are kind of strange in combination with the conventional validation. The validating event seems to be designed to allow you to validate a value once when the user is done entering a value instead of every time the value changes as the user is entering it. This makes sense for a textbox where you want to look at the completed text instead of after each character that the user types. But it's a little more obscure for radio buttons. In fact I think you should generally avoid the validating event of radio buttons and instead use the validating event of the container (radio buttons should always be in an embedded container). This allows a keyboard user to select/move through different options to arrive at the one they want without repeated validations as they move through the options. Then when they move focus out of the group box (or whatever container you used), you can validate the whole option group at once. This behavior is more consistent, then, with that of other controls' validation. In fact I see very little purpose to using the validating event on individual radio buttons. The only reason I see is if you want to cancel the user's new selection without causing extra click events. But be aware that when no radio button is selected and the user first clicks on one, no validating event will occur! No radio button lost focus and validating events only occur when a control loses focus. So this is why I think you should just avoid the validating event on radio buttons and just use the validating event of the container or the click event of a radio button.
Also, I think if you want to be nice to keyboard users, you should keep the validation logic separate from the click logic and use the events appropriately. Things like enabling controls based on which option is selected would belong in the click event of a radio button, but error and warning messages about the currently selected option should go in the validating event of the container.
Edit: You asked specifically about when one event occurs and not the other. I would add this information in response to that:
Validate will be called without calling click if the code is what causes the selected radio button to change, assuming focus then passes to the radio button (or container, if you are using the container's validate event).
Click will be called without (or should I say before) calling validate if no radio button was selected and the user then clicks on one (validate only occurs when the control loses focus). Validate will eventually occur for the clicked option, though.
Click will be called without (or should I say before) calling validate if your validate handler is not linked to the specific option that was previously selected or its container. It will be called for the option that is now selected (and the container) when this option (or the container) loses focus, though.
Click could be called without validate being called if your code that looks at the value doesn't require the selected option to lose focus before looking at it.
The validation event exist for when a controls value has changes, this is decoupled from how the controls value was changed. Was it changed because a datasource was refreshed, was it changed by an end user, was it changed on a timer? Doesn't matter!
I would use the validating event to evaluate if something is valid. Even if you know that there is "no way ever that it could happen any other way".

C# wpf: Need to add mouseclick event to certain text within a textbox

I have a textbox with a paragraph of information. There are certain words in the paragraph that i want the user to be able to click on, and when clicked, a different textbox is populated with more information.
I know that you can have the event for the whole textbox, but that isn't want i want. I only want to call that event when certain words within the box are clicked.
Sorry, but I don't think you're going to be able to do it like that. The text in a textbox is a singular value type string, there are no objects for "words" or "letters" which would be necessary to raise such an event.
Your best bet will be to subscribe to the textbox keypressed event and parse the string each time a new letter is added. It may not be beautiful, but it's not a huge overhead.

Categories

Resources