如何向FlowLayoutPanel添加矩形 [英] How can I add a rectangle to a FlowLayoutPanel

查看:98
本文介绍了如何向FlowLayoutPanel添加矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试将一个填充的矩形添加到FlowLayoutPanel,以便它通过循环在FlowLayoutPanel中创建的按钮后面。但是我无法让它发挥作用。该程序一次从访问数据库创建1个按钮。我需要它在它创建的按钮后面绘制一个矩形 - 对于循环中的每个按钮。



我到目前为止的代码在下面,除了矩形绘图



 私有  Sub  TABONEButtons()

' 下面创建按钮以读取访问权限数据库
' 它一遍又一遍地循环,直到达到计数

Dim buttonlabel
Dim count 作为 整数

count = Table1BindingSource.Count
Timer1。停止()



Dim BTN 由于 按钮

' 设置控件的属性[按钮]

buttonlabel = ButtonTextTextBox.Text

BTN.Name = buttonlabel
BTN.Text = buttonlabel
BTN.Location = 点(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

创造即使T HANDLER
AddHandler BTN.Click, AddressOf myButtonHandler_Click
< span class =code-comment>' 将新控件[按钮]添加到控件集合中
FlowLayoutPanel1.Controls.Add( BTN)



' ------- - 计算下面的按钮代码--------------------------------------------- ---------------


inc_control = inc_control + 1



' 转到下一条记录----- ----------------------

Table1BindingSource.MoveNext()

' MsgBox(搜索已运行)



' MsgBox( IDTextBox.Text)

如果 inc_control< count 然后 TABONEButtons()


结束 Sub







 私有  Sub  FlowLayoutPanel1_Paint(发件人 As   Object ,e  As  PaintEventArgs)句柄 FlowLayoutPanel1.Paint 
MsgBox( paint flow

Dim p 作为 笔(Color.DarkBlue,< span class =code-digit> 10 .0F)

e.Graphics.DrawRectangle(p, New Rectangle( loc_control.X + 10 ,_
loc_contr ol.Y, 150 10 ))
结束 Sub

解决方案

填充矩形是不是控制。只需将其包裹在一个控件中。创建自定义控件并在其上呈现您想要的任何内容。 (或者,使用 PictureBox ,图像显示一个矩形。如果图像在任何意义上都应该是动态的,请不要这样做。)请查看我过去的答案:

在图片框中附加图片

在C#中绘制一个矩形

我该怎么做从旧图纸中清除面板

什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...))

在面板上捕捉绘图

在mdi子表单之间绘制线条



-SA


感谢您的输入 - 我已经尝试了图片框,它已经完成了一半,但它将按钮放在图片框旁边,它需要在它上面,以便图片框成为按钮的背景。这需要允许我将透明背景图像加载到按钮中,同时通过图片框查看背景中的按钮颜色。



到目前为止我的代码是:



 私人  Sub  TABONEButtons()

' 下面创建阅读访问数据库的按钮
' 它一遍又一遍地循环,直到达到计数

Dim buttonlabel
Dim count 正如 整数

count = Table1BindingSource.Count
Timer1。停止()



Dim BTN 作为 按钮

' 设置控件的属性[按钮]

buttonlabel = ButtonTextTextBox.Text

Dim pb As New PictureBox
pb.Width = 100 ' 或其他
pb.Height = 100
pb.Top = loc_control.X ' 或者其他
pb.Left = loc_control.Y
' pb.ImageLocation =dog.png
pb.BackColor = Color.Aqua
FlowLayoutPanel1.Controls.Add(pb)


BTN.Name = buttonlabel
BTN.Text = buttonlabel
BTN.Locat ion = 点(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

' 创建事件处理程序
AddHandler BTN.Click, AddressOf myButtonHandler_Click
' 添加新控件[按钮]收集控制
FlowLayoutPanel1.Controls .Add(BTN)



' ---- ----计算下面的按钮代码------------------------------------------ ------------------


inc_control = inc_control + 1



' 转到下一条记录 - -------------------------

Table1BindingSource.MoveNext()

' MsgBox(搜索已运行)



' MsgBox(IDTextBox.Text)

如果 inc_control< count 然后 TABONEButtons()


结束


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

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




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

解决方案

"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


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:

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


这篇关于如何向FlowLayoutPanel添加矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