Quantcast
Channel: Visual Basic Tips and Tricks
Viewing all articles
Browse latest Browse all 2212

resize buttoin in flowlayoutpanel

$
0
0

Ho un form che contiene un FlowLayoutPanel1 con dock fill a prendere tutta la dimensione del form.

Con delcodice ci devo creare dentro un numero n di button e questi butto avranno del testo con lunghezza variabile.
Il form dovrebbe ancorarsi alla parte destra del form padre. ma questo non è un problema.

Vorrei che i button avessero la stessa larghezza del form a va bene si fa con il .Width in fase di creazione.

Il io problema è che se faccio il resize del form anche i button si devono adattare alla nuova dimensione e quindi essere sempre estesi da margine sx a margine dx.

Nel codice seguente che ho provato, mi fa un errore in fase di creazione del form.

Qualcuno ha un' idea di come aggirare l' ostacolo ?

 

 

Public Class Form2

 

    Public mybutton(5) As Button

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        ' Create the button dynamically And set its properties.

        Dim s As String

        s = " My Button "

        For i = 0 To 4

 

 

            mybutton(i) = New Button

            'mybutton(i).Left = 10 + (i * 15)

            'mybutton(i).Top = 10 + (i * 15)

 

            mybutton(i).Text = s ' il testo per adesso è fisso poi prenderà dei dati da dei campi su un db a lunghezza variabile

            mybutton(i).Tag = i ' quello che voglio scrivere nel pulsante

 

 

            mybutton(i).Dock = DockStyle.Fill

 

            mybutton(i).AutoSizeMode = AutoSizeMode.GrowOnly

            mybutton(i).AutoSize = True

 

            mybutton(i).Font = New Font("Arial", 22)

 

            ' mybutton(i).Width = Me.Width

 

            FlowLayoutPanel1.Controls.Add(mybutton(i))

            AddHandler mybutton(i).Click, AddressOf PrintMessage

 

        Next i

 

    End Sub

 

 

    Private Sub Form2_Resize(sender As Object, e As EventArgs) Handles MyBase.Resize

        Dim x As Integer

        For Each C As Button In FlowLayoutPanel1.Controls

            x = x + 1

            mybutton(x).Width = Me.Width

        Next

    End Sub

 

    Private Sub PrintMessage(sender As Button, e As EventArgs)

        Dim id As Integer

        id = Integer.Parse(sender.Tag) ' converte in numero

 

        MessageBox.Show("Dynamic event happened! index "& id)

 

    End Sub

 

 

nvece con quest' altro codice quello che voglio fare mi funziona ma solo col button n°4

Codice: Seleziona tutto

Public Class Form3

 

    Dim btn As Button

    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load

 

 

        FlowLayoutPanel1.Controls.Clear()

 

        For i = 0 To 4

            btn = New Button()

            btn.Name = "btn "& i

            btn.Tag = i

            btn.Text = i

 

            btn.AutoSizeMode = AutoSizeMode.GrowOnly

            btn.AutoSize = True

 

            btn.Font = New Font("Arial", 14.0F, FontStyle.Bold)

            ' btn.UseCompatibleTextRendering = true;

            btn.BackColor = Color.Green

            ' btn.Height = 57

            ' btn.Width = 116

            btn.Width = Me.Width

            btn.Dock = DockStyle.Fill

            AddHandler btn.Click, AddressOf PrintMessage

 

 

            FlowLayoutPanel1.Controls.Add(btn)

 

        Next i

 

    End Sub

    Private Sub PrintMessage(sender As Button, e As EventArgs)

        Dim id As Integer

        id = Integer.Parse(sender.Tag) ' converte in numero

 

        MessageBox.Show("Dynamic event happened! index "& id)

        'MsgBox "Hai premuto il pulsante "& mybutton(i).Name & " di indice "& CStr(Index)

 

    End Sub

 

    Private Sub Form3_Resize(sender As Object, e As EventArgs) Handles MyBase.Resize

        '    'Dim x As Integer

        For Each C As Button In FlowLayoutPanel1.Controls

            '    '    x = x + 1

            btn.Width = Me.Width

        Next

 

    End Sub

End Class

 

 


Viewing all articles
Browse latest Browse all 2212