option button control array - won't work 
Author Message
 option button control array - won't work

I'm trying to use a form only to set one of 5 options. The form only
has an option button control array on it.

How do I pass the value back to the first form that invoked this
second form ?
(I'm using a public variable [terrain] to do this. - but it doesn't
work as I want it to.]

When the form is activated (or shown) the click event for optTerrain
is also activated (without having clicked an option) - this has the
effect of placing the value of the default button in the terrain
variable. I only want this variable changed when I click.

How do I load the form with an option button array in it without this
happening?

Do I need to use a form to bring up a new set of options? What is the
most common way to enter an option?

Note: I added a command button to the form and set the focus to it
when the form was activated but this had no effect - the option
button's click event was still activated mearly on loading the form -
why is this happening?

The MS manual doesn't say much about control arrays - and I'm not a
professional so I have no one to help me. Should I ring the b*****ds
up and demand that they tell me what to do? Would they? - I have the
licence but it's nearly a year old.

Private Sub optTerrain_Click(Index As Integer)
   Select Case Index
      Case 0
         Terrain = Index
         frmWilderness.Hide
      Case 1
         Terrain = Index
         frmWilderness.Hide
      Case 2
         Terrain = Index
         frmWilderness.Hide
      Case 3
         Terrain = Index
         frmWilderness.Hide
      Case 4
         Terrain = Index
         frmWilderness.Hide
   End Select
End Sub



Tue, 30 Jan 2001 03:00:00 GMT  
 option button control array - won't work

Quote:

> I'm trying to use a form only to set one of 5 options. The form only
> has an option button control array on it.

> How do I pass the value back to the first form that invoked this
> second form ?
> (I'm using a public variable [terrain] to do this. - but it doesn't
> work as I want it to.]

I think it's best to do this:Your initial form I will call form1
The form with the option's on it I will call form2

When you have called form2 from form1 and you want the value of an option
returned to form1 you give in (in form2 before you give the statement to
go back to form1)
form1.<stringname> = <stringname>
in your case
form1.index = index

