Results 1 to 6 of 6

Thread: [RESOLVED] Problem with Option button

  1. #1

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Resolved [RESOLVED] Problem with Option button

    Hi,

    I attached my form as attachment herewith. It has one option button. In run time I load another 3 option button.

    In run time please click command1 which will create three more option buttons.

    My problem is,

    run the form;
    select the option1
    now click on command1 which will create 3 more option buttons

    but it removes the selection in option1(0). I mean the first option button is unchecked.

    how to fix this problem?
    Attached Files Attached Files
    CS

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Problem with Option button

    Put
    VB Code:
    1. Option1(0).Value = True
    as the last command in your button click event.

  3. #3
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Problem with Option button

    Maybe you want it to work with the first option selected or not, if so you can save its state before in a boolean variable and then update its state at the end, using that variable.
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim I As Integer, lFirstChecked As Boolean
    3.  
    4.     lFirstChecked = Me.Option1.Item(0).Value
    5.     For I = 1 To 3
    6.         Load Option1(I)
    7.         With Option1(I)
    8.             .Left = Option1(I - 1).Left
    9.             .Top = Option1(I - 1).Top + 357
    10.             .Value = False
    11.             .Visible = True
    12.         End With
    13.     Next I
    14.     Me.Option1.Item(0).Value = lFirstChecked
    15. End Sub
    Last edited by jcis; Oct 24th, 2006 at 02:00 PM.

  4. #4

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Problem with Option button

    hmmm. I think I haven't stated my problem here properly. The code I posted is a sample. But in my project I will create n number of controls at any time. and I do not know which control is selected.

    Any way I got an idea. so, I will be going through all controls to make sure which one is selected and then load additional control and check the selected control again.

    Thanks for your inputs
    CS

  5. #5
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Problem with Option button

    You can use an Integer and Option1_Click event. Since Option1 is a control array, you will get "Index" when one of theme is clicked. Here's what i'm talking about:
    VB Code:
    1. Option Explicit
    2.     [B]Private selected As Integer[/B]
    3.  
    4. Private Sub Command1_Click()
    5.     Dim I As Integer
    6.     For I = 1 To 3
    7.         Load Option1(I)
    8.         With Option1(I)
    9.             .Left = Option1(I - 1).Left
    10.             .Top = Option1(I - 1).Top + 357
    11.             .Value = False
    12.             .Visible = True
    13.         End With
    14.     Next I
    15.    
    16.     Option1([B]selected[/B]).Value = True
    17. End Sub
    18.  
    19. Private Sub Option1_Click([B]Index[/B] As Integer)
    20.     [B]selected = Index[/B]
    21. End Sub

  6. #6
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Problem with Option button

    The problem is the first created instance inherits the Value property of index 0 instance, which is True, so it becomes the selected option until .Value = False line in with structure is reached. At which point both option buttons have value = false. 2nd and 3rd option buttons then inherit instance index 0 value property of false.

    You don't have to iterate through the entire control array... just save the value property of instance at index 0 and reset it after loading new instances like what jcis did
    Attached Files Attached Files

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