For 循环遍历 Windows 窗体中的所有选中列表框 [英] For loop to iterate through all checkedlistboxes in a windows form
问题描述
我已经为我的问题找到了解决方案,但处理时间太长.我想以某种方式获得一些帮助,以便我可以遍历表单中的所有 10 个检查列表框,以便每个框都填充我的数据网格视图的一列.这是我的代码
I have already a solution for my problem but it takes too long to process. I would like to get some help in a way that i can loop through all the 10 checkedlistboxes i have in my form in order to each one fill a column of my datagridview. This is the code i have
第一块代码:
Dim dt As New DataTable
dt.Columns.Add("Região", Type.GetType("System.String"))
dt.Columns.Add("Produto", Type.GetType("System.String"))
dt.Columns.Add("Cliente", Type.GetType("System.String"))
dt.Columns.Add("Tipo Atividade", Type.GetType("System.String"))
dt.Columns.Add("Acabamento", Type.GetType("System.String"))
dt.Columns.Add("Cores", Type.GetType("System.String"))
dt.Columns.Add("Gramagem", Type.GetType("System.String"))
dt.Columns.Add("Tipo Embalagem", Type.GetType("System.String"))
dt.Columns.Add("Formatos", Type.GetType("System.String"))
dt.Columns.Add("Contagem", Type.GetType("System.String"))
dt.Columns.Add("Transporte", Type.GetType("System.String"))
dt.Columns.Add("Moeda", Type.GetType("System.String"))
dt.Columns.Add("Preço", Type.GetType("System.String"))
dt.Columns.Add("Data Inicio", Type.GetType("System.String"))
dt.Columns.Add("Data Fim", Type.GetType("System.String"))
dt.Columns.Add("Standard", GetType(Boolean))
Dim dataInicio, dataFim As String
dataInicio = DateTimeInicio.Value.ToString("dd-MM-yyyy")
dataFim = DateTimeFim.Value.ToString("dd-MM-yyyy")
Dim a, b, c, d, f, g, h, i, j, k As Integer
Dim dr As DataRow = dt.NewRow
第二块代码:
For a = 0 To ChkListRegiao.Items.Count - 1
For b = 0 To ChkListProduto.Items.Count - 1
For c = 0 To ChkListTipoAtividade.Items.Count - 1
For d = 0 To ChkListAcabamento.Items.Count - 1
For f = 0 To ChkListCores.Items.Count - 1
For g = 0 To ChkListGramagem.Items.Count - 1
For h = 0 To ChkListTipoEmbalagem.Items.Count - 1
For i = 0 To ChkListFormatos.Items.Count - 1
For j = 0 To ChkListContagem.Items.Count - 1
For k = 0 To ChkListTransporte.Items.Count - 1
dr("Região") = ChkListRegiao.GetItemChecked(a)
dr("Produto") = ChkListProduto.GetItemChecked(b)
dr("Cliente") = ""
dr("Tipo Atividade") = ChkListTipoAtividade.GetItemChecked(c)
dr("Acabamento") = ChkListAcabamento.GetItemChecked(d)
dr("Cores") = ChkListCores.GetItemChecked(f)
dr("Gramagem") = ChkListGramagem.GetItemChecked(g)
dr("Tipo Embalagem") = ChkListTipoEmbalagem.GetItemChecked(h)
dr("Formatos") = ChkListFormatos.GetItemChecked(i)
dr("Contagem") = ChkListContagem.GetItemChecked(j)
dr("Transporte") = ChkListTransporte.GetItemChecked(k)
dr("Moeda") = txtMoeda.Text
dr("Preço") = txtPrecoTon.Text
dr("Data Inicio") = dataInicio
dr("Data Fim") = dataFim
dr("Standard") = chkStandard.Checked
If ChkListRegiao.GetItemChecked(a) And ChkListProduto.GetItemChecked(b) And ChkListTipoAtividade.GetItemChecked(c) And
ChkListAcabamento.GetItemChecked(d) And ChkListCores.GetItemChecked(f) And ChkListGramagem.GetItemChecked(g) And
ChkListTipoEmbalagem.GetItemChecked(h) And ChkListFormatos.GetItemChecked(i) And ChkListContagem.GetItemChecked(j) And
ChkListTransporte.GetItemChecked(k) Then
dt.Rows.Add(ChkListRegiao.Items(a), ChkListProduto.Items(b), "", ChkListTipoAtividade.Items(c), ChkListAcabamento.Items(d), ChkListCores.Items(f),
ChkListGramagem.Items(g), ChkListTipoEmbalagem.Items(h), ChkListFormatos.Items(i),
ChkListContagem.Items(j), ChkListTransporte.Items(k), txtMoeda.Text, txtPrecoTon.Text, dataInicio, dataFim, chkStandard.Checked)
DataGridView1.DataSource = dt
End If
Next
Next
Next
Next
Next
Next
Next
Next
Next
Next
推荐答案
走这条线:
DataGridView1.DataSource = dt
并在所有For
循环结束后移动它.这不是唯一让你慢下来的事情,但我怀疑这是最大的......每次更改后都试图重建网格.
And move it after all of the For
loops end. This is not the only thing that slows you down, but I suspect it is the biggest... trying to rebuild the grid after every change.
此外,您可以重构循环以在调用其他级别之前在每个级别进行过滤,这样可以节省相当多的工作.例如,假设您只有 3 个列表,每个列表都选择了 10 个项目中的 5 个.原始代码将运行整个 if()
条件 1000 次,每次进行 3 次布尔比较,以便仅产生 125 个结果.通过在下降到下一个级别之前在每个级别进行过滤,您可以最大限度地减少失败检查的数量.如果您不预先缓存选择,则相同的 3 个列表只需要 310 次布尔检查即可产生相同的 125 个结果,或者如果您这样做,则需要 30 次布尔检查 + 一点额外的内存:
Additionally, you can restructure the loops to filter at each level before calling into the other levels, and save quite a bit of work this way. For example, say you only have 3 lists, each with 5 out of 10 items selected. The original code would run the entire if()
conditions 1000 times, each with 3 boolean comparisons, in order to produce only 125 results. By filtering at each level before descending into the next, you minimize the number of failed checks. The same 3 lists would only require 310 boolean checks to produce the same 125 results if you don't pre-cache selections, or 30 boolean checks + a little extra memory if you do:
Dim dt As New DataTable
dt.Columns.Add("Região", Type.GetType("System.String"))
dt.Columns.Add("Produto", Type.GetType("System.String"))
dt.Columns.Add("Cliente", Type.GetType("System.String"))
dt.Columns.Add("Tipo Atividade", Type.GetType("System.String"))
dt.Columns.Add("Acabamento", Type.GetType("System.String"))
dt.Columns.Add("Cores", Type.GetType("System.String"))
dt.Columns.Add("Gramagem", Type.GetType("System.String"))
dt.Columns.Add("Tipo Embalagem", Type.GetType("System.String"))
dt.Columns.Add("Formatos", Type.GetType("System.String"))
dt.Columns.Add("Contagem", Type.GetType("System.String"))
dt.Columns.Add("Transporte", Type.GetType("System.String"))
dt.Columns.Add("Moeda", Type.GetType("System.String"))
dt.Columns.Add("Preço", Type.GetType("System.String"))
dt.Columns.Add("Data Inicio", Type.GetType("System.String"))
dt.Columns.Add("Data Fim", Type.GetType("System.String"))
dt.Columns.Add("Standard", GetType(Boolean))
Dim dataInicio As String = DateTimeInicio.Value.ToString("dd-MM-yyyy")
Dim dataFim As String = DateTimeFim.Value.ToString("dd-MM-yyyy")
'Uncached:
For Each a In ChkListRegiao.Items.Cast(Of ListItem)().Where(Function(li) li.Selected)
For Each b In ChkListProduto.Items.Cast(Of ListItem)().Where(Function(li) li.Selected)
For Each c In ChkListTipoAtividade.Items.Cast(Of ListItem)().Where(Function(li) li.Selected)
For Each d In ChkListAcabamento.Items.Cast(Of ListItem)().Where(Function(li) li.Selected)
For Each f In ChkListCores.Items.Cast(Of ListItem)().Where(Function(li) li.Selected)
For Each g In ChkListGramagem.Items.Cast(Of ListItem)().Where(Function(li) li.Selected)
For Each h In ChkListTipoEmbalagem.Items.Cast(Of ListItem)().Where(Function(li) li.Selected)
For Each i In ChkListFormatos.Items.Cast(Of ListItem)().Where(Function(li) li.Selected)
For Each j In ChkListContagem.Items.Cast(Of ListItem)().Where(Function(li) li.Selected)
For Each k In ChkListTransporte.Items.Cast(Of ListItem)().Where(Function(li) li.Selected)
dt.Rows.Add(a, b, "", c, d, f, g, h, i, j, k, txtMoeda.Text, txtPrecoTon.Text, dataInicio, dataFim, chkStandard.Checked)
Next
Next
Next
Next
Next
Next
Next
Next
Next
Next
DataGridView1.DataSource = dt
'Cached:
Dim regiao = ChkListRegiao.Items.Cast(Of ListItem)().Where(Function(li) li.Selected).ToList()
Dim produto = ChkListProduto.Items.Cast(Of ListItem)().Where(Function(li) li.Selected).ToList()
Dim tipoatividade = ChkListTipoAtividade.Items.Cast(Of ListItem)().Where(Function(li) li.Selected).ToList()
Dim acabamento = ChkListAcabamento.Items.Cast(Of ListItem)().Where(Function(li) li.Selected).ToList()
Dim cores = ChkListCores.Items.Cast(Of ListItem)().Where(Function(li) li.Selected).ToList()
Dim gramagem = ChkListGramagem.Items.Cast(Of ListItem)().Where(Function(li) li.Selected).ToList()
Dim tipoembalagem = ChkListTipoEmbalagem.Items.Cast(Of ListItem)().Where(Function(li) li.Selected).ToList()
Dim formatos = ChkListFormatos.Items.Cast(Of ListItem)().Where(Function(li) li.Selected).ToList()
Dim contagem = ChkListContagem.Items.Cast(Of ListItem)().Where(Function(li) li.Selected).ToList()
Dim transporte = ChkListTransporte.Items.Cast(Of ListItem)().Where(Function(li) li.Selected).ToList()
For Each a In regiao
For Each b In produto
For Each c In tipoatividade
For Each d In acabamento
For Each f In cores
For Each g In gramagem
For Each h In tipoembalagem
For Each i In formatos
For Each j In contagem
For Each k In transporte
dt.Rows.Add(a, b, "", c, d, f, g, h, i, j, k, txtMoeda.Text, txtPrecoTon.Text, dataInicio, dataFim, chkStandard.Checked)
Next
Next
Next
Next
Next
Next
Next
Next
Next
Next
DataGridView1.DataSource = dt
您需要同时尝试这两种方法,看看哪个更适合您的环境.
You'll want to try both to see which is faster for your environment.
但这似乎是错误的.您真的想要从所有列表中选择所有可能的项目组合吗?
But even this seems wrong. Do you really want every possible combination of items selected from all the lists?
这篇关于For 循环遍历 Windows 窗体中的所有选中列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!