Make sure you have in the general - declarations this (on both form's!)
public <stringname> as string/integer/etc.
in your case
public index as integer

If you want an other string/integer filled in form1 just use another
string/integer name, i.e. form1.myvalue = index
on form1 in the general - declarations you put then public myvalue as
integer and on form2 public index as integer.

I hope you can use this.

Quote:

> When the form is activated (or shown) the click event for optTerrain
> is also activated (without having clicked an option) - this has the
> effect of placing the value of the default button in the terrain
> variable. I only want this variable changed when I click.

> How do I load the form with an option button array in it without this
> happening?

> Do I need to use a form to bring up a new set of options? What is the
> most common way to enter an option?

> Note: I added a command button to the form and set the focus to it
> when the form was activated but this had no effect - the option
> button's click event was still activated mearly on loading the form -
> why is this happening?

> The MS manual doesn't say much about control arrays - and I'm not a
> professional so I have no one to help me. Should I ring the b*****ds
> up and demand that they tell me what to do? Would they? - I have the
> licence but it's nearly a year old.

> Private Sub optTerrain_Click(Index As Integer)
>    Select Case Index
>       Case 0
>          Terrain = Index
>          frmWilderness.Hide
>       Case 1
>          Terrain = Index
>          frmWilderness.Hide
>       Case 2
>          Terrain = Index
>          frmWilderness.Hide
>       Case 3
>          Terrain = Index
>          frmWilderness.Hide
>       Case 4
>          Terrain = Index
>          frmWilderness.Hide
>    End Select
> End Sub

Bye,

Hans Dreve



Tue, 30 Jan 2001 03:00:00 GMT  
 option button control array - won't work
Mark,

I think that what is happening is that when the form is activated VB
"helpfully" sets the focus to the control with the smallest tab index. In
your case this is the first option button.  Setting the focus to an option
button is the same as clicking it, hence it becomes selected.

Your idea of adding a command button was almost there.  Go and set the tab
index of the command button to 0 and I think you may be OK!

In terms of getting the value back from the form, your idea of using a
global variable is a good one.  An alternative approach is to hide the form
rather than unloading (as you are doing). This means that you can
interrogate the values on the form.  Thus your code would read:

    frmWilderness.Show vbModal
    if frmWilderness!optTerrain(0).Value then
        ' Do something
    else
        ' Do something else
    end if

    Unload frmTerrain

It is often a good idea to use the tag field of the form to denote success.
So after the show command you might insert
    if frmWilderness.tag = "OK" then
        ' The user made some sort of selection so process it
        . . .
    end if
    Unload frmWilderness.

HTH

Peter

Quote:

>I'm trying to use a form only to set one of 5 options. The form only
>has an option button control array on it.

>How do I pass the value back to the first form that invoked this
>second form ?
>(I'm using a public variable [terrain] to do this. - but it doesn't
>work as I want it to.]

>When the form is activated (or shown) the click event for optTerrain
>is also activated (without having clicked an option) - this has the
>effect of placing the value of the default button in the terrain
>variable. I only want this variable changed when I click.

>How do I load the form with an option button array in it without this
>happening?

>Do I need to use a form to bring up a new set of options? What is the
>most common way to enter an option?

>Note: I added a command button to the form and set the focus to it
>when the form was activated but this had no effect - the option
>button's click event was still activated mearly on loading the form -
>why is this happening?

>The MS manual doesn't say much about control arrays - and I'm not a
>professional so I have no one to help me. Should I ring the b*****ds
>up and demand that they tell me what to do? Would they? - I have the
>licence but it's nearly a year old.

>Private Sub optTerrain_Click(Index As Integer)
>   Select Case Index
>      Case 0
>         Terrain = Index
>         frmWilderness.Hide
>      Case 1
>         Terrain = Index
>         frmWilderness.Hide
>      Case 2
>         Terrain = Index
>         frmWilderness.Hide
>      Case 3
>         Terrain = Index
>         frmWilderness.Hide
>      Case 4
>         Terrain = Index
>         frmWilderness.Hide
>   End Select
>End Sub



Tue, 30 Jan 2001 03:00:00 GMT  
 option button control array - won't work
Hi Mark!

There are too many questions about your problem:

1. Which version of VB do you use (i suppose 4 or 5 becaue you mentioned
a "public" variable)?

2. How do you show the form? Modal or modeless?

3. Do you invoke the option-button's click-event yourself by setting the
value of one of them to "true"?

4. Should the form immediatelly disappear after clicking one of the
buttons, or should it contain Ok- and Cancel-Buttons?

5. Where is the "terrain"-variable defined (public could also be in a
BAS-module)?

=====

Anyway, i would do it this way:

I suppose your form is a modal form because you hide it after the
click-event. Whenever you show a modal dialog, you should create a
public method in the form:

e.g. "Public Function Dialog (Parameters ...) As Boolean"

The function's return value shows whether the dialog was cancelled. (A
user should always be able to cancel a dialog). "True" should tell you
that the dialog was successful. In this case your only parameter tells
the calling procedure which option was selected in your options-form.
And just this value can be supplied by your options-form in the normally
contained OK-button's click-event.

I guess you don't want to be able to cancel the form, i.e. as soon as
you click one of the option-buttons, the form should disappear. In this
case you should do the following steps in the Dialog-Function:

1. Set a form-global boolean-variable ("mIgnoreClick") to true.

2. Load Me

(3. if you want to preselect an option-button (.value): do it here)

4. Set mIgnoreClick to false.

In the option-click-event you should exit the procedure when
mIgnoreClick is true.

I can't explain the click-event when using the normal
"OptionButton"-control-class - if you don't raise it yourself by code.

=====

And two more things:

First, you should move the "frmWilderness.Hide"-Code past the "End
Select"-Statement. Second, please use only "Hide" or "Me.Hide" because
you then will never mix up different instances of one form, because
"frmWilderness" is a form-class AND an "automatic" global variable. Even
if that's probably no problem with your form.

AZ, Germany

=====

Mark Pawelek schrieb:

Quote:
> I'm trying to use a form only to set one of 5 options. The form only
> has an option button control array on it.

> How do I pass the value back to the first form that invoked this
> second form ?
> (I'm using a public variable [terrain] to do this. - but it doesn't
> work as I want it to.]

> When the form is activated (or shown) the click event for optTerrain
> is also activated (without having clicked an option) - this has the
> effect of placing the value of the default button in the terrain
> variable. I only want this variable changed when I click.

