在数组中查找与选择匹配的元素并写入数组的该行
Find element in array that matches selection and write to that row of the array
好消息是我已经到了这个项目的最后一部分,坏消息是我想不通。我的程序由两种形式组成。第一种形式只在此时相关,因为它是我最初将文本文件加载到字典中的地方。
class SharedMethods
{
public static void LoadDictionary(Dictionary<string, string> vendorPhones)
{
string currentLine;
string[] fields = new string[2];
StreamReader vendorReader = new StreamReader("Vendor.txt");
while (vendorReader.EndOfStream == false)
{
currentLine = vendorReader.ReadLine();
fields = currentLine.Split(',');
vendorPhones.Add(fields[1], fields[6]);
string[] name = { fields[1] };
string[] phone = { fields[6] };
}
vendorReader.Close();
}
}
现在第二个表格很重要。此表单从第一个表单打开,并允许用户从组合框中 select 一个名称,并且属于该名称的 phone 数字显示在文本框中。然后用户可以在文本框中键入以覆盖该名称,然后单击“保存”将其保存到文本文件中。我的问题是我无法弄清楚如何让写入函数在文本中找到 selected 名称,然后覆盖该行中的当前 phone 元素。这是我的表单代码:
public partial class UpdateVendor : Form
{
public UpdateVendor()
{
InitializeComponent();
}
public Dictionary<string, string> vendorPhones = new Dictionary<string, string>();
private void UpdateVendor_Load(object sender, EventArgs e)
{
SharedMethods.LoadDictionary(vendorPhones);
foreach (string name in vendorPhones.Keys)
{
cboVendors.Items.Add(name);
}
}
private void cboVendors_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedName = cboVendors.SelectedItem.ToString();
string phone = vendorPhones[selectedName];
txtPhone.Text = phone.ToString();
}
private void btnSave_Click(object sender, EventArgs e)
{
//SharedMethods.LoadDictionary(vendorPhones);
//string selectedName = cboVendors.SelectedItem.ToString();
//string newPhone;
//newPhone = txtPhone.Text;
//using (var sw = new StreamWriter("Vendors.txt"))
//{
//}
// I've tried a lot of things but can't get any to work.
}
如果代码让您感到厌恶,我们深表歉意。我只是在学习代码,我很高兴它能成功一半。
补充一下,程序在 运行 时的样子如下:
Second form running
尝试以下操作:
string selectedName = cboVendors.SelectedItem.ToString();
string phone = vendorPhones[selectedName];
if (vendorPhones.ContainsKey(selectedName))
{
vendorPhones[selectedName] = phone;
}
else
{
vendorPhones.Add(selectedName, phone);
}
这里的问题是,因为您一开始在读取文件时没有捕获所有数据,所以您在写回文件时必须有点技巧,这样您就不会丢失任何数据。
我们可以做的是将整个文件重新读回一个数组,然后在数组的每一行中搜索包含我们的供应商和原始 phone 编号的行。当我们找到该行时,将 phone 数字部分更新为新数字。然后,再次将所有行写回文件
这是一种方法:
private void btnSave_Click(object sender, EventArgs e)
{
// Grab the selected vendor name
string selectedName = cboVendors.SelectedItem.ToString();
// Grab the original phone number for this vendor
string originalPhone = vendorPhones[selectedName];
// Read all the file lines into an array:
var fileLines = File.ReadAllLines("Vendor.txt");
// Now we iterate over the file lines one by one, looking for a match
for (int i = 0; i < fileLines.Length; i++)
{
// Break the line into parts to see if this line is the one we need to update
string[] lineParts = fileLines[i].Split(',');
string name = lineParts[1];
string phone = lineParts[6];
// Compare this line's name and phone with our originals
if (name == selectedName && phone == originalPhone)
{
// It's a match, so we will update the phone number part of this line
lineParts[6] = txtPhone.Text;
// And then we join the parts back together and assign it to the line again
fileLines[i] = string.Join(",", lineParts);
// Now we can break out of the loop since we updated our vendor info
break;
}
}
// Finally, we can write all the lines back to the file
File.WriteAllLines("Vendor.txt", fileLines);
// May not be necessary, but if you're not reading back from the file right away
// then update the dictionary with the new phone number for this vendor
vendorPhones[selectedName] = txtPhone.Text;
}
然而,理想情况下,您将在读取文件时捕获所有数据,并且为文件中的每一行创建一个新的 Vendor
对象,该对象具有表示每个逗号分隔值的属性。然后,这些 Vendor
个对象可以存储在 List<Vendor>
中。现在您有了一组可以显示和操作的强类型对象,完成后您可以用类似的方式将它们全部写回到文件中。
但这不是你的问题,听起来你可能还没有完全明白...
好消息是我已经到了这个项目的最后一部分,坏消息是我想不通。我的程序由两种形式组成。第一种形式只在此时相关,因为它是我最初将文本文件加载到字典中的地方。
class SharedMethods
{
public static void LoadDictionary(Dictionary<string, string> vendorPhones)
{
string currentLine;
string[] fields = new string[2];
StreamReader vendorReader = new StreamReader("Vendor.txt");
while (vendorReader.EndOfStream == false)
{
currentLine = vendorReader.ReadLine();
fields = currentLine.Split(',');
vendorPhones.Add(fields[1], fields[6]);
string[] name = { fields[1] };
string[] phone = { fields[6] };
}
vendorReader.Close();
}
}
现在第二个表格很重要。此表单从第一个表单打开,并允许用户从组合框中 select 一个名称,并且属于该名称的 phone 数字显示在文本框中。然后用户可以在文本框中键入以覆盖该名称,然后单击“保存”将其保存到文本文件中。我的问题是我无法弄清楚如何让写入函数在文本中找到 selected 名称,然后覆盖该行中的当前 phone 元素。这是我的表单代码:
public partial class UpdateVendor : Form
{
public UpdateVendor()
{
InitializeComponent();
}
public Dictionary<string, string> vendorPhones = new Dictionary<string, string>();
private void UpdateVendor_Load(object sender, EventArgs e)
{
SharedMethods.LoadDictionary(vendorPhones);
foreach (string name in vendorPhones.Keys)
{
cboVendors.Items.Add(name);
}
}
private void cboVendors_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedName = cboVendors.SelectedItem.ToString();
string phone = vendorPhones[selectedName];
txtPhone.Text = phone.ToString();
}
private void btnSave_Click(object sender, EventArgs e)
{
//SharedMethods.LoadDictionary(vendorPhones);
//string selectedName = cboVendors.SelectedItem.ToString();
//string newPhone;
//newPhone = txtPhone.Text;
//using (var sw = new StreamWriter("Vendors.txt"))
//{
//}
// I've tried a lot of things but can't get any to work.
}
如果代码让您感到厌恶,我们深表歉意。我只是在学习代码,我很高兴它能成功一半。
补充一下,程序在 运行 时的样子如下: Second form running
尝试以下操作:
string selectedName = cboVendors.SelectedItem.ToString();
string phone = vendorPhones[selectedName];
if (vendorPhones.ContainsKey(selectedName))
{
vendorPhones[selectedName] = phone;
}
else
{
vendorPhones.Add(selectedName, phone);
}
这里的问题是,因为您一开始在读取文件时没有捕获所有数据,所以您在写回文件时必须有点技巧,这样您就不会丢失任何数据。
我们可以做的是将整个文件重新读回一个数组,然后在数组的每一行中搜索包含我们的供应商和原始 phone 编号的行。当我们找到该行时,将 phone 数字部分更新为新数字。然后,再次将所有行写回文件
这是一种方法:
private void btnSave_Click(object sender, EventArgs e)
{
// Grab the selected vendor name
string selectedName = cboVendors.SelectedItem.ToString();
// Grab the original phone number for this vendor
string originalPhone = vendorPhones[selectedName];
// Read all the file lines into an array:
var fileLines = File.ReadAllLines("Vendor.txt");
// Now we iterate over the file lines one by one, looking for a match
for (int i = 0; i < fileLines.Length; i++)
{
// Break the line into parts to see if this line is the one we need to update
string[] lineParts = fileLines[i].Split(',');
string name = lineParts[1];
string phone = lineParts[6];
// Compare this line's name and phone with our originals
if (name == selectedName && phone == originalPhone)
{
// It's a match, so we will update the phone number part of this line
lineParts[6] = txtPhone.Text;
// And then we join the parts back together and assign it to the line again
fileLines[i] = string.Join(",", lineParts);
// Now we can break out of the loop since we updated our vendor info
break;
}
}
// Finally, we can write all the lines back to the file
File.WriteAllLines("Vendor.txt", fileLines);
// May not be necessary, but if you're not reading back from the file right away
// then update the dictionary with the new phone number for this vendor
vendorPhones[selectedName] = txtPhone.Text;
}
然而,理想情况下,您将在读取文件时捕获所有数据,并且为文件中的每一行创建一个新的 Vendor
对象,该对象具有表示每个逗号分隔值的属性。然后,这些 Vendor
个对象可以存储在 List<Vendor>
中。现在您有了一组可以显示和操作的强类型对象,完成后您可以用类似的方式将它们全部写回到文件中。
但这不是你的问题,听起来你可能还没有完全明白...