Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Control

Control

Scheduled Pinned Locked Moved Visual Basic
question
13 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Anonymous

    I've created my own ocx control (with 2 option buttons y & n). I inserted that control in my project. My question:how do I find out the value (y or n) from conrol? Thanks

    D Offline
    D Offline
    Dave Kreskowiak
    wrote on last edited by
    #2

    Your control is going to have to expose this property. What you are probably looking to do in your control is when a button is clicked, the code that handles that button click sets a property in your control with a value that is unique to that button, say 1 for the Y button and 0 for N button. Then your application can check the value of the property for which button was clicked. Another method would be to have your control fire an event that includes which button was clicked (as above) in its arguments. In your control, something like this: (not actual code!)

    Public Event ButtonClicked(ByVal ButtonID as Integer)

    Button handling code:
    Private Sub YButton_Clicked(...)
    RaiseEvent ButtonClicked(1)
    End Sub

    Private Sub NButton_Clicked(...)
    RaiseEvent ButtonClicked(0)
    End Sub

    RageInTheMachine9532

    A 1 Reply Last reply
    0
    • D Dave Kreskowiak

      Your control is going to have to expose this property. What you are probably looking to do in your control is when a button is clicked, the code that handles that button click sets a property in your control with a value that is unique to that button, say 1 for the Y button and 0 for N button. Then your application can check the value of the property for which button was clicked. Another method would be to have your control fire an event that includes which button was clicked (as above) in its arguments. In your control, something like this: (not actual code!)

      Public Event ButtonClicked(ByVal ButtonID as Integer)

      Button handling code:
      Private Sub YButton_Clicked(...)
      RaiseEvent ButtonClicked(1)
      End Sub

      Private Sub NButton_Clicked(...)
      RaiseEvent ButtonClicked(0)
      End Sub

      RageInTheMachine9532

      A Offline
      A Offline
      Anonymous
      wrote on last edited by
      #3

      If I understood you correct. I add the following code into my contorll porject and them created new ocx. Private Sub optYes_Click() optYes.Value = True End Sub Private Sub optNo_Click() optNo.Value = True End Sub Private Sub UserControl_Initialize() optNo.Value = False optYes.Value = False End Sub However, when I am placing that control in my main project I still can't see selected value. Please help me.

      D 1 Reply Last reply
      0
      • A Anonymous

        If I understood you correct. I add the following code into my contorll porject and them created new ocx. Private Sub optYes_Click() optYes.Value = True End Sub Private Sub optNo_Click() optNo.Value = True End Sub Private Sub UserControl_Initialize() optNo.Value = False optYes.Value = False End Sub However, when I am placing that control in my main project I still can't see selected value. Please help me.

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #4

        Wait a minute... My mistake, I thought you were using two command buttons. :doh: OK. An OCX means your using VB6... What you have to do is expose (make Public) your own custom Property. What you would normally do is define an global variable used internal to your control, like m_OptionPicked. You use the optYes and optNo click events to set the value of that variable, either a number or some predefined Enumeration, whatever you want your Property to expose. I'll use an Enum to demonstrate:

        ' Setup our Public enumeration values
        ' These are the possible values of our controls property.
        Public Enum ControlOptions
        Undefined = -1
        Yes = 1
        No = 0
        End Enum
         
        ' Internal variables (visible only inside the control!)
        ' Declaring as 'ControlOptions' will limit the value to
        ' the possible values of our Enumeration above.
        Private m_OptionPicked as ControlOptions

        Now, when an option button is clicked, you handle the Click event just as you have in your previous post, BUT you set the value of m_OptionPicked to one of the predefined values of ControlOptions, like this:

        Private Sub UserControl_Initialize()
        m_OptionPicked = ControlOptions.Undefined
        End Sub
         
        Private Sub optYes_Click()
        m_OptionPicked = ControlOptions.Yes
        End Sub
         
        Private Sub optNo_Click()
        m_OptionPicked = ControlOptions.No
        End Sub

        Almost done! Now you have to expose the internal variable as a Property of your control. You can do this using the Property statements. We're going to make a property called OptionPicked that your control exposes to its host. The code in the Property statement will just return the value of the internal variable m_OptionPicked. Once again, we'll declare that the value returned is one of the possible values of ControlOptions above.

        Public Property Get OptionPicked() as ControlOptions
        return m_OptionPicked
        End Property

        Since we have only a Property Get defined for OptionPicked, is automatically becomes a Read-Only property. That is, your application can only read the value of OptionPicked, it can't set a new value for it. If you wanted to do that, you would have to define a block of code for a Property Set statement so your control knows what to do with the new value:

        Public Property Set OptionPicked( NewValue as ControlOptions )
        m_OptionPicked = NewValue
        ' Some addition code to alter the status
        ' of the option buttons on screen should go here.
        End Property

        That's all there is to

        A 1 Reply Last reply
        0
        • D Dave Kreskowiak

          Wait a minute... My mistake, I thought you were using two command buttons. :doh: OK. An OCX means your using VB6... What you have to do is expose (make Public) your own custom Property. What you would normally do is define an global variable used internal to your control, like m_OptionPicked. You use the optYes and optNo click events to set the value of that variable, either a number or some predefined Enumeration, whatever you want your Property to expose. I'll use an Enum to demonstrate:

          ' Setup our Public enumeration values
          ' These are the possible values of our controls property.
          Public Enum ControlOptions
          Undefined = -1
          Yes = 1
          No = 0
          End Enum
           
          ' Internal variables (visible only inside the control!)
          ' Declaring as 'ControlOptions' will limit the value to
          ' the possible values of our Enumeration above.
          Private m_OptionPicked as ControlOptions

          Now, when an option button is clicked, you handle the Click event just as you have in your previous post, BUT you set the value of m_OptionPicked to one of the predefined values of ControlOptions, like this:

          Private Sub UserControl_Initialize()
          m_OptionPicked = ControlOptions.Undefined
          End Sub
           
          Private Sub optYes_Click()
          m_OptionPicked = ControlOptions.Yes
          End Sub
           
          Private Sub optNo_Click()
          m_OptionPicked = ControlOptions.No
          End Sub

          Almost done! Now you have to expose the internal variable as a Property of your control. You can do this using the Property statements. We're going to make a property called OptionPicked that your control exposes to its host. The code in the Property statement will just return the value of the internal variable m_OptionPicked. Once again, we'll declare that the value returned is one of the possible values of ControlOptions above.

          Public Property Get OptionPicked() as ControlOptions
          return m_OptionPicked
          End Property

          Since we have only a Property Get defined for OptionPicked, is automatically becomes a Read-Only property. That is, your application can only read the value of OptionPicked, it can't set a new value for it. If you wanted to do that, you would have to define a block of code for a Property Set statement so your control knows what to do with the new value:

          Public Property Set OptionPicked( NewValue as ControlOptions )
          m_OptionPicked = NewValue
          ' Some addition code to alter the status
          ' of the option buttons on screen should go here.
          End Property

          That's all there is to

          A Offline
          A Offline
          Anonymous
          wrote on last edited by
          #5

          I have some dificulty to compile tha code with the following lines: Public Property Get OptionPicked() As ControlOptions return m_OptionPicked End Property Public Property Set OptionPicked(NewValue As ControlOptions) m_OptionPicked = NewValue End Property Please help me. Thanks:((

          D M 3 Replies Last reply
          0
          • A Anonymous

            I have some dificulty to compile tha code with the following lines: Public Property Get OptionPicked() As ControlOptions return m_OptionPicked End Property Public Property Set OptionPicked(NewValue As ControlOptions) m_OptionPicked = NewValue End Property Please help me. Thanks:((

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #6

            You have to include what the error/problem is! RageInTheMachine9532

            1 Reply Last reply
            0
            • A Anonymous

              I have some dificulty to compile tha code with the following lines: Public Property Get OptionPicked() As ControlOptions return m_OptionPicked End Property Public Property Set OptionPicked(NewValue As ControlOptions) m_OptionPicked = NewValue End Property Please help me. Thanks:((

              M Offline
              M Offline
              Mike Dimmick
              wrote on last edited by
              #7

              Return isn't supported in VB6. Try

              Public Property Get OptionPicked() As ControlOptions

              OptionPicked = m_OptionPicked

              End Property

              I think you're OK with the Set part.

              1 Reply Last reply
              0
              • A Anonymous

                I have some dificulty to compile tha code with the following lines: Public Property Get OptionPicked() As ControlOptions return m_OptionPicked End Property Public Property Set OptionPicked(NewValue As ControlOptions) m_OptionPicked = NewValue End Property Please help me. Thanks:((

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #8

                My bad! That's what I get for writting VB6 code from memory! The fixed line should be:

                Public Property Get OptionPicked() As ControlOptions
                OptionPicked = m_OptionPicked
                End Property

                RageInTheMachine9532

                A 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  My bad! That's what I get for writting VB6 code from memory! The fixed line should be:

                  Public Property Get OptionPicked() As ControlOptions
                  OptionPicked = m_OptionPicked
                  End Property

                  RageInTheMachine9532

                  A Offline
                  A Offline
                  Anonymous
                  wrote on last edited by
                  #9

                  Here is my code with your modification: Option Explicit Public Enum ControlOptions Undefined = -1 Yes = 1 No = 0 End Enum Private m_OptionPicked As ControlOptions Private Sub UserControl_Initialize() m_OptionPicked = ControlOptions.Undefined End Sub Private Sub optYes_Click() m_OptionPicked = ControlOptions.Yes End Sub Private Sub optNo_Click() m_OptionPicked = ControlOptions.No End Sub Public Property Get OptionPicked() As ControlOptions OptionPicked = m_OptionPicked End Property Public Property Set OptionPicked(NewValue As ControlOptions) m_OptionPicked = NewValue End Property And here is the error: Compile error: Definitions of property procedures for the same property are inconsistent, or property procedure has an optional parameter, as ParamArray, or an invalid Set final parameter

                  D 1 Reply Last reply
                  0
                  • A Anonymous

                    Here is my code with your modification: Option Explicit Public Enum ControlOptions Undefined = -1 Yes = 1 No = 0 End Enum Private m_OptionPicked As ControlOptions Private Sub UserControl_Initialize() m_OptionPicked = ControlOptions.Undefined End Sub Private Sub optYes_Click() m_OptionPicked = ControlOptions.Yes End Sub Private Sub optNo_Click() m_OptionPicked = ControlOptions.No End Sub Public Property Get OptionPicked() As ControlOptions OptionPicked = m_OptionPicked End Property Public Property Set OptionPicked(NewValue As ControlOptions) m_OptionPicked = NewValue End Property And here is the error: Compile error: Definitions of property procedures for the same property are inconsistent, or property procedure has an optional parameter, as ParamArray, or an invalid Set final parameter

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #10

                    Once again, my bad! Change the Set to Let:

                    Public Property Let OptionPicked(NewValue As ControlOptions)
                    m_OptionPicked = NewValue
                    End Property

                    The Set version is used for setting object references... RageInTheMachine9532

                    A 1 Reply Last reply
                    0
                    • D Dave Kreskowiak

                      Once again, my bad! Change the Set to Let:

                      Public Property Let OptionPicked(NewValue As ControlOptions)
                      m_OptionPicked = NewValue
                      End Property

                      The Set version is used for setting object references... RageInTheMachine9532

                      A Offline
                      A Offline
                      Anonymous
                      wrote on last edited by
                      #11

                      It is not your bad it is my luck of knoledge that I can't figure out by myself.But I am learning :) I was able to compile the ocx project and then use it in my main project, however I still can't see the value. UserControl11.Value ????

                      D 1 Reply Last reply
                      0
                      • A Anonymous

                        It is not your bad it is my luck of knoledge that I can't figure out by myself.But I am learning :) I was able to compile the ocx project and then use it in my main project, however I still can't see the value. UserControl11.Value ????

                        D Offline
                        D Offline
                        Dave Kreskowiak
                        wrote on last edited by
                        #12

                        Thats because it's called OptionPicked, not Value. RageInTheMachine9532

                        A 1 Reply Last reply
                        0
                        • D Dave Kreskowiak

                          Thats because it's called OptionPicked, not Value. RageInTheMachine9532

                          A Offline
                          A Offline
                          Anonymous
                          wrote on last edited by
                          #13

                          I love you... :) Thanks for the help...

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups