How do i do this.

I have a few buttons that i add dynamically to a panel at the bottom of an MDI form. each button indicates which form is opened. when a form is opened a button gets added to the panel. when the form is closed the button is deleted.

when i click the button I want the respective form to get focus. i dont know how to achieve this.

Thanks in advance
LVL 1
muthiahmerchantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi muthiahmerchant;

The simplest way to do this is when you create the Button for that Form assign that Form to the Tag property, for example:

    Dim f3 As New Form3()
    Dim b3 As New Button()
    b3.Tag = f3

When you click on the button you can set focus to that Form in the following way in the click event handler of the button:

    Dim f3 As Form3 = CType( b3.Tag, Form3)
    f3.Focus()


I hope that this is of some help.

Fernando

   
muthiahmerchantAuthor Commented:
The problem is that, since the buttons are created dynamically when a new mdi child form is opened, I don't know how to write code in the event handler of the button.

Thanks
Fernando SotoRetiredCommented:
Hi muthiahmerchant;

Here is a small mdi program which when Child 1 and Child 2 buttons are clicked they create a button for the new child form and wiers up an event handler.

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Panel1 = New System.Windows.Forms.Panel
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Panel1.SuspendLayout()
Me.SuspendLayout()
'
'Panel1
'
Me.Panel1.Controls.Add(Me.Button2)
Me.Panel1.Controls.Add(Me.Button1)
Me.Panel1.Dock = System.Windows.Forms.DockStyle.Bottom
Me.Panel1.Location = New System.Drawing.Point(0, 274)
Me.Panel1.Name = "Panel1"
Me.Panel1.Size = New System.Drawing.Size(584, 40)
Me.Panel1.TabIndex = 1
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(8, 8)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 2
Me.Button1.Text = "Child 1"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(88, 8)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 3
Me.Button2.Text = "Child 2"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(584, 314)
Me.Controls.Add(Me.Panel1)
Me.IsMdiContainer = True
Me.Name = "Form1"
Me.Text = "Form1"
Me.Panel1.ResumeLayout(False)
Me.ResumeLayout(False)

    End Sub

#End Region


    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        ' Create new child 1 form, give it a name, and assign MDI parent
        Dim C1 As New Child1
        C1.Name = "Child1"
        C1.MdiParent = Me
        ' Create dynamically a button, set its text and name, assign child
        ' form to the Tag property and locate the button on the form/Pannel
        ' MDI set its parent
        Dim newButton As New Button
        newButton.Text = "Set Child 1"
        newButton.Name = "btnSetChild1"
        newButton.Tag = C1
        ' The location of the buttons should be set up with variables so that
        ' they may be placed at specific locations depending on available space
        newButton.Location = New Point(200, 8)
        newButton.Parent = Panel1
        ' Wire up the new button to an event handler, event handler must have
        ' been created at design time. The new event handler is the new button
        ' name and the "." followed by the event to handle, then the address of
        ' the event handler in this case it is SetFocusChild_Click.
        AddHandler newButton.Click, AddressOf SetFocusChild_Click
        newButton.Show()
        C1.Show()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button2.Click

        Dim C2 As New Child1
        C2.Name = "Child2"
        C2.MdiParent = Me
        Dim newButton As New Button
        newButton.Text = "Set Child 2"
        newButton.Name = "btnSetChild2"
        newButton.Tag = C2
        newButton.Location = New Point(280, 8)
        newButton.Parent = Panel1
        AddHandler newButton.Click, AddressOf SetFocusChild_Click
        newButton.Show()
        C2.Show()

    End Sub

    Private Sub SetFocusChild_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs)

        ' This event handler is for the dynamically creatted buttons
        ' that set focus to the form connected to it. This must be
        ' created at design time and already set up.
        Dim btn As Button = CType(sender, Button)
        Dim Childfrm As Form = CType(btn.Tag, Form)

        Childfrm.Focus()

    End Sub

End Class


I hope that this is of some help.

Fernando

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
muthiahmerchantAuthor Commented:
Perfect.

Thanks a ton.
Fernando SotoRetiredCommented:
No problem glad I was able to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.