Results 1 to 4 of 4

Thread: Looping Captions and Option Buttons

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    2

    Looping Captions and Option Buttons

    Hi,

    I am new to programming and am trying to develop a GIS based application. Very simple, I have code to copy text into an attribute table. I have a form which has several option boxes with captions of different attributes, I need to be able to produce a loop code to discover which option buttons value=true and for each of these to store the caption as a string into a predefined string (mytext) seperated by a comma.

    So I have 4 optionbuttons (Optionbutton1 - OptionButton4)

    This is as far as I have got...

    Dim mytext as String

    For x = 1 To 4 'x being the number after the optionbox name
    If Optionbuttons(x).value = True Then 'this line doesn't work as it wont recognise the control numbers with x as a suffix
    (Store caption into mytext with comma at the end)
    End If
    Optionbox(x).Value = False

    I would appreciate any help, as I said I am a beginner and sorry if I am wasting anyones time.

    Many Thanks

    Craig

  2. #2
    Frenzied Member HanneSThEGreaT's Avatar
    Join Date
    Nov 2003
    Location
    Vereeniging, South Africa
    Posts
    1,492

    Re: Looping Captions and Option Buttons

    You're not wasting anyone's time, stop apologising...

    I'd suggest you make use of a For Each loop here. Inside the For Each loop you can determine if the current object is an option button or not, and then get or set the text accordingly
    VB.NET MVP 2008 - Present

  3. #3
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Looping Captions and Option Buttons

    Hope this helps

    Code:
      Dim sb As New System.Text.StringBuilder
            For index As Integer = 1 To 4
                Dim rbutton() As Control
                rbutton = Me.Controls.Find("Radiobutton" & index.ToString, True)
                'Button Found
                If rbutton.Length = 1 Then
                    'Check if it is Radio Button
                    If rbutton(0).GetType.Name.ToLower = "radiobutton" Then
                        Dim rb As RadioButton = TryCast(rbutton(0), RadioButton)
                        If Not rb Is Nothing AndAlso rb.Checked = True Then
                            'Append the Text
                            sb.AppendLine(rb.Text)
                            'Make it uncheck
                            rb.Checked = False
                            rb.Dispose()
                            rb = Nothing
                        End If
                    End If
                    
                End If
            Next
            'display the string
            MessageBox.Show(sb.ToString)
    BTW : Welcome to Forums
    Last edited by danasegarane; Feb 9th, 2010 at 06:38 AM.
    Please mark you thread resolved using the Thread Tools as shown

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    2

    Re: Looping Captions and Option Buttons

    Thanks for your help so far, I think the VB edition I am using is a little old now, VB 6.5 so some of the code isn;t recognised. I've put it into 2008 and it seems to be working however there a few issues, I need to be able to select more than one radio button and for the captions to be displayed in a textbox for instance.

    So close....

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