Reputation: 67
I have spent a long time trying to find a solution for the problem I am having with comboboxes within a child form. I'm sure this must be a Microsoft error and not something I'm doing wrong... I would really appreciate some help, Thanks in advance...
The best way to explain the problem would be to tell you what I have done so far:
If you open a new Project in VB and add two Windows Forms. Form1 will be the parent. Form2 will be the child.
On Form1 add 1 x panel(Form2 will open up within the panel so make sure the panel is big enough to fit Form2) and 1 x button. The only code needed for this form is:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Form2.Show()
End Sub
On Form2 Add one Textbox and one combobox. Copy the following code into Form2:
Private Sub Form2_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
ActivateMdiChild(Me)
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TopLevel = False
Form1.Panel1.Controls.Add(Me)
Me.BringToFront()
End Sub
Now run the program and click button1 to open form2 (form2 should now open within panel1).
The comboboxes and textboxes lose some of there functions for example: 1. You cant set focus on combobox without hitting drop-down 2. You cant highlight text properly in either the combobox or textbox. 3. You cant set the cursor index position using your mouse in the combobox or text box.
Does anyone know a way around this?? Mainly for the combobox as I've got some functions working for the textbox.
Regards, Ben
Upvotes: 2
Views: 936
Reputation: 56
Another way to get the functionality you are trying to get on Form2
is to set Form1
as an MDI container through the IsMdiContainer
property on Form1
. You could then call Form2
in the Form1
button click with something like this:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim f As New Form2()
f.MdiParent = Me
f.Show()
End Sub
This would obviously create a new instance of Form2
every time you click Button1, so you could change the click actions to Form2.Show()
and in Form2
constructor set the MdiParent
to Form1
. That should only create one instance of Form2
.
I'm curious why you're mimicking MDI functionality through the Panel?
Upvotes: 0
Reputation: 81675
Your click event should probably look something like this:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim f As New Form2
f.TopLevel = False
f.Dock = DockStyle.Fill
f.Visible = True
Panel1.Controls.Add(f)
f.BringToFront()
End Sub
and you can pretty much discard the code you have posted for Form2. Moving a form that is an MDI child of the parent to a child control of a panel is a bit awkward. I'm guessing that is not what you are really trying to do.
Also, this current code will just keep putting a new Form2 over any existing controls or forms that are already in the panel. Make sure to dispose of unwanted controls.
Upvotes: 0