本文介绍了如何在文本文件中保存列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 早上好! 我怀疑,我搜索了很多但没有发现任何东西,我希望有人可以帮助我。 非常好。 我有2个表格,在Form1上我有一个按钮,在Form2中我有一个带有数据的ListBox。 我想要的是点击按钮Form1并将Form2 ListBox中的数据保存在文本文件中。 列表框中的数据类型相当简单,列表框最多有12行,并且每行只有一个单词。 我尝试过: 这是Form1上的按钮 private void toolStripButtonGuardar_Click(object sender,EventArgs e) { var myForm = new FormVer(); // Escolher onde salvar o arquivo SaveFileDialog sfd = new SaveFileDialog(); sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); sfd.Title =Guardar; sfd.Filter =Arquivos TXT(* .txt)| * .txt; if(sfd.ShowDialog()== DialogResult.OK) { try { File.WriteAllLines(sfd。 FileName,myForm.listBox.Items.OfType< string>()); // Mensagemdeconfirmçç MessageBox.Show(Guardado com sucesso,Notificação,MessageBoxButtons.OK,MessageBoxIcon.Information); } catch(Exception ex) { MessageBox.Show(ex.Message,Erro,MessageBoxButtons.OK,MessageBoxIcon.Error); } } } 但它不起作用,始终将文件保存为空白。解决方案 Good morning!I have a doubt, I searched a lot but found nothing, I hope someone can help me.Very well.I have 2 Forms, on Form1 I have a button and in the Form2 I have a ListBox with data.What I want is to click the button on Form1 and save the data from the Form2 ListBox in a text file.The data types that are in the listbox are fairly straightforward, the listbox has at most 12 line, and each row has only one word.What I have tried:This is button on Form1private void toolStripButtonGuardar_Click(object sender, EventArgs e) { var myForm = new FormVer(); //Escolher onde salvar o arquivo SaveFileDialog sfd = new SaveFileDialog(); sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); sfd.Title = "Guardar"; sfd.Filter = "Arquivos TXT (*.txt)|*.txt"; if (sfd.ShowDialog() == DialogResult.OK) { try { File.WriteAllLines(sfd.FileName, myForm.listBox.Items.OfType<string>()); //Mensagem de confirmação MessageBox.Show("Guardado com sucesso", "Notificação", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }But it doesn't work, always save the file blank. 解决方案 这篇关于如何在文本文件中保存列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-16 02:13