Results 1 to 9 of 9

Thread: What good is a command button's Value property?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    566

    What good is a command button's Value property?

    I had never noticed the existence of a Value property in command buttons.
    I just happened to notice that today.

    I googled it and came up with this:
    The only relevant CommandButton's run-time property is Value, which sets or returns the state of the control (True if pressed, False otherwise). Value is also the default property for this type of control. In most cases, you don't need to query this property because if you're inside a button's Click event you can be sure that the button is being activated. The Value property is useful only for programmatically clicking a button:

    This fires the button's Click event.
    Command1.Value = True
    on this webpage: https://www.freetutes.com/learn-vb6/...-controls.html

    However, this is very strange.
    Setting a commandbutton's Value property to True, appears to me to be the exact same thing as directly calling that button's Click event.
    In other words, the following two pieces of code do the exact same thing:
    This:
    Code:
    Command1.Value = True
    and this:
    Code:
    call Command1_Click
    So, why would anyone want to set Command1.Value to True instead of calling its Click event function?

    And Also, what does setting the value property to False do?
    I tested this:
    Code:
    Command1.Value = False
    And it looks like it does nothing.
    So, why would anybody want to do such thing?

    And why would anybody want to inquire on (read) the value of Command1.Value?

    Does anybody know what good really is a command button's Value property?
    Thanks.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,515

    Re: What good is a command button's Value property?

    Click is an event, Value is a method/property. Standard code flow does not call events, it raises events and calls methods.

    It has one advantage. You can call the click from outside the form, i.e., Form1.Command1.Value = True. Otherwise, you'd have to make the Click event public/friend. You can use CallByName with the Value property/method.

    As for why the method is a True/False property? It's almost like VB said, hmmm we have a Value property standard for all our controls, what should we make the default value of this command button? And someone said, let's raise a raise a click event with it. I'm not sure why raising the click event is in a boolean method.

    edited: Does "Command1.Value = True" return true while the click event is occurring? Might want to test that. If it does, then it is a way to know that whatever code is inside the event is running; assuming there's a DoEvents in that event that allows you to test that property outside of the event.
    Last edited by LaVolpe; Sep 17th, 2020 at 01:59 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    2,309

    Re: What good is a command button's Value property?

    Quote Originally Posted by IliaPreston View Post
    So, why would anybody want to do such thing?
    You might have other event sinks besides Command_Click subroutine, for instance in a class you can have Private WithEvents m_oCtl As CommandButton so that it can listen for Click event on any button. Setting Value property will notify instances of this class too.

    Value property raising Click event is pretty random decision. I personally use methods named FireXxxEvent which are one-liners with something like RaiseEvent Xxx(...)

    Another caveat with user-control events is to *never* raise these from any code calling methods (outside FireXxxEvent ones) or getting/setting properties but only to notify for end-user interaction with the control. This has saved me numerous grievances typical for built-in controls of using flags (like m_bInSet = True : txtName.Text = sName : m_bInSet = False) to prevent Change event impl being executed when setting Text property from code.

    cheers,
    </wqw>

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    6,507

    Re: What good is a command button's Value property?

    Here's a way I often use it. In several places, I might display a list of items, with each of these items representing several fields that are editable. So, there winds up being two ways to actually edit the items from the list: 1) select the item from the list, and then click some "Edit" button, or 2) double-click the item in the list. The "Edit" button approach is always the one I get up and running first. And then, I want to implement the double-click approach. To do that, I simply insert the following:

    Code:
    
    Private Sub lstResearchStudyList_DblClick()
        cmdEdit.Value = True
    End Sub
    
    
    I've got basically that exact code (just different control names) all over my primary project.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    21,547

    Re: What good is a command button's Value property?

    Command1_Click is a method procedure that VB's event binding will bind as an event handler.

    There is nothing wrong with invoking the method yourself from other code.

    Command1.Value = True also works, though it is probably a holdover from earlier versions of VB and jumps through more hoops than just invoking the method. Sometimes I'll use it, most times I won't.

  6. #6
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    6,507

    Re: What good is a command button's Value property?

    Quote Originally Posted by dilettante View Post
    There is nothing wrong with invoking the method yourself from other code.
    Personally, I'll admit that I sometimes do that. However, if I've got a large body of code in an event procedure that's used in a few different places, I'd much prefer to pull that out and make a dedicated procedure (non-event) that does that work. And then, I'll call that non-event procedure from wherever I need it.

    I know it probably makes little (if any) difference in terms of speed, but it just seems better organized to me if done that way.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  7. #7
    Hyperactive Member
    Join Date
    Jun 2015
    Posts
    371

    Re: What good is a command button's Value property?

    20 years never knew this lol, always called the event handler directly , making it public if I had to call from another module. other obj refs sinking events is a good argument against this (although fairly rare for me)

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    21,547

    Re: What good is a command button's Value property?

    Quote Originally Posted by dz32 View Post
    other obj refs sinking events is a good argument against this
    This is an excellent point, or would be if we weren't talking about basic UI management local to a Form or UserControl. Doing something like that with a control event is pretty outré and ought to be documented inline to warn future maintainers of the program.

    If anything it make more sense to expose a Public method to perform the action rather than add external subscribers to local controls' events. I think the argument might have its merits completely reversed from those above if we want to talk about best practices.

  9. #9
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    2,309

    Re: What good is a command button's Value property?

    Quote Originally Posted by dilettante View Post
    Doing something like that with a control event is pretty outré and ought to be documented inline to warn future maintainers of the program..
    We do this all the time. In fact for all intrinsic and 3-rd party controls there are "wrapper" classes that implement common features and these definately sink events outside Form's code.

    For instance Elroy's snippet where DblClick event forwards to Click event would be implemented in the CommandButton wrapper class once and for all, not to have in each and every form the same one-liner code multiple times.

    cheers,
    </wqw>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width