Вопрос

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.

Это было полезно?

Решение

From your comments, I understand that your ideas regarding adding buttons at runtime are not too clear and thus I am including a small code which hopefully will help you on this front. Start a new project and put a Panel (Panel5) and a Button (AddButtons) on it, and write this code:

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

This code will add a new button to the panel every time you click on AddButtons. All the buttons will have an associated Click Event (the same one for all of them): Button_Click. The way to know which button is the current one inside this method is via sender, as shown in the code (you can put as many conditions as buttons. The names are given sequentially starting from 1; but you can take any other property as reference, curButton is the given Button Control).

Bear in mind that one of the problems you have to take care of is the location of the buttons. The code above has a very simplistic X/Y values (Left/Top properties) auto-increase which, logically, will not deliver what you want.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top