> How do I load the form with an option button array in it without this
> happening?

> Do I need to use a form to bring up a new set of options? What is the
> most common way to enter an option?

> Note: I added a command button to the form and set the focus to it
> when the form was activated but this had no effect - the option
> button's click event was still activated mearly on loading the form -
> why is this happening?

> The MS manual doesn't say much about control arrays - and I'm not a
> professional so I have no one to help me. Should I ring the b*****ds
> up and demand that they tell me what to do? Would they? - I have the
> licence but it's nearly a year old.

> Private Sub optTerrain_Click(Index As Integer)
>    Select Case Index
>       Case 0
>          Terrain = Index
>          frmWilderness.Hide
>       Case 1
>          Terrain = Index
>          frmWilderness.Hide
>       Case 2
>          Terrain = Index
>          frmWilderness.Hide
>       Case 3
>          Terrain = Index
>          frmWilderness.Hide
>       Case 4
>          Terrain = Index
>          frmWilderness.Hide
>    End Select
> End Sub



Tue, 30 Jan 2001 03:00:00 GMT  
 option button control array - won't work
On Fri, 14 Aug 1998 16:32:01 +0200, Armin Zingler

Quote:

>Hi Mark!

>There are too many questions about your problem:

>1. Which version of VB do you use (i suppose 4 or 5 becaue you mentioned
>a "public" variable)?

It was initially written in 4 - then I moved to 5.0 - with which I'm
staying. When I saved the project it became VB 5.0.

Quote:
>2. How do you show the form? Modal or modeless?

modeless

[I haven't tried making it modal - it wouldn't have it when I first
tried but I figured out why not now so I can change it.]

Quote:
>3. Do you invoke the option-button's click-event yourself by setting the
>value of one of them to "true"?

No. I even went to the trouble of disabling the control array !
setting the value of the global variable to 6 (meaningless] and
re-enabling the control array in the Command1 click !

Here's my revised code - for you to laugh at. This still doesn't work.
The problem appears to be with the calling code (which isn't shown
below).

Declarations in frmEncounter:
Public Ter As Integer

Code in frmWilderness (the second called form):

Private Sub Command1_Click()
   For i = 0 To 4
      optTerrain(i).Enabled = True
   Next i
End Sub

Private Sub Command2_Click()
   frmWilderness.Hide
End Sub

Private Sub Form_Activate()
   For i = 0 To 4
      optTerrain(i).Enabled = False
   Next i
   Terrain = 6
   Command1.SetFocus
End Sub

Private Sub optTerrain_Click(index As Integer)
   Select Case index
      Case 0
         frmEncounter.Ter = index
      Case 1
         frmEncounter.Ter = index
      Case 2
         frmEncounter.Ter = index
      Case 3
         frmEncounter.Ter = index
      Case 4
         frmEncounter.Ter = index
   End Select
End Sub

That still didn't work !

Of course I can't see why I need the Case statement in the option
click but I'll leave it there.

Quote:
>4. Should the form immediatelly disappear after clicking one of the
>buttons, or should it contain Ok- and Cancel-Buttons?

I'd prefer it to disappear - but I'll tolerate OK and cancel if I
must.

Quote:
>5. Where is the "terrain"-variable defined (public could also be in a
>BAS-module)?

Yes I defined it in a BAS module.

Sorry about all this. This is the first multi-form program I've
written in VB and the first with more than 200 lines. [It now has

Quote:
>1000]

Thanks for help from everyone who replied - I'm sure I'll sort it out
with your suggestions.

I'll try everything you tell me and get back if I do or don't get it
to work to put an end to this thread I started.



Tue, 30 Jan 2001 03:00:00 GMT  
 option button control array - won't work
Your automatic click problem may be due to not having
any option selected when the form loads.  In any grouping
of Option buttons, one must show as selected.  If none of
your option buttons are marked (selected) when the form
loads, VB will mark one for you, thus generating the click
event.

I would have gone about it a little differently:

Here is the code for the entire form:

(Gen. Declarations)
Dim NewTerrain%

Private Sub Form_Load()
       If Terrain <= 4 and Terrain >= 0 then optTerrain(Terrain) = True
End Sub

