Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been trying and trying to add a filled rectangle to a FlowLayoutPanel so that its behind the buttons im creating in the FlowLayoutPanel via a loop. But I cant get it to work. The program creates 1 button at a time from an access database. I need it to draw a rectangle behind the button it creates - for each button in the loop.

The code I have so far is below and all works except for the rectangle drawing

VB
Private Sub TABONEButtons()

    ' below create the buttons from reading the access database
    ' it loops over and over until the count is reached

    Dim buttonlabel
    Dim count As Integer

    count = Table1BindingSource.Count
    Timer1.Stop()



    Dim BTN As New Button

    'SET THE PROPERTIES OF THE CONTROL[BUTTON]

    buttonlabel = ButtonTextTextBox.Text

    BTN.Name = buttonlabel
    BTN.Text = buttonlabel
    BTN.Location = New Point(loc_control.X + 10, _
                                    loc_control.Y)
    BTN.Height = 100
    BTN.Width = 100
    BTN.FlatStyle = FlatStyle.Flat
    loc_control.Y += BTN.Height + 10
    BTN.BackColor = Color.FromName(ButtonColourTextBox.Text)
    BTN.ForeColor = Color.White
    BTN.TextAlign = ContentAlignment.BottomCenter
    BTN.Image = PhotoPictureBox.Image

    'CREATE THE EVENT HANDLER
    AddHandler BTN.Click, AddressOf myButtonHandler_Click
    'ADD THE NEW CONTROL[BUTTON] TO THE COLLECTION OF CONTROLS
    FlowLayoutPanel1.Controls.Add(BTN)



    '-------- count button code below ------------------------------------------------------------


    inc_control = inc_control + 1



    'move to next record---------------------------

    Table1BindingSource.MoveNext()

    'MsgBox("search has been run")



    'MsgBox(IDTextBox.Text)

    If inc_control < count Then TABONEButtons()


End Sub



VB
Private Sub FlowLayoutPanel1_Paint(sender As Object, e As PaintEventArgs) Handles FlowLayoutPanel1.Paint
    MsgBox("paint flow")

    Dim p As New Pen(Color.DarkBlue, 10.0F)

    e.Graphics.DrawRectangle(p, New Rectangle(loc_control.X + 10, _
                                    loc_control.Y, 150, 10))
End Sub
Posted

"Filled rectangle" is not a control. Simply wrap it in a control. Create a custom control and render whatever you want on it. (Alternatively, use PictureBox with image showing a rectangle. Don't do it if you image is supposed to be dynamic, in any sense.) Please see my past answers:
Append a picture within picturebox,
draw a rectangle in C#,
How do I clear a panel from old drawing,
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...)),
capture the drawing on a panel,
Drawing Lines between mdi child forms.

—SA
 
Share this answer
 
v2
Thanks for your input - I have tried the picturebox and it half works but it puts the buttons beside the picturebox and it needs to be over it so that the picturebox becomes the background for the button. This is needed to allow me to load transparent background images into the button while seeing the buttons colour in the background via the picturebox.

the code i have so far is:

VB
Private Sub TABONEButtons()

    ' below create the buttons from reading the access database
    ' it loops over and over until the count is reached

    Dim buttonlabel
    Dim count As Integer

    count = Table1BindingSource.Count
    Timer1.Stop()



    Dim BTN As New Button

    'SET THE PROPERTIES OF THE CONTROL[BUTTON]

    buttonlabel = ButtonTextTextBox.Text

    Dim pb As New PictureBox
    pb.Width = 100 'or whatever
    pb.Height = 100
    pb.Top = loc_control.X 'or whatever
    pb.Left = loc_control.Y
    'pb.ImageLocation = "dog.png"
    pb.BackColor = Color.Aqua
    FlowLayoutPanel1.Controls.Add(pb)


    BTN.Name = buttonlabel
    BTN.Text = buttonlabel
    BTN.Location = New Point(loc_control.X + 10, _
                                    loc_control.Y)
    BTN.Height = 100
    BTN.Width = 100
    BTN.FlatStyle = FlatStyle.Flat
    loc_control.Y += BTN.Height + 10
    BTN.BackColor = Color.FromName(ButtonColourTextBox.Text)
    BTN.ForeColor = Color.White
    BTN.TextAlign = ContentAlignment.BottomCenter
    BTN.Image = PhotoPictureBox.Image

    'CREATE THE EVENT HANDLER
    AddHandler BTN.Click, AddressOf myButtonHandler_Click
    'ADD THE NEW CONTROL[BUTTON] TO THE COLLECTION OF CONTROLS
    FlowLayoutPanel1.Controls.Add(BTN)



    '-------- count button code below ------------------------------------------------------------


    inc_control = inc_control + 1



    'move to next record---------------------------

    Table1BindingSource.MoveNext()

    'MsgBox("search has been run")



    'MsgBox(IDTextBox.Text)

    If inc_control < count Then TABONEButtons()


End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900