Home All Groups Group Topic Archive Search About

multiple selection from checkboxes into text box

Author
5 Apr 2006 9:18 PM
Demonicpagan
I have this form where I am trying to place items from checked boxes
into a text box.  An image of what this form looks like is at
http://www.stelth2000inc.com/images/screen.png

Here is some code I have done for the first 2 check boxes, name and
world, they do what I want them to do, but for the rest of the check
boxes, the codes would be a bit minotonous.  Is there a better way of
doing this, maybe checkedboxlist?

    Private Sub sigBtn_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles sigBtn.Click
        Dim bbname As String = "[color=blue][size=10]Name: " &
charNameTxt.Text & "[/size][/color] " & vbCrLf
        Dim bbworld As String = "[color=blue][size=10]World: " &
worldCombo.SelectedItem & "[/size][/color] " & vbCrLf

       ' Name check box checked
        If chNameChk.Checked = True Then
            signatureTxt.Text = ""
            signatureTxt.Text = signatureTxt.Text & bbname
        End If

        ' World check box checked
        If worldChk.Checked = True Then
            signatureTxt.Text = ""
            signatureTxt.Text = signatureTxt.Text & bbworld
        End If

        ' Name & world check boxes checked
        If chNameChk.Checked = True And worldChk.Checked = True Then
            signatureTxt.Text = ""
            signatureTxt.Text = signatureTxt.Text & bbname & bbworld
        End If

    End Sub

Any help with this would be awesome

Author
5 Apr 2006 9:26 PM
Chris
Demonicpagan wrote:
Show quoteHide quote
> I have this form where I am trying to place items from checked boxes
> into a text box.  An image of what this form looks like is at
> http://www.stelth2000inc.com/images/screen.png
>
> Here is some code I have done for the first 2 check boxes, name and
> world, they do what I want them to do, but for the rest of the check
> boxes, the codes would be a bit minotonous.  Is there a better way of
> doing this, maybe checkedboxlist?
>
>     Private Sub sigBtn_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles sigBtn.Click
>         Dim bbname As String = "[color=blue][size=10]Name: " &
> charNameTxt.Text & "[/size][/color] " & vbCrLf
>         Dim bbworld As String = "[color=blue][size=10]World: " &
> worldCombo.SelectedItem & "[/size][/color] " & vbCrLf
>
>        ' Name check box checked
>         If chNameChk.Checked = True Then
>             signatureTxt.Text = ""
>             signatureTxt.Text = signatureTxt.Text & bbname
>         End If
>
>         ' World check box checked
>         If worldChk.Checked = True Then
>             signatureTxt.Text = ""
>             signatureTxt.Text = signatureTxt.Text & bbworld
>         End If
>
>         ' Name & world check boxes checked
>         If chNameChk.Checked = True And worldChk.Checked = True Then
>             signatureTxt.Text = ""
>             signatureTxt.Text = signatureTxt.Text & bbname & bbworld
>         End If
>
>     End Sub
>
> Any help with this would be awesome
>

For Each C as Control in Me.Controls
    if typeof c is checkbox then
        if directcast(c, checkbox).checked then
            signatureTxt.Text += C.Name
        end if
    end if
Next

That might not be the exact code, but it's close.

good luck
Author
5 Apr 2006 10:24 PM
Ken Halter
Show quote Hide quote
"Demonicpagan" <Demonicpa***@gmail.com> wrote in message
news:1144271905.651129.177130@t31g2000cwb.googlegroups.com...
>
>       ' Name check box checked
>        If chNameChk.Checked = True Then
>            signatureTxt.Text = ""
>            signatureTxt.Text = signatureTxt.Text & bbname
>        End If
>
>        ' World check box checked
>        If worldChk.Checked = True Then
>            signatureTxt.Text = ""
>            signatureTxt.Text = signatureTxt.Text & bbworld
>        End If
>
>        ' Name & world check boxes checked
>        If chNameChk.Checked = True And worldChk.Checked = True Then
>            signatureTxt.Text = ""
>            signatureTxt.Text = signatureTxt.Text & bbname & bbworld
>        End If
>
>    End Sub

I'm sure someone'll pop in with some real help... in the mean time, I'm
wondering why you're clearing then appending text to a textbox.

You should be able to replace this.....
>        If worldChk.Checked = True Then
>            signatureTxt.Text = ""
>            signatureTxt.Text = signatureTxt.Text & bbworld
>        End If

....with....
>        If worldChk.Checked = True Then
>            signatureTxt.Text = bbworld
>        End If

Depending on the details, I might code it like this in VB5/6
'========
Option Explicit

Private mobjCheckBoxes As Collection

Private Sub Form_Load()
   Set mobjCheckBoxes = New Collection

   Check1.Tag = "Name"
   Check2.Tag = "Address"

   'I like to build my own collection instead of looping through
   'the entire Controls collection each time
   mobjCheckBoxes.Add Check1
   mobjCheckBoxes.Add Check2

End Sub

Private Sub Command1_Click()
   Dim c As CheckBox

   With Text1
      .Text = "" 'clear all text

      'Loop through the collection, looking for 'checked' boxes
      'appending the Tag property to the textbox
      'I'm sure dotNet has something similar
      For Each c In mobjCheckBoxes
         If c.Value = vbChecked Then
            .SelText = " " & c.Tag
            'Reset Cursor = end of all text
            .SelStart = Len(.Text)
         End If
      Next

   End With

End Sub
'========

--
Ken Halter - MS-MVP-VB (visiting from VB6 world) - http://www.vbsight.com
Please keep all discussions in the groups..
Author
6 Apr 2006 4:22 AM
Demonicpagan
The way that i am wanting this to work is the user selects items that
they would want in a forum signature.  Each check box is representative
of items in other tabs in this program (i.e. Name is the Character name
which is a text box in the General Tab, World is a combo box in that
same tab).  The user can select as many as all the check boxes or as
little as one check box.  I was clearing the box and appending text to
the box in case user makes selections, clicks create signature, changes
their mind and selects other items, and clicks create signature again.
I didn't want the new selections following the old selections, but
rather overwrite what was previously there.

Ultimately output would look something like this if user was to select
the Character Name & World check boxes:

color=blue][size=10]Name: character name[/size][/color]
[color=blue][size=10]World: character world[/size][/color]

Just Name:
[color=blue][size=10]Name: character name[/size][/color]

Just World:
[color=blue][size=10]Name: character name[/size][/color]

World & Linkshell:
[color=blue][size=10]World: character world[/size][/color]
[color=blue][size=10]Linkshell: linkshell[/size][/color]

so on and so forth
Author
6 Apr 2006 4:26 AM
Demonicpagan
Just for knowledge purposes, I am using Visual Basic 2005 Express
Edition to code this
Author
6 Apr 2006 10:14 AM
Demonicpagan
I probably should also ask this while I'm at it because I know it will
be something that I will be asking.

The checkbox I have as Sex will be outputting either

[color=blue][size=10]Sex: Male[/size][/color]

or

[color=blue][size=10]Sex: Female[/size][/color]

based on what radio button was selected in the General Tab.  How will I
need to go about handling this with the rest of what I'm trying to do?
Author
9 Apr 2006 6:45 AM
Demonicpagan
Is there anyone that can help me with this?
Author
10 Apr 2006 8:25 AM
Demonicpagan
Still looking for some help with this