C# 列表框中的多列数据
我想在我的列表框中有多列。下面是我在应用程序中得到的图片示例 我实际上有7个专栏,但为了便于理解,我只打印了两个专栏 因此,第一列将显示C# 列表框中的多列数据,c#,.net,winforms,listbox,multiple-columns,C#,.net,Winforms,Listbox,Multiple Columns,我想在我的列表框中有多列。下面是我在应用程序中得到的图片示例 我实际上有7个专栏,但为了便于理解,我只打印了两个专栏 因此,第一列将显示date,第二列将显示name。正如您所看到的,数据没有进入它们自己的列中 这是我的代码: this.listBox1 = new System.Windows.Forms.ListBox(); this.SuspendLayout(); // // listBox1 // this.listBox1.FormattingEnabled = true; t
date
,第二列将显示name
。正如您所看到的,数据没有进入它们自己的列中
这是我的代码:
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.HorizontalScrollbar = true;
foreach (XmlNode xn in xnList)
{
string date = xn.OfType<XmlNode>().FirstOrDefault(n => n.Name == "Date").FirstChild.Value;
string id = xn.OfType<XmlNode>().FirstOrDefault(n => n.Name == "ID").FirstChild.Value;
if (date == cari)
{
this.listBox1.Items.AddRange(new object[] {
//dateBox.Text,
dateBox.Text + "\r\n" + date});
this.listBox1.Items.AddRange(new object[] {
"sarabrown"});
}
}
this.listBox1.Location = new System.Drawing.Point(12, 28);
this.listBox1.MultiColumn = true;
this.listBox1.Name = "listBox1";
this.listBox1.ScrollAlwaysVisible = true;
this.listBox1.Size = new System.Drawing.Size(300, 95);
this.listBox1.TabIndex = 0;
this.listBox1.ColumnWidth = 100;
//
// Form3
//
this.ClientSize = new System.Drawing.Size(400, 273);
this.Controls.Add(this.listBox1);
this.Name = "Form3";
this.ResumeLayout(false);
this.listBox1=new System.Windows.Forms.ListBox();
这个.SuspendLayout();
//
//列表框1
//
this.listBox1.FormattingEnabled=true;
this.listBox1.HorizontalScrollbar=true;
foreach(xnList中的XmlNode xn)
{
字符串date=xn.OfType().FirstOrDefault(n=>n.Name==“date”).FirstChild.Value;
字符串id=xn.OfType().FirstOrDefault(n=>n.Name==“id”).FirstChild.Value;
如果(日期=cari)
{
this.listBox1.Items.AddRange(新对象[]{
//dateBox.Text,
dateBox.Text+“\r\n”+日期});
this.listBox1.Items.AddRange(新对象[]{
“萨拉布朗”});
}
}
this.listBox1.Location=新系统.图纸.点(12,28);
this.listBox1.MultiColumn=true;
this.listBox1.Name=“listBox1”;
this.listBox1.scrollwaysvisible=true;
this.listBox1.Size=新系统图尺寸(300,95);
this.listBox1.TabIndex=0;
this.listBox1.ColumnWidth=100;
//
//表格3
//
this.ClientSize=新系统.Drawing.Size(400273);
this.Controls.Add(this.listBox1);
this.Name=“Form3”;
此选项为.resume布局(false);
我找到了这段代码,但它创建了一个列表框,看起来就像上面的图片一样。有人知道吗?列表框的
MultiColumn
属性只会帮助避免垂直滚动,因此只会将溢出的项目堆叠到下一列中。默认情况下,您的要求在.NET中不可用。因此,您可能必须构建自己的自定义控件来支持它
顺便说一句,GridView
是你的朋友。。使用GridView
可以轻松实现您需要的功能。
例如,为了使它简单化(您可能需要完全调整它以适应您的问题)
更新:
您可能希望签出ListView
,而不是GridView
或ListBox
。它比GridView
相对轻量级
使用ListView
还可以在不同的列中放入其他控件,如复选框
查看一下,给你一个想法。或者这个更简单的: 对于C#中ListBox中的多列,这工作正常
listBox1.Items.AddRange(
new object[]
{
"Name","Aman"
}
);
我认为您误解了ListBox中多列属性的用法 多列列表框将项目放入所需的任意多个列中,以避免垂直滚动 所以这只是为了避免滚动
如果您想显示多个列,每个列上都有单独的数据,我建议您使用。我已经能够创建一个代码,以便我可以从Excel中的电子表格中对任何年份范围的日期进行排序
Private Sub UserForm_Initialize()
With Me
.StartUpPosition = 0
'.Width = Application.Width * 0.46
'.Height = Application.Height * 0.57
.Left = Application.Left + (Application.Width * 0.7) \ 2
.Top = Application.Top + (Application.Height * 0.3) \ 2
End With
End Sub
Private Sub CommandButton1_Click()
initialize ("lth") 'Call rutine "initialize" to order listbox from down to up
End Sub
Private Sub CommandButton2_Click()
initialize ("htl") 'Call rutine "initialize" to order listbox from up to down
End Sub
Sub initialize(ByVal ordertype As String)
Dim x As Integer
Dim i As Integer
Dim fechas As Date
Dim datofecha As String
Dim arrayyear() As Date
Dim diasmesbisiesto(1 To 12) As Integer
Dim diasmesnobisiesto(1 To 12) As Integer
Dim lastrow As Integer
Dim mayoryear As Integer
Dim menoryear As Integer
Dim f As Integer
Dim c As Date
lastrow = Worksheets(1).Range("A" & Rows.Count).End(xlUp).Row
ReDim arrayyear(lastrow)
For u = 1 To lastrow
arrayyear(u) = Format(Sheet1.Cells(u, 1).Value, "dd/mm/yyyy")
Next u
'-------------------------
'Obtener mayor valor fecha
'-------------------------
Dim vm As Integer
Dim maxindex As Integer
maxindex = 1
For vm = 2 To UBound(arrayyear) 'suponemos indice desde 1 a n
If arrayyear(vm) > arrayyear(maxindex) Then
maxindex = vm
End If
Next
mayor = arrayyear(maxindex)
'-------------------------
'-------------------------
'Obtener menor valor fecha
'-------------------------
Dim vn As Integer
Dim minindex As Integer
minindex = 1
For vn = 2 To UBound(arrayyear) 'suponemos indice desde 1 a n
If arrayyear(minindex) < arrayyear(vn) Then
Else
minindex = vn
End If
Next
menor = arrayyear(minindex)
'-------------------------
menoryear = Year(menor) ' Asign the lowest year in menoryear
mayoryear = Year(mayor) ' Asign the highest year in mayoryear
ListBox1.Clear
'Carga vector diasmesbisiesto
'---------------------------
diasmesbisiesto(1) = 31
diasmesbisiesto(2) = 29
diasmesbisiesto(3) = 31
diasmesbisiesto(4) = 30
diasmesbisiesto(5) = 31
diasmesbisiesto(6) = 30
diasmesbisiesto(7) = 31
diasmesbisiesto(8) = 31
diasmesbisiesto(9) = 30
diasmesbisiesto(10) = 31
diasmesbisiesto(11) = 30
diasmesbisiesto(12) = 31
'---------------------------
'Carga vector diasmesnobisiesto
'---------------------------
diasmesnobisiesto(1) = 31
diasmesnobisiesto(2) = 28
diasmesnobisiesto(3) = 31
diasmesnobisiesto(4) = 30
diasmesnobisiesto(5) = 31
diasmesnobisiesto(6) = 30
diasmesnobisiesto(7) = 31
diasmesnobisiesto(8) = 31
diasmesnobisiesto(9) = 30
diasmesnobisiesto(10) = 31
diasmesnobisiesto(11) = 30
diasmesnobisiesto(12) = 31
'---------------------------
f = 0 'Variable para ubicar la ubicacion de la fila en donde se guardaran los datos en el listbox
cuenta = 0 ' Variable para contar la cantidad de elementos que se cargaron en el listbox
If ordertype = "lth" Then
GoTo 1 ' Ordering lowest to highest
End If
If ordertype = "htl" Then
GoTo 2 ' Ordering highest to lowest
End If
'---------------------------
' Ordering lowest to highest
'---------------------------
1:
For anio = menoryear To mayoryear
For mes = 1 To 12
If (anio Mod 4 = 0 And anio Mod 100 <> 0 Or anio Mod 400 = 0) Then
' Años bisiestos
diasmes = diasmesbisiesto(mes)
Else
' Años no bisiestos
diasmes = diasmesnobisiesto(mes)
End If
datofecha = Format(DateSerial(anio, mes, 1), "dd/mm/yyyy")
fechas = datofecha
'Carga del listbox
'------------------------------------------------------------------------------------------------------------
For x = 0 To diasmes - 1
For i = 1 To lastrow
c = fechas
If Format(Sheet1.Cells(i, 1), "dd/mm/yyyy") = c + x Then
Me.ListBox1.AddItem
Me.ListBox1.List(f, 0) = Format(Sheet1.Cells(i, 1), "dd/m/yyyy")
cuenta = cuenta + 1
For b = 1 To 1
Me.ListBox1.List(f, b) = Sheet1.Cells(i, b + 1)
Next b
f = f + 1
End If
Next i
Next x
'------------------------------------------------------------------------------------------------------------
Next mes
Next anio
Label2.Caption = "Number of Elements: " & cuenta
Label3.Caption = "Order from Lowest to Highest "
Exit Sub
'---------------------------
' Ordering highest to lowest
'---------------------------
2:
For anio = mayoryear To menoryear Step -1
For mes = 12 To 1 Step -1
If (anio Mod 4 = 0 And anio Mod 100 <> 0 Or anio Mod 400 = 0) Then
' Años bisiestos
diasmes = diasmesbisiesto(mes)
Else
' Años no bisiestos
diasmes = diasmesnobisiesto(mes)
End If
datofecha = Format(DateSerial(anio, mes, diasmes), "dd/mm/yyyy")
fechas = datofecha
'Carga del listbox
'------------------------------------------------------------------------------------------------------------
For x = 0 To diasmes - 1
For i = 1 To lastrow
c = fechas
If Format(Sheet1.Cells(i, 1), "dd/mm/yyyy") = c - x Then
Me.ListBox1.AddItem
Me.ListBox1.List(f, 0) = Format(Sheet1.Cells(i, 1), "dd/m/yyyy")
cuenta = cuenta + 1
For b = 1 To 1
Me.ListBox1.List(f, b) = Sheet1.Cells(i, b + 1)
Next b
f = f + 1
End If
Next i
Next x
'------------------------------------------------------------------------------------------------------------
Next mes
Next anio
Label2.Caption = "Number of Elements: " & cuenta
Label3.Caption = "Order from Highest to Lowest "
Exit Sub
End Sub
Private子用户表单_Initialize()
和我一起
.StartUpPosition=0
'.Width=Application.Width*0.46
'.Height=Application.Height*0.57
.Left=Application.Left+(Application.Width*0.7)\2
.Top=Application.Top+(Application.Height*0.3)\2
以
端接头
私有子命令按钮1_单击()
initialize(“lth”)调用rutine“initialize”命令列表框从下到上
端接头
私有子命令按钮2_单击()
initialize(“htl”)”调用rutine“initialize”从上到下订购列表框
端接头
子初始化(ByVal ordertype作为字符串)
作为整数的Dim x
作为整数的Dim i
这是我的生日
作为字符串的Dim datofecha
Dim arrayyear()作为日期
Dim diasmesbisiesto(1到12)作为整数
Dim DISAMESNOBISESTO(1到12)作为整数
将最后一行设置为整数
Dim mayoryear作为整数
Dim menoryear作为整数
作为整数的Dim f
尺寸c作为日期
lastrow=工作表(1).Range(“A”和Rows.Count).End(xlUp).Row
ReDim阵列EAR(最后一行)
对于u=1到最后一行
arrayyear(u)=格式(Sheet1.单元格(u,1).值,“dd/mm/yyyy”)
下一个美国
'-------------------------
“奥本纳市长瓦洛·费查
'-------------------------
将vm设置为整数
Dim maxindex作为整数
maxindex=1
对于vm=2至UBound(arrayyear)‘suponemos指示1 a n
如果arrayyear(vm)>arrayyear(maxindex),则
maxindex=vm
如果结束
下一个
mayor=arrayyear(最大索引)
'-------------------------
'-------------------------
“奥普滕·门诺·瓦勒·费查
'-------------------------
作为整数的Dim vn
作为整数的Dim minindex
minindex=1
对于vn=2至UBound(阵列)的“suponemos指示1 a n
如果arrayyear(minindex)Private Sub UserForm_Initialize()
With Me
.StartUpPosition = 0
'.Width = Application.Width * 0.46
'.Height = Application.Height * 0.57
.Left = Application.Left + (Application.Width * 0.7) \ 2
.Top = Application.Top + (Application.Height * 0.3) \ 2
End With
End Sub
Private Sub CommandButton1_Click()
initialize ("lth") 'Call rutine "initialize" to order listbox from down to up
End Sub
Private Sub CommandButton2_Click()
initialize ("htl") 'Call rutine "initialize" to order listbox from up to down
End Sub
Sub initialize(ByVal ordertype As String)
Dim x As Integer
Dim i As Integer
Dim fechas As Date
Dim datofecha As String
Dim arrayyear() As Date
Dim diasmesbisiesto(1 To 12) As Integer
Dim diasmesnobisiesto(1 To 12) As Integer
Dim lastrow As Integer
Dim mayoryear As Integer
Dim menoryear As Integer
Dim f As Integer
Dim c As Date
lastrow = Worksheets(1).Range("A" & Rows.Count).End(xlUp).Row
ReDim arrayyear(lastrow)
For u = 1 To lastrow
arrayyear(u) = Format(Sheet1.Cells(u, 1).Value, "dd/mm/yyyy")
Next u
'-------------------------
'Obtener mayor valor fecha
'-------------------------
Dim vm As Integer
Dim maxindex As Integer
maxindex = 1
For vm = 2 To UBound(arrayyear) 'suponemos indice desde 1 a n
If arrayyear(vm) > arrayyear(maxindex) Then
maxindex = vm
End If
Next
mayor = arrayyear(maxindex)
'-------------------------
'-------------------------
'Obtener menor valor fecha
'-------------------------
Dim vn As Integer
Dim minindex As Integer
minindex = 1
For vn = 2 To UBound(arrayyear) 'suponemos indice desde 1 a n
If arrayyear(minindex) < arrayyear(vn) Then
Else
minindex = vn
End If
Next
menor = arrayyear(minindex)
'-------------------------
menoryear = Year(menor) ' Asign the lowest year in menoryear
mayoryear = Year(mayor) ' Asign the highest year in mayoryear
ListBox1.Clear
'Carga vector diasmesbisiesto
'---------------------------
diasmesbisiesto(1) = 31
diasmesbisiesto(2) = 29
diasmesbisiesto(3) = 31
diasmesbisiesto(4) = 30
diasmesbisiesto(5) = 31
diasmesbisiesto(6) = 30
diasmesbisiesto(7) = 31
diasmesbisiesto(8) = 31
diasmesbisiesto(9) = 30
diasmesbisiesto(10) = 31
diasmesbisiesto(11) = 30
diasmesbisiesto(12) = 31
'---------------------------
'Carga vector diasmesnobisiesto
'---------------------------
diasmesnobisiesto(1) = 31
diasmesnobisiesto(2) = 28
diasmesnobisiesto(3) = 31
diasmesnobisiesto(4) = 30
diasmesnobisiesto(5) = 31
diasmesnobisiesto(6) = 30
diasmesnobisiesto(7) = 31
diasmesnobisiesto(8) = 31
diasmesnobisiesto(9) = 30
diasmesnobisiesto(10) = 31
diasmesnobisiesto(11) = 30
diasmesnobisiesto(12) = 31
'---------------------------
f = 0 'Variable para ubicar la ubicacion de la fila en donde se guardaran los datos en el listbox
cuenta = 0 ' Variable para contar la cantidad de elementos que se cargaron en el listbox
If ordertype = "lth" Then
GoTo 1 ' Ordering lowest to highest
End If
If ordertype = "htl" Then
GoTo 2 ' Ordering highest to lowest
End If
'---------------------------
' Ordering lowest to highest
'---------------------------
1:
For anio = menoryear To mayoryear
For mes = 1 To 12
If (anio Mod 4 = 0 And anio Mod 100 <> 0 Or anio Mod 400 = 0) Then
' Años bisiestos
diasmes = diasmesbisiesto(mes)
Else
' Años no bisiestos
diasmes = diasmesnobisiesto(mes)
End If
datofecha = Format(DateSerial(anio, mes, 1), "dd/mm/yyyy")
fechas = datofecha
'Carga del listbox
'------------------------------------------------------------------------------------------------------------
For x = 0 To diasmes - 1
For i = 1 To lastrow
c = fechas
If Format(Sheet1.Cells(i, 1), "dd/mm/yyyy") = c + x Then
Me.ListBox1.AddItem
Me.ListBox1.List(f, 0) = Format(Sheet1.Cells(i, 1), "dd/m/yyyy")
cuenta = cuenta + 1
For b = 1 To 1
Me.ListBox1.List(f, b) = Sheet1.Cells(i, b + 1)
Next b
f = f + 1
End If
Next i
Next x
'------------------------------------------------------------------------------------------------------------
Next mes
Next anio
Label2.Caption = "Number of Elements: " & cuenta
Label3.Caption = "Order from Lowest to Highest "
Exit Sub
'---------------------------
' Ordering highest to lowest
'---------------------------
2:
For anio = mayoryear To menoryear Step -1
For mes = 12 To 1 Step -1
If (anio Mod 4 = 0 And anio Mod 100 <> 0 Or anio Mod 400 = 0) Then
' Años bisiestos
diasmes = diasmesbisiesto(mes)
Else
' Años no bisiestos
diasmes = diasmesnobisiesto(mes)
End If
datofecha = Format(DateSerial(anio, mes, diasmes), "dd/mm/yyyy")
fechas = datofecha
'Carga del listbox
'------------------------------------------------------------------------------------------------------------
For x = 0 To diasmes - 1
For i = 1 To lastrow
c = fechas
If Format(Sheet1.Cells(i, 1), "dd/mm/yyyy") = c - x Then
Me.ListBox1.AddItem
Me.ListBox1.List(f, 0) = Format(Sheet1.Cells(i, 1), "dd/m/yyyy")
cuenta = cuenta + 1
For b = 1 To 1
Me.ListBox1.List(f, b) = Sheet1.Cells(i, b + 1)
Next b
f = f + 1
End If
Next i
Next x
'------------------------------------------------------------------------------------------------------------
Next mes
Next anio
Label2.Caption = "Number of Elements: " & cuenta
Label3.Caption = "Order from Highest to Lowest "
Exit Sub
End Sub