Private Sub optTerrain_Click(Index As Integer)
        NewTerrain = Index
End Sub

Private Sub OK_Click()
        Terrain = NewTerrain
        Unload Me
End Sub

Private Sub Cancel_Click()
        Unload Me
End Sub

Now the current selection is selected, and will be
changed only if the user changes the option and
presses OK.  If the user presses Cancel, no change
is made....

LFS

Quote:

> I'm trying to use a form only to set one of 5 options. The form only
> has an option button control array on it.

> How do I pass the value back to the first form that invoked this
> second form ?
> (I'm using a public variable [terrain] to do this. - but it doesn't
> work as I want it to.]



Tue, 30 Jan 2001 03:00:00 GMT  
 option button control array - won't work
Mark,

Actually, you will find that CheckBox control array is more suitable in this
application.

Any way, if you still insist on OptionBox, this is the solution:

Add a TextBox (say Text1) in you form and set properties TabIndex=0 and
Locked=True,
set the value of all OptionBox to False.

Put following code in your form module:

=====================================
Private iReturnValue As Integer

Private Sub Form_Load()
    Text1.Move -500, -500, 0, 0
    iReturnValue = -1
End Sub

Public Function OptionDialog() As Integer
    Me.Show vbModal
    OptionDialog = iReturnValue
    Unload Me
End Function

Private Sub optTerrain_Click(Index As Integer)
    iReturnValue = Index
    Me.Hide
End Sub
========================================

In the form you call up this option dialog do:

Terrain = frmWilderness.OptionDialog

=========================================

The reason way use TextBox instead of Command Button is TextBox can filter
the arrow keys, but unfortunately, if user press the Tab, he will get the
first option.

Hope this helps

Ken

Quote:

>I'm trying to use a form only to set one of 5 options. The form only
>has an option button control array on it.



Wed, 31 Jan 2001 03:00:00 GMT  
 option button control array - won't work
Here are a number of  Option - Options to try:

'GET THE TRUE MEMBER OF AN OPTION ARRAY
'Setting an element in an array of option buttons to True is easy. 'Click on
the option button, or use this code:

OptionArray(ThisOne) = True
'ThisOne is the Index of the member you want to be selected. Getting 'the
True member of an option array is not so simple. Because you need 'to
perform this kind of task in almost any reasonably complex program, 'adding
this function to your code library or generic module simplifies 'the task.
You don't need to know the array size in advance:

Function OptionTrueIs(OptionArrayName As _
Variant) As Integer

' Returns the index of the True
' member of an option array

 Dim Ctl as Control
 For Each Ctl in OptionArrayName
 If Ctl.Value = True Then
 OptionTruels = Ctl.Index
 Exit For
 End If
 Next
End Function
You can use the routine to set a Public variable from a Properties sheet
using this format:

SomePublicVariable = OptionTrueIs(SomeOptionArray())
Or use this code to save a program variable between runs:

SaveSetting("MyApp", "OptionSettings", _
 "OptionKey", OptionTrueIs(SomeOptionArray())
Load the routine using this code:

SomeOptionArray(GetSetting("MyApp", "", _
 "OptionSettings", "OptionKey",0))=True
Or you can load the routine using this code:

SomePublicVariable = GetSetting("MyApp", "", _
 "OptionSettings", "OptionKey",0)
Use this code to control a Select Case structure:

Select Case OptionTrueIs(SomeOptionArray())
Case 0:
' Insert code to execute if element 0
' of SomeOptionArray() is selected.
End Select
Use this code to test a condition:

If OptionTrueIs(SomeOptionArray()) = SomeValue The
' This code will execute if element
' SomeValue of SomeOptionArray() is
' selected.
End If



Thu, 01 Feb 2001 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Working with an Option Button Array and Checkbox Button Array

2. Option Button Control Array Not Working

3. Option Buttons: Won't Drag and Drop

4. Working with Option Button/Checkbox Arrays

5. My undo and delete button won't work

6. Command Button won't work while in loop

7. Option Button control Array problem

8. Using Frames containing option buttons as control arrays

9. Run-Time Control Array of Option Buttons w/in Frames

10. click event won't work in cancel button

11. Control arrays with option buttons

12. Controls from the Control Toolbox won't work

 

 
Powered by phpBB® Forum Software