More Event Handling...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Whitworth

    More Event Handling...

    I'm tying myself up in knots again here.

    I think my query is a little different from the previous Event handling one,
    so have started a new thread.

    In VB2008, I want a reusable control, which can effectively show (and set)
    bit settings with true/false descriptions for each byte.

    Eventually, I want to group each byte into a panel, so I can treat them as a
    whole (and then probably put several panels into a tab control, so that a
    3-byte config parameter can be worked on within one form), but for the
    moment, I have just added 8 "BitSwitch" controls into a form, the class &
    code being as follows:

    Public Class BitSwitch

    Private _Status As Boolean

    Private _TrueDesc As String

    Private _FalseDesc As String

    Private _BitNo As Integer

    Public Sub New(ByVal Status As Boolean, ByVal BitNo As Integer, Optional
    ByVal TrueDesc As String = "True", Optional ByVal FalseDesc As String =
    "False")

    Me.InitializeCo mponent()

    Me.RadioPos.Tex t = TrueDesc

    Me.RadioNeg.Tex t = FalseDesc

    Me.BitLabel.Tex t = "Bit " & BitNo.ToString & ":"

    If Status = True Then

    RadioPos.Checke d = Enabled

    Else

    RadioNeg.Checke d = Enabled

    End If

    End Sub

    Public Property BitNo() As Integer

    Get

    Return _BitNo

    End Get

    Set(ByVal value As Integer)

    _BitNo = value

    End Set

    End Property

    Public Property Status() As Boolean

    Get

    Return _Status

    End Get

    Set(ByVal value As Boolean)

    _Status = value

    End Set

    End Property

    Public Property TrueDesc() As String

    Get

    Return _TrueDesc

    End Get

    Set(ByVal value As String)

    _TrueDesc = value

    End Set

    End Property

    Public Property FalseDesc() As String

    Get

    Return _FalseDesc

    End Get

    Set(ByVal value As String)

    _FalseDesc = value

    End Set

    End Property

    Private Sub RadioPos_Checke dChanged(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles RadioPos.Checke dChanged

    If RadioPos.Checke d = True Then

    _Status = True

    Else

    _Status = False

    End If

    ' I'm pretty sure I need to fire off my custom event here!!

    End Sub

    End Class



    Public Class TestForm

    Private Sub TestForm_Load(B yVal sender As Object, ByVal e As
    System.EventArg s) Handles Me.Load

    Dim Bit(7) As BitSwitch

    Dim CtrlLp As Integer

    For CtrlLp = 7 To 0 Step -1

    Bit(CtrlLp) = New BitSwitch(True, CtrlLp, "True " & CtrlLp.ToString , "False
    " & CtrlLp.ToString )

    Bit(CtrlLp).Loc ation = New Point(0, (7 - CtrlLp) * 45)

    Me.Controls.Add (Bit(CtrlLp))

    Next

    End Sub

    End Class



    So TestForm populates with 8 BitSwitches, and they all show their true/false
    descriptions properly. What I want to do is have a custom event that is
    fired whenever the state of any of the radio switches is changed in one of
    the 8 BitSwitches. Basically, I want an event in TestForm that can
    recalculate the byte value based on the settings of the 8 BitSwitches. I
    want this to be reusable, as I may want to use the class for several sets of
    bit settings at one time.

    I looked in the online examples, and started playing with Delegates etc, but
    got in a right royal mess! :-) I may well be overengineering this, but I'm
    sure this is not simply something that I can do with the GUI.

    Thanks

    John

  • Armin Zingler

    #2
    Re: More Event Handling...

    John Whitworth wrote:
    What I want to do is have a custom
    event that is fired whenever the state of any of the radio switches
    is changed in one of the 8 BitSwitches. Basically, I want an event in
    TestForm that can recalculate the byte value based on the settings of
    the 8 BitSwitches.
    1. Class BitSwitch:

    Public Event StatusChanged( _
    ByVal sender As Object, ByVal NewStatus As Boolean _
    )


    In Sub RadioPos_Checke dChanged:

    RaiseEvent StatusChanged(M e, _Status)



    2. Form:
    Before "Next":
    AddHandler Bit(CtrlLp).Sta tusChanged, AddressOf OnStatusChanged

    And a new event handler: (an example)

    Private Sub OnStatusChanged ( _
    ByVal sender As Object, ByVal NewStatus As Boolean)

    MsgBox(DirectCa st(sender, BitSwitch).BitN o & ": " & NewStatus)
    End Sub

    I want this to be reusable, as I may want to use
    the class for several sets of bit settings at one time.
    You mean, you need these 8 controls multiple times? Then put them in a
    Usercontrol, not directly on the Form. You can add a "Value" property to the
    new Usercontrol that returns the calculated value from the 8 bits.


    BTW, you don't store all values in Sub New in the usercontrol:

    _TrueDesc = TrueDesc
    _FalseDesc = FalseDesc
    _BitNo = BitNo


    And in Sub RadioPos_Checke dChanged, you probably want to update the
    displayed text:

    If RadioPos.Checke d = True Then
    _Status = True
    BitLabel.Text = _TrueDesc
    Else
    _Status = False
    BitLabel.Text = _FalseDesc
    End If



    Armin

    Comment

    • John Whitworth

      #3
      Re: More Event Handling...


      "Armin Zingler" <az.nospam@free net.dewrote in message
      news:eXNMLL%23S JHA.6060@TK2MSF TNGP06.phx.gbl. ..
      If RadioPos.Checke d = True Then
      _Status = True
      BitLabel.Text = _TrueDesc
      Else
      _Status = False
      BitLabel.Text = _FalseDesc
      End If
      Brilliant! Thanks Armin. That all works perfectly. I should be able to move
      on a huge amount now. As you suggest, I do need to move this all into
      another control, but I just wanted to get something working first in a form.
      I don't need to update BitLabel.Text though, as that is just a numeric
      label. The way each BitSwitch looks is:

      Bit ?: Here is the true description O O Here is the false
      description.

      Thanks again - it's much appreciated.

      John

      Comment

      Working...