Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I activate the TextChanged event from a button Click event 3

Status
Not open for further replies.

tango1948

Programmer
Dec 23, 2006
111
IL
Hi I have the following sub for a text box

Code:
Private Sub userPword_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handels userPword.TextChanged

some code

End Sub

If I press enter from within the text box everything is ok.
How do I activate the above sub from a button click event.

Thanks for any help

David
 
Hello David,

Somewhere in your button click event code place the following:
Code:
userPword_TextChanged(userPword, System.EventArgs.Empty)
This will call the event code with userPword as the sender. Good Luck!

 
My question is a nobrainer as the textchanged event is always executed when the textbox loses focus, but as a matter of intrest how would I do it if the event did'nt fire automaticaly.
 
Alternatively, if you need the code to be called from various places DON'T put it in the TextChanged event handler - instead create a separate sub and call that from wherever you want (including TextChanged).

Additionally, I believe TextChanged is called whenever the text actually changes i.e. as you type.

If the event didn't fire automatically you would need to call the appropriate routine from (at the very least) KeyPress (to handle typing) and LostFocus (to handle pasting or other auto-populating method e.g. bound to a db field).


Hope this helps.

[vampire][bat]
 
Hello David,

Earthandfire's suggestion is better if you will be calling this code from multiple places. Good Luck!
 
Hi earthandfire,
What you say about the TextChange event being fired after every character is true for windows forms but in web forms it only fires when you leave the field.
Thanks for your suggestion, I'll use it in the future

David
 

Say you have a Textbox named userPword and a Button named myButton and if you want to execute the same code when the text changed and/or when the button clicked then here is a quick trick.

Code:
Private Sub userPword_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handels userPword.TextChanged[b][COLOR=red], myButton.Click[/color][/b]

' Some code goes here.

End Sub

Hope this SIMPLE solution will solve your problem.

 
computerjin, the only argument I would have against that is that you would then need to separately handle any object specific code, either by way of If statements or Case statements depending on how many controls this routine handled. As such I would prefer spinning the code off to a standalone sub that could be called by any interested handler, without causing problems for you when writing handler specific code.

Additionally, the name of the sub:

[tt]userPword_TextChanged[/tt]

gives no indication that it also supports the button_click handler, which could cause subsequent confusion.


Hope this helps.

[vampire][bat]
 

Dear earthandfire, to implement my proposed solution, I imposed a condition in my last posting i.e. I mentioned clearly that if David wants to execute same code from both event handlers then this code can be used easily without using Select Case or If...Then..Else.

I provided him a simple solution based on his scenario. If a problem can be solved SIMPLY and by using a feature given by that language then use it (intelligently).

As far as the name of the module goes, I'm sorry for that. I admit it is really confusing :) I just did a quick copy and paste from David's code line. So here is the new event handler (sorry for the name again).

Code:
Private Sub EventHandlerWithoutAnyInterestingName(ByVal sender As Object, ByVal e As System.EventArgs) Handels userPword.TextChanged, myButton.Click

   ' Some code goes here.

End Sub

:)

DAVID:
If your requirement is to execute some code in click event handler before and/or after executing the code in TextChanged eventhandler then the simple and more structured approach has already been told by earthandfire - use a seperate method and call it from wherever you want.

Hope this helps.

 
computerjin, my post was not meant as a criticism, only as a comment. I did notice your proviso, but felt that it was still worth mentioning the potential drawback especially insofar as subsequent code changes would be concerned.

Under the circumstances specified by you, then I agree, your suggestion is neat and simple.

[vampire][bat]
 

earthandfire, I did not take your comments in a bad sense, positive criticism is always welcome. I'm just a student and believe me I have been learning and learning and learning since years. I always learn from other people, books, nature. Believe me, I've learned many great things from your postings.

I'm REALLY VERY SORRY that my words hurt you. I apologize for my words. We are not here to hurt eachother but to give help, get help and to learn something.

Infact, I just wanted to support my argument in my last post. English is not my primary language that sometimes I cannot define well in english.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top