积极答复者
VB.net 中如何开发像这样的界面。谢谢。

问题
答案
-
没有直接绑定到WinForm的控件能够像WebForm一样呈现此种效果。建议你动态生成:
1)设置WinForm的AutoScroll=True。
2)然后动态使用TableLayout进行布局——
Namespace WinFormCSharp Public Partial Class Form1 Inherits Form Public Sub New() InitializeComponent() End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Dim tpanel As New TableLayoutPanel() tpanel.Dock = DockStyle.Fill tpanel.Parent = Me tpanel.RowStyles.Clear() tpanel.ColumnStyles.Clear() tpanel.ColumnCount = 5 tpanel.RowCount = 5 '绘制行和列 For i As Integer = 1 To 5 tpanel.RowStyles.Add(New RowStyle(SizeType.Percent, 20F)) Next For i As Integer = 1 To 5 tpanel.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 20F)) Next '放控件 For row As Integer = 0 To 4 For col As Integer = 0 To 4 Dim lb As New Label() lb.Text = "Label" & ((row * 5) + (col + 1)) tpanel.Controls.Add(lb, col, row) lb.Dock = DockStyle.Fill lb.Font = New Font("黑体", 40.5F) Next Next End Sub End Class
End Namespace
【示例图】
QQ我:
下载MSDN桌面工具(Vista,Win7)
我的博客园
慈善点击,点击此处
- 已编辑 ThankfulHeartModerator 2012年2月6日 8:05
- 已建议为答案 Mike FengModerator 2012年2月8日 10:19
- 已标记为答案 Mike FengModerator 2012年2月21日 8:15
-
或者建议楼主也可以使用DataRepeater(Visual Studio Service Packs)工具,拖拽一个控件到WinForm,然后放上4个Label,然后循环执行,示例代码(label距离计算忽略)——
【代码】
Namespace WinFormCSharp Public Partial Class Form1 Inherits Form Private i As Integer = 1 Public Sub New() InitializeComponent() End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) dataRepeater1.DataSource = New Integer() {1, 2, 3, 4, 5} End Sub Private Sub dataRepeater1_ItemCloning(sender As Object, e As Microsoft.VisualBasic.PowerPacks.DataRepeaterItemCloneEventArgs) For Each item As var In e.Source.Controls TryCast(item, Label).Text = (System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)).ToString() Next End Sub End Class End Namespace
【设计界面】
【运行效果】
QQ我:
下载MSDN桌面工具(Vista,Win7)
我的博客园
慈善点击,点击此处
- 已编辑 ThankfulHeartModerator 2012年2月6日 8:06
- 已建议为答案 Mike FengModerator 2012年2月8日 10:19
- 已标记为答案 Mike FengModerator 2012年2月21日 8:15
-
谢谢。此问题已解决。写一个自定义控件。然后继承FlowLayoutPanel.问题就变的十分简单了。
- 已标记为答案 feiyun0112Moderator 2012年2月20日 6:23
-
图:
定义两个类。这个是第一个类。
Public Class Box
Private _Title As String
Private _Info As String
Private _Status As String
Private _BillCode As String
Public Sub New()' 此调用是设计器所必需的。
InitializeComponent()' 在 InitializeComponent() 调用之后添加任何初始化。
End Sub
Public Sub New(ByVal _BoxAttribute As BoxAttribute)
InitializeComponent()
With _BoxAttribute
_Title = .Title
_Info = .Info
_Status = .Status
_BillCode = .BillCode
Label_Title.Text = _Title
Label_Title.Font = .TitleTextFont
Label_Title.ForeColor = Color.FromArgb(.TitleTextColor)
Label_Info.Text = _Info
Label_Info.Font = .InfoTextFont
Label_Info.ForeColor = Color.FromArgb(.InfoTextColor)
Me.BackColor = Color.FromArgb(.BoxBakColor)
Me.Width = .BoxWidth
Me.Height = .BoxHeight
Me.Height = .BoxHeight
Me.Margin = New Padding(.BoxMargin)
End With
End Sub
Private Sub UserControl_Box_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.DoubleClick
MsgBox(_Status)
End Sub
End Class第二个类:
Public Class StateGrid
Private _TitleTextColor As Integer
Private _TitleTextFont As Font
Private _InfoTextColor As Integer
Private _InfoTextFont As Font
Private _FreeColor As Integer
Private _OnlineColor As Integer
Private _BookColor As Integer
Private _BoxMargin As Integer
Private _BoxWidth As Integer
Private _BoxHeight As Integer
Public Sub New()' 此调用是设计器所必需的。
InitializeComponent()
ReadConfig()
' 在 InitializeComponent() 调用之后添加任何初始化。End Sub
'读取配置
Private Sub ReadConfig()
Try
Dim strConfig As String = Application.StartupPath + "\PreferencesConfig.xml"
If System.IO.File.Exists(strConfig) = False Then
Return
End If
Dim ds As New DataSet
ds.ReadXml(strConfig)
_TitleTextColor = ds.Tables("Config").Rows(0)("TitleTextColor").ToString
Dim TitleTextFontName As String = _
ds.Tables("Config").Rows(0)("TitleTextFontName").ToString
Dim TitleTextFontSize As String = _
ds.Tables("Config").Rows(0)("TitleTextFontSize").ToString
Dim TitleTextFontBold As String = _
ds.Tables("Config").Rows(0)("TitleTextFontBold").ToString
Dim TitleTextFontItalic As String = _
ds.Tables("Config").Rows(0)("TitleTextFontItalic").ToString
Dim TitleTextFontStrikeout As String = _
ds.Tables("Config").Rows(0)("TitleTextFontStrikeout").ToString
Dim TitleTextFontUnderline As String = _
ds.Tables("Config").Rows(0)("TitleTextFontUnderline").ToString
Dim InfoTextFontName As String = _
ds.Tables("Config").Rows(0)("InfoTextFontName").ToString
Dim InfoTextFontSize As String = _
ds.Tables("Config").Rows(0)("InfoTextFontSize").ToString
Dim InfoTextFontBold As String = _
ds.Tables("Config").Rows(0)("InfoTextFontBold").ToString
Dim InfoTextFontItalic As String = _
ds.Tables("Config").Rows(0)("InfoTextFontItalic").ToString
Dim InfoTextFontStrikeout As String = _
ds.Tables("Config").Rows(0)("InfoTextFontStrikeout").ToString
Dim InfoTextFontUnderline As String = _
ds.Tables("Config").Rows(0)("InfoTextFontUnderline").ToString
_InfoTextColor = ds.Tables("Config").Rows(0)("InfoTextColor").ToString
_FreeColor = ds.Tables("Config").Rows(0)("FreeColor").ToString
_OnlineColor = ds.Tables("Config").Rows(0)("OnlineColor").ToString
_BookColor = ds.Tables("Config").Rows(0)("BookColor").ToString
_BoxMargin = ds.Tables("Config").Rows(0)("Margin").ToString
_BoxWidth = ds.Tables("Config").Rows(0)("Width").ToString
_BoxHeight = ds.Tables("Config").Rows(0)("Height").ToString
Dim TitleTextStye As FontStyle
If (TitleTextFontBold = "True") Then
TitleTextStye = TitleTextStye Or FontStyle.Bold
End If
If (TitleTextFontItalic = "True") Then
TitleTextStye = TitleTextStye Or FontStyle.Italic
End If
If (TitleTextFontStrikeout = "True") Then
TitleTextStye = TitleTextStye Or FontStyle.Strikeout
End If
If (TitleTextFontUnderline = "True") Then
TitleTextStye = TitleTextStye Or FontStyle.Underline
End If
Dim InfoTextStye As FontStyle
If (InfoTextFontBold = "True") Then
InfoTextStye = InfoTextStye Or FontStyle.Bold
End If
If (InfoTextFontItalic = "True") Then
InfoTextStye = InfoTextStye Or FontStyle.Italic
End If
If (InfoTextFontStrikeout = "True") Then
InfoTextStye = InfoTextStye Or FontStyle.Strikeout
End If
If (InfoTextFontUnderline = "True") Then
InfoTextStye = InfoTextStye Or FontStyle.Underline
End If
_TitleTextFont = New Font(TitleTextFontName, TitleTextFontSize, TitleTextStye, GraphicsUnit.Point)
_InfoTextFont = New Font(InfoTextFontName, InfoTextFontSize, InfoTextStye, GraphicsUnit.Point)
Catch ex As Exception
MsgBox(ex.ToString, 0 + 16, "读取配置")
End Try
End Sub
'添加一个方格项目
Public Sub Add(ByVal Title As String, _
ByVal Info As String, _
ByVal Status As String, _
ByVal BillCode As String)
Try
Dim _BoxAttribute As New Box.BoxAttribute
With _BoxAttribute
.Title = Title
.Info = Info
.BoxHeight = _BoxHeight
.BoxWidth = _BoxWidth
.BoxMargin = _BoxMargin
.Status = Status
.BillCode = BillCode
.TitleTextColor = _TitleTextColor
.TitleTextFont = _TitleTextFont
.InfoTextFont = _InfoTextFont
.InfoTextColor = _InfoTextColor
Select Case Status
Case "空闲"
.BoxBakColor = _FreeColor
Case "使用"
.BoxBakColor = _OnlineColor
Case "预订"
.BoxBakColor = _BookColor
End Select
End With
Dim TemplateBox As New Box.Box(_BoxAttribute)
FlowLayoutPanel1.Controls.Add(TemplateBox)
Catch ex As Exception
MsgBox(ex.ToString, 0 + 16, "添加一个方格项目")
End Try
End Sub
End Class- 已建议为答案 ThankfulHeartModerator 2012年2月21日 1:24
- 已标记为答案 Mike FengModerator 2012年2月21日 8:16
全部回复
-
没有直接绑定到WinForm的控件能够像WebForm一样呈现此种效果。建议你动态生成:
1)设置WinForm的AutoScroll=True。
2)然后动态使用TableLayout进行布局——
Namespace WinFormCSharp Public Partial Class Form1 Inherits Form Public Sub New() InitializeComponent() End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Dim tpanel As New TableLayoutPanel() tpanel.Dock = DockStyle.Fill tpanel.Parent = Me tpanel.RowStyles.Clear() tpanel.ColumnStyles.Clear() tpanel.ColumnCount = 5 tpanel.RowCount = 5 '绘制行和列 For i As Integer = 1 To 5 tpanel.RowStyles.Add(New RowStyle(SizeType.Percent, 20F)) Next For i As Integer = 1 To 5 tpanel.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 20F)) Next '放控件 For row As Integer = 0 To 4 For col As Integer = 0 To 4 Dim lb As New Label() lb.Text = "Label" & ((row * 5) + (col + 1)) tpanel.Controls.Add(lb, col, row) lb.Dock = DockStyle.Fill lb.Font = New Font("黑体", 40.5F) Next Next End Sub End Class
End Namespace
【示例图】
QQ我:
下载MSDN桌面工具(Vista,Win7)
我的博客园
慈善点击,点击此处
- 已编辑 ThankfulHeartModerator 2012年2月6日 8:05
- 已建议为答案 Mike FengModerator 2012年2月8日 10:19
- 已标记为答案 Mike FengModerator 2012年2月21日 8:15
-
或者建议楼主也可以使用DataRepeater(Visual Studio Service Packs)工具,拖拽一个控件到WinForm,然后放上4个Label,然后循环执行,示例代码(label距离计算忽略)——
【代码】
Namespace WinFormCSharp Public Partial Class Form1 Inherits Form Private i As Integer = 1 Public Sub New() InitializeComponent() End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) dataRepeater1.DataSource = New Integer() {1, 2, 3, 4, 5} End Sub Private Sub dataRepeater1_ItemCloning(sender As Object, e As Microsoft.VisualBasic.PowerPacks.DataRepeaterItemCloneEventArgs) For Each item As var In e.Source.Controls TryCast(item, Label).Text = (System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)).ToString() Next End Sub End Class End Namespace
【设计界面】
【运行效果】
QQ我:
下载MSDN桌面工具(Vista,Win7)
我的博客园
慈善点击,点击此处
- 已编辑 ThankfulHeartModerator 2012年2月6日 8:06
- 已建议为答案 Mike FengModerator 2012年2月8日 10:19
- 已标记为答案 Mike FengModerator 2012年2月21日 8:15
-
谢谢。此问题已解决。写一个自定义控件。然后继承FlowLayoutPanel.问题就变的十分简单了。
- 已标记为答案 feiyun0112Moderator 2012年2月20日 6:23
-
图:
定义两个类。这个是第一个类。
Public Class Box
Private _Title As String
Private _Info As String
Private _Status As String
Private _BillCode As String
Public Sub New()' 此调用是设计器所必需的。
InitializeComponent()' 在 InitializeComponent() 调用之后添加任何初始化。
End Sub
Public Sub New(ByVal _BoxAttribute As BoxAttribute)
InitializeComponent()
With _BoxAttribute
_Title = .Title
_Info = .Info
_Status = .Status
_BillCode = .BillCode
Label_Title.Text = _Title
Label_Title.Font = .TitleTextFont
Label_Title.ForeColor = Color.FromArgb(.TitleTextColor)
Label_Info.Text = _Info
Label_Info.Font = .InfoTextFont
Label_Info.ForeColor = Color.FromArgb(.InfoTextColor)
Me.BackColor = Color.FromArgb(.BoxBakColor)
Me.Width = .BoxWidth
Me.Height = .BoxHeight
Me.Height = .BoxHeight
Me.Margin = New Padding(.BoxMargin)
End With
End Sub
Private Sub UserControl_Box_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.DoubleClick
MsgBox(_Status)
End Sub
End Class第二个类:
Public Class StateGrid
Private _TitleTextColor As Integer
Private _TitleTextFont As Font
Private _InfoTextColor As Integer
Private _InfoTextFont As Font
Private _FreeColor As Integer
Private _OnlineColor As Integer
Private _BookColor As Integer
Private _BoxMargin As Integer
Private _BoxWidth As Integer
Private _BoxHeight As Integer
Public Sub New()' 此调用是设计器所必需的。
InitializeComponent()
ReadConfig()
' 在 InitializeComponent() 调用之后添加任何初始化。End Sub
'读取配置
Private Sub ReadConfig()
Try
Dim strConfig As String = Application.StartupPath + "\PreferencesConfig.xml"
If System.IO.File.Exists(strConfig) = False Then
Return
End If
Dim ds As New DataSet
ds.ReadXml(strConfig)
_TitleTextColor = ds.Tables("Config").Rows(0)("TitleTextColor").ToString
Dim TitleTextFontName As String = _
ds.Tables("Config").Rows(0)("TitleTextFontName").ToString
Dim TitleTextFontSize As String = _
ds.Tables("Config").Rows(0)("TitleTextFontSize").ToString
Dim TitleTextFontBold As String = _
ds.Tables("Config").Rows(0)("TitleTextFontBold").ToString
Dim TitleTextFontItalic As String = _
ds.Tables("Config").Rows(0)("TitleTextFontItalic").ToString
Dim TitleTextFontStrikeout As String = _
ds.Tables("Config").Rows(0)("TitleTextFontStrikeout").ToString
Dim TitleTextFontUnderline As String = _
ds.Tables("Config").Rows(0)("TitleTextFontUnderline").ToString
Dim InfoTextFontName As String = _
ds.Tables("Config").Rows(0)("InfoTextFontName").ToString
Dim InfoTextFontSize As String = _
ds.Tables("Config").Rows(0)("InfoTextFontSize").ToString
Dim InfoTextFontBold As String = _
ds.Tables("Config").Rows(0)("InfoTextFontBold").ToString
Dim InfoTextFontItalic As String = _
ds.Tables("Config").Rows(0)("InfoTextFontItalic").ToString
Dim InfoTextFontStrikeout As String = _
ds.Tables("Config").Rows(0)("InfoTextFontStrikeout").ToString
Dim InfoTextFontUnderline As String = _
ds.Tables("Config").Rows(0)("InfoTextFontUnderline").ToString
_InfoTextColor = ds.Tables("Config").Rows(0)("InfoTextColor").ToString
_FreeColor = ds.Tables("Config").Rows(0)("FreeColor").ToString
_OnlineColor = ds.Tables("Config").Rows(0)("OnlineColor").ToString
_BookColor = ds.Tables("Config").Rows(0)("BookColor").ToString
_BoxMargin = ds.Tables("Config").Rows(0)("Margin").ToString
_BoxWidth = ds.Tables("Config").Rows(0)("Width").ToString
_BoxHeight = ds.Tables("Config").Rows(0)("Height").ToString
Dim TitleTextStye As FontStyle
If (TitleTextFontBold = "True") Then
TitleTextStye = TitleTextStye Or FontStyle.Bold
End If
If (TitleTextFontItalic = "True") Then
TitleTextStye = TitleTextStye Or FontStyle.Italic
End If
If (TitleTextFontStrikeout = "True") Then
TitleTextStye = TitleTextStye Or FontStyle.Strikeout
End If
If (TitleTextFontUnderline = "True") Then
TitleTextStye = TitleTextStye Or FontStyle.Underline
End If
Dim InfoTextStye As FontStyle
If (InfoTextFontBold = "True") Then
InfoTextStye = InfoTextStye Or FontStyle.Bold
End If
If (InfoTextFontItalic = "True") Then
InfoTextStye = InfoTextStye Or FontStyle.Italic
End If
If (InfoTextFontStrikeout = "True") Then
InfoTextStye = InfoTextStye Or FontStyle.Strikeout
End If
If (InfoTextFontUnderline = "True") Then
InfoTextStye = InfoTextStye Or FontStyle.Underline
End If
_TitleTextFont = New Font(TitleTextFontName, TitleTextFontSize, TitleTextStye, GraphicsUnit.Point)
_InfoTextFont = New Font(InfoTextFontName, InfoTextFontSize, InfoTextStye, GraphicsUnit.Point)
Catch ex As Exception
MsgBox(ex.ToString, 0 + 16, "读取配置")
End Try
End Sub
'添加一个方格项目
Public Sub Add(ByVal Title As String, _
ByVal Info As String, _
ByVal Status As String, _
ByVal BillCode As String)
Try
Dim _BoxAttribute As New Box.BoxAttribute
With _BoxAttribute
.Title = Title
.Info = Info
.BoxHeight = _BoxHeight
.BoxWidth = _BoxWidth
.BoxMargin = _BoxMargin
.Status = Status
.BillCode = BillCode
.TitleTextColor = _TitleTextColor
.TitleTextFont = _TitleTextFont
.InfoTextFont = _InfoTextFont
.InfoTextColor = _InfoTextColor
Select Case Status
Case "空闲"
.BoxBakColor = _FreeColor
Case "使用"
.BoxBakColor = _OnlineColor
Case "预订"
.BoxBakColor = _BookColor
End Select
End With
Dim TemplateBox As New Box.Box(_BoxAttribute)
FlowLayoutPanel1.Controls.Add(TemplateBox)
Catch ex As Exception
MsgBox(ex.ToString, 0 + 16, "添加一个方格项目")
End Try
End Sub
End Class- 已建议为答案 ThankfulHeartModerator 2012年2月21日 1:24
- 已标记为答案 Mike FengModerator 2012年2月21日 8:16