2012-04-24 47 views
1

好吧,我要再试一次。我现在获得了更多信息。我知道我不能使用打开和保存对话框,并且没有数据库。所以我仍然有点失落,因为我被告知如何在打开和保存对话框之前做到这一点。我打算把我想要做的,然后到目前为止我所拥有的代码。我有我的代码必须建立和添加。我也会展示我想要添加的内容。我只是试图找到理解这个原因的最好方法,现在我不知道。我还是个新人,我知道最近几天人们一直在试图帮助我理解,然后我被告知它没有打开和保存对话框。这是我想要做的。文件路径和文本框

•添加一个名为txtFilePath <文本框---已经有

•旁边添加一个按钮,上面的文本框,上面写着“加载”(适当命名)< -already有

•添加一个按钮,上面写着“保存”(适当命名)< - 已经有此

•当thebutton“装载”被点击,阅读文本 (txtFilePath:不是相对的绝对路径)指定的文件,并添加发现的对象内到列表框中< ---不理解

•当用户点击“保存”按钮,写选定的记录 在txtFilePath(绝对路径而不是相对)指定的文件,而不 截断目前里面的值< - 不理解

这里是代码的一部分,我有:`

public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void addButton_Click(object sender, EventArgs e) 
     { 
      EditDialog newEmployeeDialog = new EditDialog(); 
      if (newEmployeeDialog.ShowDialog() == DialogResult.OK) 
      { 
       employeeList.Items.Add(newEmployeeDialog.StaffMember); 
      } 
     } 

     private void deleteButton_Click(object sender, EventArgs e) 
     { 
      if (employeeList.SelectedIndex == -1) 
       return; 

      if (MessageBox.Show("Really delete this employee", 
       "Delete", MessageBoxButtons.YesNo, 
        MessageBoxIcon.Question) 
      == DialogResult.Yes) 
      { 
       employeeList.Items.Remove(
        employeeList.SelectedItem); 
      } 
     } 

     private void editButton_Click(object sender, EventArgs e) 
     { 
      if (employeeList.SelectedIndex == -1) 
       return; 

      int employeeNum = employeeList.SelectedIndex; 
      EditDialog newEmployeeDialog = new EditDialog(); 
      newEmployeeDialog.StaffMember = 
       (Employee)employeeList.SelectedItem; 

      if (newEmployeeDialog.ShowDialog() == DialogResult.OK) 
      { 
       employeeList.Items.RemoveAt(employeeNum); 
       employeeList.Items.Insert(employeeNum, newEmployeeDialog.StaffMember); 
       employeeList.SelectedIndex = employeeNum; 
      } 
     } 

     private void employeeList_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      if (employeeList.SelectedIndex != -1) 
      { 
       Employee currentEmployee = (Employee)employeeList.SelectedItem; 
       firstName.Text = currentEmployee.FirstName; 
       lastName.Text = currentEmployee.LastName; 
       jobTitle.Text = currentEmployee.JobTitle; 
      } 
      else 
      { 
       firstName.Text = lastName.Text = jobTitle.Text = ""; 
      } 
     } 
` 

现在我知道你不能看到按钮点击,但我确实有这些标记。我知道你何时使用open并保存它的工作方式。我如何去做这件事?我会使用流编写器right.I理解用户将输入路径到文本框中,当用户点击加载时,它将加载它们指定的文件。现在我只是想了解一个代码能够说出这个权利。

会是这样的:

String filePath = this.txtFilePath.Text; 

,因为我需要命名的文本框txtFilePath。我知道你们中的一些人可能会说这很简单,但是当你第一次学习时,看起来并不那么简单。自从我在家里上大学以来,我一直在试图帮助我理解。感谢您阅读希望听到你们。

更新:难道是这样的

读取文件

private void Load_Click(object sender, EventArgs e) 
{ 
StreamReader myReader = new StreamReader(C:\\") 
txtFilePath.Text = my reader.read to end(); 
myReader.Close(); 
} 

则表示有写入文件

{ 
StreamWriter myWriter = new StreamWriter("C:\\test.txt", true); 
      myWriter.Write("Some text"); 
      myWriter.WriteLine("Write a line"); 
      myWriter.WriteLine("Line 2"); 
      myWriter.Close(); 
} 

如果这是正确的,那么我必须得到它,如果文件不存在让记事本弹出以便它们可以添加它,则可以保存它而不删除文件或文件中的任何内容。

回答

0

假设该文件包含员工姓名列表,你应该能够使用像这样的东西它们加载到你的列表框:

var filepath = txtFilePath.Text; 
if (File.Exists(filepath)) 
{ 
    var lines = File.ReadAllLines(filepath); 
    foreach (var line in lines) 
     employeeList.Items.Add(line); 
} 

然后假设你要到一个新员工的名字添加到该文件用户只需进入列表框:

var filepath = txtFilePath.Text; 
if (File.Exists(filepath)) 
    using (var sw = File.AppendText(filepath)) 
     sw.WriteLine((string)employeeList.Text); 
else 
    using (var sw = File.CreateText(filepath)) 
     sw.WriteLine((string)employeeList.Text);  

这尚未经过测试,但它应该工作,几乎原样......

+0

这就是我的工作是代码本身,因此文件将会 加载到列表框中。我很想理解,所以我可以编写代码。 – shan 2012-04-24 15:13:01

+0

因此,“加载”按钮必须允许你选择一个文件,而不用实际使用Open Dialog控件?这是你正在努力的一部分吗? – 2012-04-24 15:20:20

+0

是的,我知道如何使用opendialog,但我不允许使用它。而且我认为保存一份员工名单可以让你保存每个人。 – shan 2012-04-24 15:34:14