Usuário com melhor resposta
CheckBoxList

Pergunta
-
Respostas
-
Olá Garibaldo,
esse tipo de informação é essencial para que possamos ajudá-lo.
seguem novos códigos, testei aqui e funcionou no Windows Forms.
Crie uma classe chamada ListItem.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; public class ListItem { public string Value { get; set; } public string Text { get; set; } public bool Selected { get; set; } public ListItem(string value, string text) { this.Value = value; this.Text = text; } }
Agora no seu Form1.cs faça o seguinte:
private void Form1_Load(object sender, EventArgs e) { string strDiretorio = @"C:\Windows Service\"; string[] arArquivos = System.IO.Directory.GetFiles(strDiretorio); lblDiretorio.Text = strDiretorio; List<ListItem> lstArquivos = new List<ListItem>(); foreach (string item in arArquivos) { lstArquivos.Add(new ListItem(item.Replace(strDiretorio, ""), item)); } cblArquivos.CheckOnClick = true; ((ListBox)cblArquivos).DataSource = lstArquivos; ((ListBox)cblArquivos).DisplayMember = "Text"; ((ListBox)cblArquivos).DisplayMember = "ValueMember"; for (int i = 0; i < cblArquivos.Items.Count; i++) { ListItem obj = (ListItem)cblArquivos.Items[i]; cblArquivos.SetItemChecked(i, obj.Selected); } } private void btnCapturar_Click(object sender, EventArgs e) { lblArquivosSelecionados.Text = ""; for (int i = 0; i < cblArquivos.Items.Count; i++) { if (cblArquivos.GetItemChecked(i)) { ListItem obj = (ListItem)cblArquivos.Items[i]; lblArquivosSelecionados.Text += obj.Value + "\n"; } } }
Att, Lucio Rogerio
Espero ter ajudado, se ajudei, por favor "Vote como Útil", e se resolvi seu problema, clique em "Propor como Resposta".- Sugerido como Resposta Lucio Rogerio SPBanned terça-feira, 16 de junho de 2015 02:18
- Marcado como Resposta Matheus L. M. C. Campos terça-feira, 16 de junho de 2015 21:24
segunda-feira, 15 de junho de 2015 19:43
Todas as Respostas
-
Lucio boa tarde.
Estou desenvolvendo em C#. Quando você coloca
protected void Page_Load(object sender, EventArgs e) é a mesma coisa de
private void form_Load(object sender, EventArgs e)
protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) Não está reconhecendo IsPostBack { string strDiretorio = @"C:\Windows Service\WsNoticias\"; string[] arArquivos = System.IO.Directory.GetFiles(strDiretorio); lblDiretorio.Text = strDiretorio; foreach (string item in arArquivos) { cblArquivos.Items.Add(new ListItem(item.Replace(strDiretorio, ""), item)); } } } protected void btnCapturar_Click(object sender, EventArgs e) { lblArquivosSelecionados.Text = ""; foreach (ListItem item in cblArquivos.Items) { if (item.Selected == true) { lblArquivosSelecionados.Text += item.Value + "<br />"; } } }
O IsPostBack não está sendo reconhecido.
O ListItem também não está sendo reconhecido
O que eu tenho que fazer?
-
-
Boa noite Lúcio. Desculpe a Hora.
Olha só, a rotina que você criou foi magnifica, funcionou certinho. Agora me diz uma coisa. Eu tenho essa rotina desenvolvida. O que ela faz? Ela me mostra os diretórios do Windows, onde eu posso navegar e escolher o diretório que eu quiser, para selecionar os arquivos. Nesse caso eu consegui selecionar apenas um arquivo, eu não estou conseguindo selecionar vários arquivos. Eu gostaria de selecionar vários arquivos, ou seja, XMLs, e quando eu clicar em abrir, eu povoar o meu checkListBox, você consegue dar-me uma luz quanto a essa situação?
private void btnProcuraXML_Click(object sender, EventArgs e) { OpenFileDialog ofd1 = new OpenFileDialog(); //define as propriedades do controle //OpenFileDialog ofd1.Multiselect = false; ofd1.Title = "Selecionar XML"; ofd1.InitialDirectory = @"C:\GLSISTEM\GERADOS\"; //filtra para exibir somente arquivos XML ofd1.Filter = "Images (*.XML)|*.XML|" + "All files (*.*)|*.*"; ofd1.CheckFileExists = true; ofd1.CheckPathExists = true; ofd1.FilterIndex = 2; ofd1.RestoreDirectory = true; ofd1.ReadOnlyChecked = false; ofd1.ShowReadOnly = false; DialogResult dr = ofd1.ShowDialog(); if (dr == System.Windows.Forms.DialogResult.OK) { txtNroChaveDigital6.Text = ""; // Le os arquivos selecionados foreach (String arquivo in ofd1.FileNames) { txtNroChaveDigital6.Text += arquivo; } } }