|
-
Jul 5th, 2012, 09:47 AM
#1
Thread Starter
New Member
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
-
Jul 5th, 2012, 10:30 AM
#2
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.
-
Jul 5th, 2012, 10:49 AM
#3
Re: Using OptionButtons correctly
Thread moved from the 'VB.Net' (VB2002 and later) forum to the 'VB6 and Earlier' forum
-
Jul 5th, 2012, 11:29 AM
#4
Member
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
-
Jul 8th, 2012, 02:47 AM
#5
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
-
Jul 8th, 2012, 08:35 AM
#6
Re: Using OptionButtons correctly
 Originally Posted by fafalone
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.
-
Jul 9th, 2012, 06:02 PM
#7
Re: Using OptionButtons correctly
 Originally Posted by MarkT
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.
-
Jul 9th, 2012, 06:26 PM
#8
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.
-
Jul 10th, 2012, 08:09 PM
#9
Re: Using OptionButtons correctly
 Originally Posted by ColinE66
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.
-
Jul 10th, 2012, 10:09 PM
#10
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.
-
Jul 11th, 2012, 08:40 AM
#11
Re: Using OptionButtons correctly
 Originally Posted by fafalone
I think the impression that would have been conveyed is that 'this is a way to do it
Really? Based on this:
 Originally Posted by fafalone
Both option buttons need to have the same name.
We'll have to agree to disagree, I'm afraid....
-
Jul 11th, 2012, 11:12 AM
#12
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.
-
Jul 12th, 2012, 01:36 AM
#13
Addicted Member
Re: Using OptionButtons correctly
 Originally Posted by Mr_Bateman
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|