Home All Groups Group Topic Archive Search About

Re: Store multi-choice groupbox selection in a field

Author
30 Mar 2006 6:22 PM
Cerebrus
Hi,

>> Question #1)
>> How do I get it to return "2, 4, 8" for [Button1] and "4, 8" for
>> [Button2] rather than the text values?

Answer #1)
I don't think you can do this, or if it is possible, it would require
unnecessary code. After all, the whole point of using such an
enumeration is to be able to test values using common names. Remember
that the members Beautiful, Lovely and Voluptuous are just another way
of referring to values 2,4 and 8, since the former is more easily
remembered.

>> Question #2)
>> How do I parse the individual values from the total. IOW how do I see
>> if 8 is contained in 14.I have been trying to find answers to this in
>> help for over an hour. I need to check each item individually.

Answer #2)
I think you should instead try to see if the value "Voluptuous" is
contained within the myGirl variable.

This can be done by :
-------------------------------
If ((myGirl And TypeOfGirlFriend.Beautiful) =
TypeOfGirlFriend.Beautiful) Then
  Console.WriteLine("My girl is Beautiful")
End If

So, instead of checking :
-----------------------------------
is 1 set
is 2 set
is 4 set
is 8 set

Check if :
--------------
Is None set ?
Is Beautiful set ?
Is Lovely set ?
Is Voluptuous set ?

Another way, that I already told you is simply to use the ToString()
method and get all the set members.
---------------------------------------------
Dim arrMembers() As String = myGirl.ToString().Split(New Char() {","c})
Dim eachMember As String
For Each eachMember In arrMembers
  Console.WriteLine(eachMember)
Next
---------------------------------------------

After that you can proceed as normal...

Hope this helps,

Regards,

Cerebrus.

Author
3 Apr 2006 12:11 PM
dbuchanan
Cerebrus,

Look what I must do to populate the checkboxes in my form;
\\
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click

        Dim myGirl As TypeOfGirlFriend = 14
        Dim arrMembers() As String = myGirl.ToString().Split(New Char()
{","c})
        Dim eachMember As String

        For Each eachMember In arrMembers
            If eachMember = "Beautiful" Then CheckBox1.Checked = True
            If eachMember = " Lovely" Then CheckBox2.Checked = True
            If eachMember = " Voluptuous" Then CheckBox3.Checked = True
        Next

    End Sub
//

Notice that when the value of myGirlfriend = 14 that I must put a
leading space before " Lovely" and " Voluptuous". That is because of
the placement of the comma in the string "Beautiful, Lovely,
Voluptuous". The code above will only for certain myGirl values and
give incorrect values for others.

Is there another way to determine if one value is contained within the
bitfield like with a pattern or mask of some sort?

thank you,
dbuchanan
Author
3 Apr 2006 1:02 PM
Cerebrus
Hi,

I think this post will solve all the problems finally ! ;-)

1. To test for a value by Common names (The method I prefer) :

Use the Trim() method... as in :
------------------------------------------------------
For Each eachMember In arrMembers
  Dim TrimmedMember as String = eachMember.Trim()    ' To trim the
spaces, if any.
  If eachMember = "Beautiful" Then CheckBox1.Checked = True
  If eachMember = "Lovely" Then CheckBox2.Checked = True
  If eachMember = "Voluptuous" Then CheckBox3.Checked = True
Next
------------------------------------------------------

In any case, the second method should have worked. I believe that is
the recommended method. The second method was :
------------------------------------------------------
If ((myGirl And TypeOfGirlFriend.Beautiful) =
TypeOfGirlFriend.Beautiful) Then
  Console.WriteLine("My girl is Beautiful")
End If
------------------------------------------------------

2. To test for a value by values :

Refer my statement above in which I said that it might be difficult to
do it this way. I was working on something else at that time, and got
it wrong. Even after having explained this to you, I forgot that in
actuality, it's the values that are added or subtracted. So do this :
----------------------------------------------
Dim arrValues() As Integer =
[Enum].GetValues(GetType(TypeOfGirlFriend))
Dim eachVal As Integer
For Each eachVal In arrValues
  If (eachVal And TypeOfGirlFriend.Beautiful = eachVal) Then
CheckBox1.Checked = True
  If (eachVal And TypeOfGirlFriend.Lovely = eachVal) Then
CheckBox2.Checked = True
  If (eachVal And TypeOfGirlFriend.Voluptuous = eachVal) Then
CheckBox3.Checked = True
Next
----------------------------------------------
Regards,

Cerebrus.
Author
3 Apr 2006 2:22 PM
dbuchanan
Cerebrus,

I'm happy to get it down to this small amount of code.
\\
    Dim myGirl As TypeOfGirlFriend = CType(Me.TextBox1.Text,
TypeOfGirlFriend)
        Me.chkBeautiful.Checked = ((myGirl And
TypeOfGirlFriend.Beautiful) <> 0)
        Me.chkLovely.Checked = ((myGirl And TypeOfGirlFriend.Lovely) <>
0)
        Me.chkVoluptuous.Checked = ((myGirl And
TypeOfGirlFriend.Voluptuous) <> 0)
//

I could not figure out how you were using the array - where the value
was coming from? I would like to do some exploring with that.

Thank you,
dbuchanan
Author
3 Apr 2006 2:59 PM
Cerebrus
Hi again,

Your code is now pretty and concise too ! And I tested it with a sample
form with 3 checkboxes and a textbox to enter the value. It seems to
work in all cases.

So... what problem are you facing now ?

Maybe we should talk in real time ? Do you use any instant messenger ?
I have MSN and Yahoo IM.

Regards,

Cerebrus.
Author
3 Apr 2006 10:11 PM
dbuchanan
Cerebrus,

Yes I am happy with my concise code, however seeing that you were using
an array in a way I didn't understand got me interested in an
alternative approach that might have other advantages to offer.

dbuchanan