2016-06-28 66 views
0

因此,我的组合框中的列表来自文本文件。该程序允许用户从组合框中选择一个项目。选定的项目应该从组合框和文本文件中删除,也可以通过点击按钮。如何从文本文件中的组合框中删除选定的项目

该代码允许程序来从文本文件中的项目,我的组合框:

string location = @"C:\\Users\\LMCPENA98\\Desktop\\COE114LPROJECT_MILLENNIUM_PAWS\\MillenniumPaws\\MillenniumPaws\\bin\\Debug\\Files.txt"; 
string[] temp = File.ReadAllLines(location); 
int[] TagNumber = new int[temp.Length]; 
string[] Breed = new string[temp.Length]; 
string[] Name = new string[temp.Length]; 
decimal[] Price = new decimal[temp.Length]; 
//Getting all the values i the text file 
for (int i = 0; i < TagNumber.Length; i++) 
{ 
TagNumber[i] = int.Parse(temp[i].Substring(0, temp[i].IndexOf("-"))); 
Breed[i] = temp[i].Substring(0, temp[i].IndexOf("+")); 
Breed[i] = Breed[i].Substring(Breed[i].LastIndexOf("-") + 1); 
Name[i] = temp[i].Substring(0, temp[i].IndexOf("=")); 
Name[i] = Name[i].Substring(Name[i].LastIndexOf("+") + 1); 
Price[i] = decimal.Parse(temp[i].Substring(temp[i].LastIndexOf("=") + 1)); 
} 

Pound p; 
for (int i = 0; i < Breed.Length; i++) 
{ 
if (Breed[i] == cmbBx_breed.Text) 
{ 
    p = new Pound(TagNumber[i], Name[i], Price[i]); 
    cmbBx_opts.Items.Add(p.GetEntry()); 
    } 
} 
} 
else 
{ 
cmbBx_breed.Text = null; 
} 

这是我如何让知道我在做选择的项目的程序:

private void btn_buy_Click(object sender, EventArgs e) 
{ 
    new MessageBox_TYBuying().Show(); 
    cmbBx_opts.Items.Remove(cmbBx_opts.SelectedItem); 
} 

因此,当我开始编程时,组合框中选定的项目现在被删除,但不在文本文件中。我该怎么做??

+0

为什么不使用.json文件或.xml文件?使用更好,更简单.. – Furtiro

+0

我的团队和我不知道如何使用这些文件 –

回答

0

没有“从文本文件中删除行”这样的事情。你所能做的只是:

方法1)
打开原始文件并将所有行读入List<string>。然后,遍历所有这些列表条目并删除需要删除的条目。然后,将所有列表条目写回文件。

方法2)
将原始文件重命名为某个“备份”文件。打开用于读取的备份文件以及用原始文件名写入的新文件。逐行读取备份文件。除了要删除的行之外,将每行写入输出文件。

相关问题