Combo boxes don't default to current value 
Author Message
 Combo boxes don't default to current value

I have a form with several combo boxes. Their values are set via .additem
and they work OK.

BUT, when I close and reopen the form they are all blank. How can I get them
to show their current values?

I have tried me.comboName.text=strValue but to no avail....

Thanks,

Jon



Wed, 02 Jul 2003 21:01:01 GMT  
 Combo boxes don't default to current value
Combo's don't store their select item state when the form is unloaded. You
will need to keep track of each combo's ListIndex and reset that in the Load
event. If the form is just hidden instead, all values set in the form
persist.

If you are looking for saving the selected index between running the app,
you'll need to save the listindexes into a file or registry and load these
in the Load event, setting the appropriate combo to the correct value. If
you opt for the file method, you can use either ini files (see
http://www.mvps.org/vbnet/code/fileapi/pprofilebasic.htm for the basics of
this) or a Random Access file and user-defined type (ie...

Private Type ComboIndexSelections
    C1 As Integer
    C2 As Integer
    C3 As Integer
   '...etc
End Type

Private Sub Form_Load()

   With Combo1
      .AddItem "a"
      .AddItem "b"
      .AddItem "c"
   End With

   With Combo2
      .AddItem "a"
      .AddItem "b"
      .AddItem "c"
   End With

   With Combo3
      .AddItem "a"
      .AddItem "b"
      .AddItem "c"
   End With

  'load. If file does not exist, it is created,
  'and the values will = 0 (the first list item).
   Dim hFile As Integer
   Dim CIndex As ComboIndexSelections

   hFile = FreeFile
   Open "myfile.dat" For Random Access Read As #hFile Len = Len(CIndex)
       Get #hFile, 1, CIndex
   Close #hFile

   Combo1.ListIndex = CIndex.C1
   Combo2.ListIndex = CIndex.C2
   Combo3.ListIndex = CIndex.C3

End Sub

Private Sub Form_Unload(Cancel As Integer)

   Dim hFile As Integer
   Dim CIndex As ComboIndexSelections

   CIndex.C1 = Combo1.ListIndex
   CIndex.C2 = Combo2.ListIndex
   CIndex.C3 = Combo3.ListIndex

   hFile = FreeFile
   Open "myfile.dat" For Random Access Write As #hFile Len = Len(CIndex)
       Put #hFile, 1, CIndex
   Close #hFile

End Sub

By adding a FileExists check you can bypass the creation of the file in the
load event, thereby preventing the first combo item to become selected if
the file doesn't exist.  You can find a FileExists routine on my site in the
code library FileAPI section
http://www.mvps.org/vbnet/code/fileapi/fileexists.htm.

Note as well that in setting the combo selected item in the Load event, you
will fire the combo's Click event for each combo set. If this is
undesirable, add a flag that bypasses the code if loading, ie ...

'form general declarations
Private bSkipClick as Boolean

'Load event, before setting the ListIndex ...
bSkipClick = True

'Load event, after setting the ListIndex ...
bSkipClick = False

'Combo Click events
Sub ..Combo x Click ...

    If bSkipClick then Exit Sub

   '<other code that normally executes on a click>
End Sub

--

Randy Birch
MVP Visual Basic

Take the vb.net poll at:
http://www.mvps.org/vbnet/
http://www.mvps.org/ccrp/

Please respond only to the newsgroups so all can benefit.


: I have a form with several combo boxes. Their values are set via .additem
: and they work OK.
:
: BUT, when I close and reopen the form they are all blank. How can I get
them
: to show their current values?
:
: I have tried me.comboName.text=strValue but to no avail....
:
: Thanks,
:
: Jon
:
:



Wed, 02 Jul 2003 23:02:59 GMT  
 Combo boxes don't default to current value
Thans Randy,

That's really useful stuff!

Jon


Quote:
> Combo's don't store their select item state when the form is unloaded. You
> will need to keep track of each combo's ListIndex and reset that in the
Load
> event. If the form is just hidden instead, all values set in the form
> persist.

> If you are looking for saving the selected index between running the app,
> you'll need to save the listindexes into a file or registry and load these
> in the Load event, setting the appropriate combo to the correct value. If
> you opt for the file method, you can use either ini files (see
> http://www.mvps.org/vbnet/code/fileapi/pprofilebasic.htm for the basics of
> this) or a Random Access file and user-defined type (ie...

> Private Type ComboIndexSelections
>     C1 As Integer
>     C2 As Integer
>     C3 As Integer
>    '...etc
> End Type

> Private Sub Form_Load()

>    With Combo1
>       .AddItem "a"
>       .AddItem "b"
>       .AddItem "c"
>    End With

>    With Combo2
>       .AddItem "a"
>       .AddItem "b"
>       .AddItem "c"
>    End With

>    With Combo3
>       .AddItem "a"
>       .AddItem "b"
>       .AddItem "c"
>    End With

>   'load. If file does not exist, it is created,
>   'and the values will = 0 (the first list item).
>    Dim hFile As Integer
>    Dim CIndex As ComboIndexSelections

>    hFile = FreeFile
>    Open "myfile.dat" For Random Access Read As #hFile Len = Len(CIndex)
>        Get #hFile, 1, CIndex
>    Close #hFile

>    Combo1.ListIndex = CIndex.C1
>    Combo2.ListIndex = CIndex.C2
>    Combo3.ListIndex = CIndex.C3

> End Sub

> Private Sub Form_Unload(Cancel As Integer)

>    Dim hFile As Integer
>    Dim CIndex As ComboIndexSelections

>    CIndex.C1 = Combo1.ListIndex
>    CIndex.C2 = Combo2.ListIndex
>    CIndex.C3 = Combo3.ListIndex

>    hFile = FreeFile
>    Open "myfile.dat" For Random Access Write As #hFile Len = Len(CIndex)
>        Put #hFile, 1, CIndex
>    Close #hFile

> End Sub

> By adding a FileExists check you can bypass the creation of the file in
the
> load event, thereby preventing the first combo item to become selected if
> the file doesn't exist.  You can find a FileExists routine on my site in
the
> code library FileAPI section
> http://www.mvps.org/vbnet/code/fileapi/fileexists.htm.

> Note as well that in setting the combo selected item in the Load event,
you
> will fire the combo's Click event for each combo set. If this is
> undesirable, add a flag that bypasses the code if loading, ie ...

> 'form general declarations
> Private bSkipClick as Boolean

> 'Load event, before setting the ListIndex ...
> bSkipClick = True

> 'Load event, after setting the ListIndex ...
> bSkipClick = False

> 'Combo Click events
> Sub ..Combo x Click ...

>     If bSkipClick then Exit Sub

>    '<other code that normally executes on a click>
> End Sub

> --

> Randy Birch
> MVP Visual Basic

> Take the vb.net poll at:
> http://www.mvps.org/vbnet/
> http://www.mvps.org/ccrp/

> Please respond only to the newsgroups so all can benefit.



> : I have a form with several combo boxes. Their values are set via
.additem
> : and they work OK.
> :
> : BUT, when I close and reopen the form they are all blank. How can I get
> them
> : to show their current values?
> :
> : I have tried me.comboName.text=strValue but to no avail....
> :
> : Thanks,
> :
> : Jon
> :
> :



Thu, 03 Jul 2003 18:43:17 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. How to set default value of combo box to value of record in database

2. ***Default value don't work with AddNew-Upadte

3. ***Default value don't work with AddNew-Upadte

4. ***Default value don't work with AddNew-Upadte

5. Save Text Boxes Values To Combo Box's Item

6. Databound combo boxes don't populate text field after Find

7. default value in combo box to last selected

8. Combo box default value - Help!

9. Default Value in Combo Box?

10. Default value for dropdown combo list box?

11. setting a default value to a Combo box

12. Default Values for Combo Box Style 2

 

 
Powered by phpBB® Forum Software