忽略此问题以解决主要问题,下面只是想法
场景:我在Visual Basic中创建了一个完整的私人消息系统。
尝试:我想为用户制作收件箱。这些消息将从MySQL数据库中检索出来,并显示在由Panel(Visual Basic中的控件类型)制作的小型自定义网格中,每个可点击的消息将被点击,整个消息将被显示。
到目前为止:虽然不是太多(谈论收件箱),但我只编写了MySQL查询。 我会在收件箱之后发送和阅读PM,因为收件箱似乎比其他两件事情更复杂。
我真的想知道如何实现这一目标。我几乎到处搜索,无论是PHP和基于Web还是什么都没有。我想知道如何动态创建面板和标签等控件并显示PM。有没有其他方法可以做到这一点,而不是在DataGridView中显示PM? (我真的不想使用它,因为它不是我想要的。)
供参考:自定义网格是这样的:
MySQL PM表:
PMId - The ID for the message (Auto Incremented)
Sender_Name - The person sending the message
Receiver_Name - The person receiving the message
Subject - The subject of the message
Date_Sent - Date on which the message was sent
PM_Read - If the PM has been read (0 for not read, 1 for read)
Deleted - If the PM has been deleted (0 for not deleted, 1 for deleted)
答案 0 :(得分:1)
查看构建表单的代码隐藏,您将看到如何在运行时创建控件。例如,我创建了一个表单,添加了一个带有按钮和标签的面板,这是在设计器中创建的代码:
Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button()
Me.Label1 = New System.Windows.Forms.Label()
Me.Panel1 = New System.Windows.Forms.Panel()
Me.Panel1.SuspendLayout()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(3, 3)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(75, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
Me.Button1.UseVisualStyleBackColor = True
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(3, 29)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(39, 13)
Me.Label1.TabIndex = 1
Me.Label1.Text = "Label1"
'
'Panel1
'
Me.Panel1.Controls.Add(Me.Button1)
Me.Panel1.Controls.Add(Me.Label1)
Me.Panel1.Location = New System.Drawing.Point(12, 12)
Me.Panel1.Name = "Panel1"
Me.Panel1.Size = New System.Drawing.Size(198, 69)
Me.Panel1.TabIndex = 2
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(284, 262)
Me.Controls.Add(Me.Panel1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.Panel1.ResumeLayout(False)
Me.Panel1.PerformLayout()
Me.ResumeLayout(False)
End Sub
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Panel1 As System.Windows.Forms.Panel
您可以在运行时执行相同的操作。
Dim iTop As Int32 = 5
For Each DR As DataRow In DT.Rows
Dim pnl As New Panel
pnl.Location = New System.Drawing.Point(12, iTop)
pnl.Size = New System.Drawing.Size(198, 40)
Dim lbl As New Label
lbl.Location = New System.Drawing.Point(3, 3)
lbl.Size = New System.Drawing.Size(39, 13)
lbl.Text = DR("Some field from your table")
'Add to panel
pnl.Controls.Add(lbl)
'Add to Form
Me.Controls.Add(pnl)
'Add to the top location so the next set of controls are not on top of the old ones
iTop += 50
Next