Results 1 to 13 of 13

Thread: Using OptionButtons correctly

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    9

    Using OptionButtons correctly

    Hi all,

    I am new to VB6. I have two optionButtons that I have drawn. When OptionButton 1 is selected I want to write "Yes" to a text file called test.INI..When OptionButton 2 is selected I want to write "No" to the text file.

    What I have below is not working...can anyone help me?

    Code:
    Private Sub Option1_Click()
      Dim path As String
        path = "C:\test.INI"
        Dim ss As String
        ss = FreeFile
        If FExist(path) Then
            Open path For Output As #ss
        Else
            Open path For Output As #ss
        End If
        Print #ss, "Yes"
    
    End Sub
    
    Private Sub Option2_Click()
      Dim path As String
        path = "C:\test.INI"
        Dim ss As String
        ss = FreeFile
        If FExist(path) Then
            Open path For Output As #ss
        Else
            Open path For Output As #ss
        End If
        Print #ss, "No"
    End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Using OptionButtons correctly

    First of all, it concerns me that someone would choose to start out with VB6 nowadays, unless it's for a specific job. VB.NET was first released 10 years ago so VB6 is well out of date, and no longer officially supported by Microsoft. If you have a choice and you haven't invested too much in VB6, I strongly suggest learning VB.NET instead.

    Apart from that, it concerns me that, when presented with forums entitled "Visual Basic .NET" and "Visual Basic 6 and Earlier", you would choose the VB.NET one to post a VB6 question. I'll ask the mods to move this thread to the correct forum.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Using OptionButtons correctly

    Thread moved from the 'VB.Net' (VB2002 and later) forum to the 'VB6 and Earlier' forum

  4. #4
    Member sgarv's Avatar
    Join Date
    Jul 2012
    Posts
    34

    Re: Using OptionButtons correctly

    Change dim ss as string to dim ss as integer


    Change

    If FExist(path) Then
    Open path For Output As #ss

    to

    If FExist(path) Then
    Open path For Append As #ss

    Changes apply to both option button events. Test and report back how it behaves. If it still does not work please provide more detail. Regards, Sgarv

  5. #5
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,653

    Re: Using OptionButtons correctly

    You want it so that you can only select one at a time, correct? (And be able to deselect)

    Both option buttons need to have the same name. Change Option2 to Option1, and say yes when VB asks if you want an array.

    Then you'll do something like this--

    Code:
    Private Sub Option1_Click(index As Integer)
      Dim path As String
        path = "C:\test.INI"
        Dim ss As Integer
        ss = FreeFile
        If FExist(path) Then
            Open path For Output As #ss
        Else
            Open path For Output As #ss
        End If
    
    If Option1(0).Value = True Then
        Print #ss, "Yes"
    Else
        Print #ss, "No"
    End If
    End Sub

  6. #6
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Using OptionButtons correctly

    Quote Originally Posted by fafalone View Post
    You want it so that you can only select one at a time, correct? (And be able to deselect)

    Both option buttons need to have the same name. Change Option2 to Option1, and say yes when VB asks if you want an array.
    I agree that placing the option buttons in an array makes it a lot cleaner but it is wrong to say they need to be in an arrray to make the mutually exclusive.

    @Mr_Bateman
    Search for GetPrivateProfileString and WritePrivateProfileString in this forum to get examples of how you should write to and read from INI files.

  7. #7
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,653

    Re: Using OptionButtons correctly

    Quote Originally Posted by MarkT View Post
    I agree that placing the option buttons in an array makes it a lot cleaner but it is wrong to say they need to be in an arrray to make the mutually exclusive.
    Thanks for the cool blast of pedantry

    In your honor the next option button I implement will use API and subclassing to manage its state.

  8. #8
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,272

    Re: Using OptionButtons correctly

    In fairness, I don't think MarkT was being pedantic at all. His point was to clarify your misleading assertion that mutual-exclusivity requires a control array, which is clearly the impression that the OP (who is learning, after all) would have gotten.

  9. #9
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,653

    Re: Using OptionButtons correctly

    Quote Originally Posted by ColinE66 View Post
    In fairness, I don't think MarkT was being pedantic at all. His point was to clarify your misleading assertion that mutual-exclusivity requires a control array, which is clearly the impression that the OP (who is learning, after all) would have gotten.
    I think the impression that would have been conveyed is that 'this is a way to do it', not 'there is absolutely only one way to do this'- a very rare thing in programming; and saying the latter is "clearly" the meaning is not accurate. It's my contention that few people would conclude my statement precluded other options. Pointing out that I didn't use language that explicitly asserted that this was one of several ways to do that is a technicality.

  10. #10
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Using OptionButtons correctly

    Why do you have this If test in there?
    Code:
    If FExist(path) Then
            Open path For Output As #ss
        Else
            Open path For Output As #ss
        End If
    It does the same thing in all cases so there is no point in having an if test as coded.

    Also you are not closing the files after you open them which would cause your program to crash should this code try to execute a second time for any reason. So if you click option1 then click option2 you would get a file already open error and your program would crash.

    Always close a file after you have did your read or write operation, ideally in the same sub routine where it is opened.

    Aside from that "Not working" is a very poor description of your problem. Tell us what it is or is not doing, any error messages you receive and if so where and you will get better help.

  11. #11
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,272

    Re: Using OptionButtons correctly

    Quote Originally Posted by fafalone View Post
    I think the impression that would have been conveyed is that 'this is a way to do it
    Really? Based on this:

    Quote Originally Posted by fafalone View Post
    Both option buttons need to have the same name.
    We'll have to agree to disagree, I'm afraid....

  12. #12
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Using OptionButtons correctly

    @fafalone

    I did agree that a control array was a good way to go but the way I read your post was that it was only solution. I was just pointing out there were other ways to accomplish the desired goal.

    I think if you step back and view your post in the eyes of someone who is just learning you may see that the original poster could get the impression that a control array is the only way to achieve their goal.

    My post was not meant to belittle in any way. If you took it that way, I’m sorry. I was just letting the user know there were other avenues to pursue if they ran into trouble figuring out what had been suggested.

  13. #13
    Addicted Member ryanframes's Avatar
    Join Date
    Apr 2012
    Posts
    210

    Re: Using OptionButtons correctly

    Quote Originally Posted by Mr_Bateman View Post
    Hi all,

    I am new to VB6. I have two optionButtons that I have drawn. When OptionButton 1 is selected I want to write "Yes" to a text file called test.INI..When OptionButton 2 is selected I want to write "No" to the text file.

    What I have below is not working...can anyone help me?

    Code:
    Private Sub Option1_Click()
      Dim path As String
        path = "C:\test.INI"
        Dim ss As String
        ss = FreeFile
        If FExist(path) Then
            Open path For Output As #ss
        Else
            Open path For Output As #ss
        End If
        Print #ss, "Yes"
    
    End Sub
    
    Private Sub Option2_Click()
      Dim path As String
        path = "C:\test.INI"
        Dim ss As String
        ss = FreeFile
        If FExist(path) Then
            Open path For Output As #ss
        Else
            Open path For Output As #ss
        End If
        Print #ss, "No"
    End Sub
    can i see that FExist function..
    i need it...
    Sorry for bad english.

Tags for this Thread

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