0

I'm using MDI container to run my bussiness application I created for my client. Since using MDI means that when I open several forms they will still run in background all the time untill I close them manualy.

What I need is to make User Control or anything else that could preview all opened forms in Tab Form so my client can easily close all or some of opened forms without closing a form he is curently viewing.

For now I have used this code, so for now only first clicked item from menu appears as button, but not others clicked menu items.

Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked
    Dim Button As New Button
    Me.Panel5.Controls.Add(Button)
    Button.Text = e.ClickedItem.Name
    Button.Width = 50
    Button.Height = 25
End Sub

Now I need to write a code to add more buttons bellow, also should add a code for adding buttons only when I click on SubMenu item (The one when is clicked new Form appear).
And also, I should now add a little Close button into previewed User-Button-Control.

4

1 回答 1

1

从您的评论中,我了解到您关于在运行时添加按钮的想法不太清楚,因此我包含了一个小代码,希望能在这方面对您有所帮助。启动一个新项目并在其上放置一个 Panel ( Panel5) 和一个 Button ( AddButtons),然后编写以下代码:

Dim lastButtonIndex, lastLeft, lastTop As Integer

Private Sub Button_Click(sender As System.Object, e As System.EventArgs)

    Dim curButton As Button = DirectCast(sender, Button)

    If (curButton.Name = "Button1") Then
        'do Button1 stuff
    End If
    'etc.

End Sub

Private Sub addNewButton()

    lastButtonIndex = lastButtonIndex + 1
    lastLeft = lastLeft + 5
    lastTop = lastTop + 5

    Dim Button As New Button
    With Button
        .Name = "Button" + lastButtonIndex.ToString()
        .Text = "Button" + lastButtonIndex.ToString()
        .Width = 50
        .Height = 25
        .Left = lastLeft
        .Top = lastTop
        AddHandler .Click, AddressOf Button_Click
    End With

    Me.Panel5.Controls.Add(Button)

End Sub

Private Sub ButtonAddButtons_Click(sender As System.Object, e As System.EventArgs) Handles AddButtons.Click
    addNewButton()
End Sub

每次单击时,此代码都会向面板添加一个新按钮AddButtons。所有按钮都将有一个关联Click Event(所有按钮都相同)Button_Click:。在这个方法中知道哪个按钮是当前按钮的方法是 via sender,如代码所示(你可以放按钮一样多的条件。名称从 1 开始依次给出;但你可以取任何其他属性作为参考,curButton是给定的Button Control)。

请记住,您必须注意的问题之一是按钮的位置。上面的代码有一个非常简单的 X/Y 值(Left/Top属性)自动增加,从逻辑上讲,它不会提供你想要的。

于 2013-10-04T09:20:13.960 回答