gCanuck
gCanuck

Reputation: 39

How to call a sub in first form from a second form and inserting controls into the first form

I believe this is just a syntax issue and shouldn't be difficult, I'm just not overly familiar with VB.net. I am using FormA to control FormB. When I execute the command from FormA, I want to create 1+x different controls (webbrowser) on FormB. To do this, I'm calling a subProcedure that resides in FormB and dynamically creating my controls from there. Problem is, no controls are created or displayed.

I know the sub is called as I can step through it, but I suspect it's trying to display the controls in FormA. If I move the sub to FormA, the controls display as expected on FormA. Hence why I think it's a syntax issue. So, going from the code example, how would I make sure the controls are added to FormB?

public sub displayonformb(byval sheetcount as integer)
for i as integer = 1 to sheetcount
    dim wb as new webbrowser
    with wb
        .name = "myname"
        .navigation(new uri("blah blah blah")
        .location = new point(((i-1)*(screenwidth / sheetcount)), hgt)
        .size = new size(wdth, hgt)
        me.controls.add(wb)
    end with
next

So, my assumption here is either with the .location or (much more likely) the me.controls.add(wb) statement. I tried changing the "me." to "FormB." but studio complained that I should use 'me.' instead.

Upvotes: 0

Views: 134

Answers (3)

Joel Coehoorn
Joel Coehoorn

Reputation: 416121

From the comment:

I call the method like so:

 Dim frm As New FormB frm.DisplayOnMainFormB(sheetCount).... 

The problem is the New keyword. This means you're using a brand new different instance of FormB. You need a reference to the same existing instance already shown on the screen.

Upvotes: 1

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112712

I suspect that you are calling the sub on a wrong form instance. Somewhere you are writing

Dim frmB As New FormB()
frmB.Show()

Somewhere else you are writing

Dim frmB As New FormB() 'Creates a new form object.
frmB.DisplayOnFormB(1)

But this is another instance of FormB that is not displayed anywhere.

Instead, store a reference to the original form in a field. In FormA write

Public Class FormA
    Private m_frmB As FormB
    ...

Then still in FormA open the other form with

m_frmB = New FormB()
m_frmB.Show()

And when you want to add the control:

m_frmB.DisplayOnFormB(1)

I.e., work with the same form instance (form object).

Upvotes: 1

Precious Uwhubetine
Precious Uwhubetine

Reputation: 3007

You are using Me.controls.add when adding the items. You should pass the form as a parameter. This is not a syntax error at all. Try passing the form to add controls to as a parameter to the function.

Public Sub displayonformb(ByVal sheetcount as integer, form As Form)
    For i As integer = 1 To sheetcount
        Dim wb as new WebBrowser
        With wb
            .name = "myname"
            .navigation(new uri("blah blah blah")
            .location = new point(((i-1)*(screenwidth / sheetcount)), hgt)
            .size = new size(wdth, hgt)
            form.controls.add(wb)
        end with
    Next
End Sub

Upvotes: 1

Related Questions