Suppressing events such as text change

how do i or can i suppress events such as text changed. cos i want to set text in code and hence so not need it to be validated and stuff


Answer this question

Suppressing events such as text change

  • Chidu

    No such thing as "the standard method" only methods.


    If you use a flag method then execution WILL pass to the handler each time the event is fired, to determine the state of the flag, if you remove the handler (non-standard method I know but... sigh...) the handler will NOT execute at all thus you can be certain that your application is not WASTING cycles doing something you do not want done and it does not need to do.

    Either way though works.


  • rokohl

    Hey THANKS the removeHandler and add works. Thanks after removing the handler i still need to add it back right but no offence SJWhiteley but i don't really understand the loading flags thingy. but i will continue to read about it. since its a standard, but remove handler is easier and it works, more importantly it doesn't give me problems so far.

    my code now :
    If txt_mask1.Text.Length <> 0 ... ... Then
    Dim mask As mask = New mask(txt_mask1.Text,...)
    RemoveHandler txt_mask.TextChanged, AddressOf txt_mask_TextChanged
    txt_mask.Text = mask.SlashNotation
    AddHandler txt_mask.TextChanged, AddressOf txt_mask_TextChanged

  • Martin Mason

    Not true: this has been a standard coding method in VB since VB had events. The whole point of developing standard coding methods is it reduces errors, and also represents a method that works in practice. Sure, you can add and remove events, but it's overkill for such a simple task. Just because you can do it, doesn't mean you should.



  • St.Vampyre

    The standard method has been to use a flag to indicate that you are setting the text through code. This usually ocurrs when the form (for example) is loaded or initialized.

    There would be a class level flag:

    private LoadingFlag as boolean

    In the load/new event, you would set it true prior to setting all your boxes:

    LoadingFlag = True ' Start Loading...
    ' Set all Text boxes, etc. that raise 'changed' events
    LoadingFlag = False ' Done

    In the Changed event for the control you would look at this flag.

    if LoadingFlag = True then Return



  • cosminb

    You could use the RemoveHandler and Addhandler routines.

    RemoveHandler <control>.<event>,AddressOf <function name that is current handler>

    ... Do what you need to do...

    AddHandler <control>.<event>,AddressOf <function name of the handler>


  • jsmircic

    err... but will that work cos i want to have the user select a option from a drop down combo box, then depending on the selected index, set the value of the corresponding text box. to a value (may not be one eg. selected index = 1 then set txtBox.text = "12", if selected index = 1 then set txtBox.text = "24") something like that. but without this my program still works anyway. and thanks alot for being here to help.

  • Cannan

    SJWhiteley wrote:

    There is no 'course material' because it's not taught (it seems C is not taught anymore). It's one of those things you learn through experience.

    There is a similar argument for naming conventions: there is no standard (sic) naming convention, but there are many 'standards' you can use: use them as you see fit, or make up your own (that's all they are - someone made it up). Same with this 'flag' convention: someone made it up, it works, so people followed it. So it becomes the standard way of doing things. Similarly with loop variables: the standard is to use letters such as i, j, k. Why There are various reasons. Do you have to follow his No. Call your loop variable MyFunkyLoopVariableThatDoesAThing if you like.

    You are free to disregard such experience as you see fit. Personally, I don't care, until I have to look at code which disregards such conventions: unless you have come up with a better way, of course.


    Now it is a convention and a standard...

    I have no objection to any method that works and when offering answers I will of course, like everyone else, give the ones I use regularly and that work for me. From time to time I see another method and appreciate the simplicity or conciseness of it and may then adopt it in my own coding, one of the reasons I look at answers given in these forums is to broaden my understanding and see different ways of achieving things in VB.

    I do object, as severely as you appear to object to code that does not comply with your view, when such methods are given with a claim to being a standard that a group uses so you must comply with it as being the only way especially if you want to be with everyone else. Have the grace to simply put your method forward without scorning any other either subtly or directly, a simple "you could also do it this way.." is sufficient and the questioner can adopt the method suited to his/her way of thinking without the pressure of having to choose an imposed non-standard standard.



  • mabxsi

    Hi,

    Why

    Why not just a label or set the textbox read-only property to true instead

    No worries about textchanged then.

    Regards,

    S_DS



  • SCRunner

    Put the handler back after you have done what you need to do without the event being handled by your handler.




  • ureyes84

     

    Hi,

    You could use the tag property of the textbox to hold a string value as the default value.

    E.G. textbox1.tag="myDefaultTextHere"

    Then validate it and replace it with.>>

    textbox1.text=textbox1.tag

    Would that do it for you

    You could force a default value and in the textbox1_textchanged event say

    MsgBox("You are not allowed to change this text this way, okay.")

    textbox1.text=textbox1.tag

     

    Regards,

    S_DS

     



  • Tailor

    err, actually i thought of wanting the user to be able to set a default value to the text box depending on a combo box input, then he can change the text, (which then i want to validate). but i think this is extra and if it can't be done then its ok. after all i am just learning

  • Simonsbs

    Can you point me to the literature or course material that sets this standard.




  • Erik BN

    There is no 'course material' because it's not taught (it seems C is not taught anymore). It's one of those things you learn through experience.

    There is a similar argument for naming conventions: there is no standard (sic) naming convention, but there are many 'standards' you can use: use them as you see fit, or make up your own (that's all they are - someone made it up). Same with this 'flag' convention: someone made it up, it works, so people followed it. So it becomes the standard way of doing things. Similarly with loop variables: the standard is to use letters such as i, j, k. Why There are various reasons. Do you have to follow his No. Call your loop variable MyFunkyLoopVariableThatDoesAThing if you like.

    You are free to disregard such experience as you see fit. Personally, I don't care, until I have to look at code which disregards such conventions: unless you have come up with a better way, of course.



  • Bhaskar Sarma

    Your response is no less than what I expected. Your ignorance of the existence of a standard coding practice doesn't mean it doesn't exist.

    I never said it was the only way. I never said it was my 'view'. I never said 'it must be done this way'. I advocate following a standard pattern when it is has been used successully in the past. The OP problem is a common one that goes back years (over a decade). The pattern I posted is what has been used for those years to solve the problem. Whenever such code is read by another experienced coder it will be familiar. This leads to fewer bugs: which is of greater importance than clever coding.



  • Suppressing events such as text change