Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

same combobox in different forms 1

Status
Not open for further replies.

noun

Programmer
Nov 27, 2003
11
AR
I've populated a combobox at application start, but would like to re-use it in several forms without loading n times all data in n comboboxes, and without the need for cloning all methods.

set cboBox.container = another_container

works well in the same form but i cant move it to another form? Is it possible?

Thanks in advance.
 
Hi noun:

As for avoiding the cloning of the methods, one can write the method on one form (declaring the subs as either friend or public) and calling the method from the other forms.

For example, suppose FormA is where one stores the methods and FormB is a form that wants to use the methods in FormA.

Code in FormA:
Code:
Friend Sub cboMyBox_Click(Index As Integer)
    ' Do whatever needs to be done
End Sub

Code in FormB:
Code:
Private Sub cboMyBox_Click(Index As Integer)
    FormA.cboMyBox_Click(Index)
End Sub

As for avoiding loading all the various copies of the control, I am not sure one can avoid this.


Cassie
PIII-500Mhz-128MB
Win98SE-VB6SP5
 
You can't avoid having multiple copies of the combobox on the separate forms. But what you should be able to do is move all the functionality to a class which can be shared between the forms.

In fact, this is done so often there's a design pattern for it -- MVC (model-view-controller).

Read about it at:

Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
>I am not sure one can avoid this

Of course you can ...

Create a new project. Make sure you have two forms. And a module. Stick a combobox and a command button on Form1, and a command button on Form2

Add the following code to the module:
Code:
[blue]Option Explicit

Public Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
[/blue]
Now add the following code to Form1:
Code:
[blue]Option Explicit

Private Sub Combo1_Click()
    MsgBox "You chose " & Combo1.Text
End Sub

Private Sub Command1_Click()
    SetParent Combo1.hWnd, Form2.hWnd
    Form2.Show
    
End Sub

Private Sub Form_Load()
    Combo1.AddItem "Hello"
    Combo1.AddItem "Goodbye"
    Combo1.AddItem "test"
End Sub[/blue]
And the following code to Form2:
Code:
[blue]Option Explicit

Private Sub Command1_Click()
    SetParent Form1.Combo1.hWnd, Form1.hWnd
    Me.Hide
End Sub[/blue]
 
interesting - I'm going to have to try that later and see how it works.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
strongm
NICELY DONE - Never saw that before. The drawback I see however is that form1 always has to stay open. Depending on whatever else may be on the form, it may be tying up resources. HOWEVER, (the blind man sees) I assume that this can be done with most controls holding data (grids, listboxes, comboboxes). So I could put all of my repeat contols on a single form, load the data and use them on all my other forms - again Neatly done.
 
>So I could put all of my repeat contols on a single form, load the data and use them on all my other forms

Exactly
 
strongm:

Thanks for your interesting answer.

i'tested the "setparent" function and worked fine placing the graphic representation and behaviour of the combobox but...

in the form2, that is the new container for the combobox, the combobox cannot get the focus (navigating with keyboard, i can pass the focus with mouse).

Additionally, when i reference the combobox, for instance, when i read the data , i have to reference

form1.combobox

since the combobox seems not to exist really into the new container.

 
I noticed the other day that supposedly you can change the container property of a control. I haven't tried it to see how it works, but heres the example code from VB help:
Code:
Private Sub Form_Click()
   Static intX As Integer
   Select Case intX
      Case 0
         Set Command1.Container = Picture1
         Command1.Top= 0
         Command1.Left= 0
      Case 1
         Set Command1.Container = Frame1
         Command1.Top= 0
         Command1.Left= 0
      Case 2
         Set Command1.Container = Form1
         Command1.Top= 0
         Command1.Left= 0
   End Select
   intX = intX + 1
End Sub

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
By the way, if you make the fonts different for each container, and add
Code:
Command1.Font = Command1.Container.Font
your command button will take on the font of the container. You can use the container object for any sort of ambient property.

Bob
 
Yep - as the original post stated, this works fine ... on the same form.
 
Sorry, forgot where I was for a moment. However, using the Container object also works with SetParent (the code assumes two forms, Form1 and Form2, and two commandbuttons on Form1, Command1 and Command2):
Code:
Option Explicit
Dim cmd As CommandButton

Private Sub Command1_Click()
Static i As Integer
If i = 0 Then
    SetParent cmd.hWnd, Form2.hWnd
    i = 1
Else
    SetParent cmd.hWnd, Form1.hWnd
    i = 0
End If
cmd.Font = cmd.Container.Font
End Sub

Private Sub Form_Load()
Set cmd = Command2
Form2.Show
End Sub
Note that you can't directly reference the Command2 button in your Command1_Click code (you can't say Form2.Command2.Container.Font after setting Command2's parent to Form2), since it doesn't exist in Form2 at compile time and this will generate a compile error. A good example of where pointers come in handy.
 
Sorry, my comment was to tsdragon's post ( you just managed to sneak two posts in between ...)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top