[英]how to go next line in ListBox?
大家好,我想在我的listBox中加載文件路徑。 一切正常,我只是遇到一個問題,即當我關閉該應用程序然后再次打開它時,加載的文件位於一個通道中,並且將它們識別為listBox中的一項,我嘗試使用“ \\ n”,“ \\ r”無這些作品中...那么你們的建議是什么? (我將用戶更改保存在“應用設置”中,以供日后閱讀)
private void Form1_Load(object sender, EventArgs e)
{
if (Properties.Settings.Default.FileList != string.Empty)
{
fileListBox.Items.Add(Properties.Settings.Default.FileList);
}
UnlockForm f2 = new UnlockForm();
if (Properties.Settings.Default.PasswordCheck == true)
f2.ShowDialog();
else
return;
}
private void button1_Click_1(object sender, EventArgs e)
{
op = new OpenFileDialog();
op.Title = "Select your stuff";
op.Filter = "All files (*.*)|*.*";
if (op.ShowDialog() == DialogResult.OK)
{
fileName = op.FileName;
fileListBox.Items.Add(fileName);
}
Properties.Settings.Default.FileList += fileName+"\n";
Properties.Settings.Default.Save();
}
在設置設計器中創建屬性時:
Files
System.Collections.Specialized.StringCollection
User
Value
單元格中使用...編輯默認值。 然后,您可以輕松地將其設置為ListBox
DataSource
。
listBox1.DataSource = Properties.Settings.Default.Files;
還添加一些值:
Properties.Settings.Default.Files.Add("something");
Properties.Settings.Default.Save();
如果您向Files
添加了一些內容,如果希望ListBox
顯示更改,請將DataSource
設置為null,然后再次設置為Files
。
您似乎已在我們的“應用程序設置”中將FileList定義為String。 有兩種方法可以解決此問題。
a)使用FileList作為集合。
您可以在“應用程序設置”中將“文件列表的類型”更改為StringCollection。 然后,您可以將項目添加到列表中,如下所示
fileListBox.Items.AddRange(Properties.Settings.Default.FileList.Cast<string>().ToArray());
b)使用FileList作為字符串。
如果您確實想將Properties.Settings.Default.FileList保留為字符串,則需要在運行時使用定界符將其拆分(假設為';')
fileListBox.Items.AddRange(Properties.Settings.Default.FileList.Split(new[] { ';' },StringSplitOptions.RemoveEmptyEntries));
在您的情況下,集合可能是更好的方法,除非您有特定的原因超出了OP的使用字符串范圍。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.