按用户放入文本框的名称获取文件

时间:2014-08-02 11:19:26

标签: c# forms text

我是C#的新手,想知道如何通过用户放入文本框的名称获取文件,然后将该数据加载到数组中,并将数组中的每个项目显示为单独的文本框,然后进行编辑,再次保存到该文件

enter image description here

namespace test
{
    public partial class Form1 : Form
    {
        private TextBox[] textBoxes;
        private Button[] buttons;
        private const string fileName = (getFile.Text);


        public Form1()
        {
            InitializeComponent();

            textBoxes = new TextBox[] { textBox1, textBox2, textBox3, textBox4 };
            buttons = new Button[] { button1, button2, button3, button4 };
        }

        private static void ReplaceLineInFile(string path, int lineNumber, string newLine)
        {
            if (File.Exists(path))
            {
                string[] lines = File.ReadAllLines(path);
                lines[lineNumber] = newLine;
                File.WriteAllLines(path, lines);
            }
        }



        private void Form1_Load(object sender, EventArgs e)
        {
            LoadFile();
        }

        private void LoadFile()
        {
            if (!File.Exists(fileName))
            {
                WriteAllLines();
                return;
            }

            string[] lines = File.ReadAllLines(fileName);
            if (lines.Length != textBoxes.Length)
            {
                // the number of lines in the file doesn't fit so create a new file
                WriteAllLines();
                return;
            }

            for (int i = 0; i < lines.Length; i++)
            {
                textBoxes[i].Text = lines[i];
            }
        }

        private void WriteAllLines()
        {
            // this will create the file or overwrite an existing one
            File.WriteAllLines(fileName, textBoxes.Select(tb => tb.Text));
        }



        private void button1_Click(object sender, EventArgs e) // save line 1
        {
            ReplaceLineInFile(fileName, 0, textBox1.Text);
        }

        private void button2_Click(object sender, EventArgs e) // save line 2
        {
            ReplaceLineInFile(fileName, 1, textBox2.Text);
        }

        private void button3_Click_1(object sender, EventArgs e) // save line 3
        {
            ReplaceLineInFile(fileName, 2, textBox3.Text);
        }

        private void button4_Click(object sender, EventArgs e) // save line 4
        {
            ReplaceLineInFile(fileName, 3, textBox4.Text);
        }

        private void button_Click(object sender, EventArgs e) // save all
        {

            Button button = sender as Button;
            if (button != null)
            {
                int lineNumber = Array.IndexOf(buttons, button);
                if (lineNumber >= 0)
                {
                    ReplaceLineInFile(fileName, lineNumber, textBoxes[lineNumber].Text);
                }
            }

        }

        private void button5_Click(object sender, EventArgs e) // get file
        {
            if (File.Exists(getFile.Text))
            {
                //shows message if testFile exist 
                MessageBox.Show("File " + getFile.Text + " Exist ");
            }
            else
            {
                //create the file testFile.txt 
                File.Create(getFile.Text);
                MessageBox.Show("File " + getFile.Text + " created ");
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

首先在表单上放置一个TableLayoutPanel控件。这将有助于轻松放置其他控件。

将此面板停靠在您想要的位置,然后编辑列并将两列的SizeType设置为AutoSize。将AutoScroll设置为true。保留控件的默认名称为tableLayoutPanel1

现在改变方法。删除所有buttonX_Click方法并粘贴此代码,而不是现有的LoadFile

private void button1_Click(object sender, EventArgs e)
{
    string[] lines = File.ReadAllLines(fileName);

    // Clear the existing rows
    tableLayoutPanel1.RowStyles.Clear();
    for (int i = 0; i < lines.Length; i++)
    {
        // Add a new row
        tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));

        // Create the TextBox
        TextBox txt = new TextBox();

        // Add any initializations for the text box here
        txt.Text = lines[i];

        // Create the button
        Button btn = new Button();

        // Add any initializations for the button here
        btn.Text = i.ToString();
        // Handle the button's click event
        btn.Click += btn_Click;
        // This value helps the button know where it is and which TextBox it is associated to
        btn.Tag = new object[] { i, txt };
        btn.Width = 30;

        // Add the controls to the created row
        tableLayoutPanel1.Controls.Add(txt, 0, i);
        tableLayoutPanel1.Controls.Add(btn, 1, i);
    }
}

void btn_Click(object sender, EventArgs e)
{
    object[] btnData = (object[]) ((Control) sender).Tag;

    // The values are inside the sender's Tag property
    ReplaceLineInFile(fileName, (int)btnData[0], ((TextBox)btnData[1]).Text);
}

更新:这是班级的全部来源,已更正:

namespace test
{
    public partial class Form1 : Form
    {
        private string fileName;

        public Form1()
        {
            InitializeComponent();
        }

        private void ReplaceLineInFile(int lineNumber, string newLine)
        {
            if (File.Exists(fileName))
            {
                string[] lines = File.ReadAllLines(fileName);
                lines[lineNumber] = newLine;
                File.WriteAllLines(fileName, lines);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void LoadFile()
        {
            string[] lines = File.ReadAllLines(fileName);

            // Clear the existing rows
            tableLayoutPanel1.RowStyles.Clear();
            for (int i = 0; i < lines.Length; i++)
            {
                // Add a new row
                tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));

                // Create the TextBox
                TextBox txt = new TextBox();

                // Add any initializations for the text box here
                txt.Text = lines[i];

                // Create the button
                Button btn = new Button();

                // Add any initializations for the button here
                btn.Text = i.ToString();
                // Handle the button's click event
                btn.Click += btn_Click;
                // This value helps the button know where it is and which TextBox it is associated to
                btn.Tag = new object[] { i, txt };
                btn.Width = 30;

                // Add the controls to the created row
                tableLayoutPanel1.Controls.Add(txt, 0, i);
                tableLayoutPanel1.Controls.Add(btn, 1, i);
            }
        }

        void btn_Click(object sender, EventArgs e)
        {
            object[] btnData = (object[])((Control)sender).Tag;

            // The values are inside the sender's Tag property
            ReplaceLineInFile((int)btnData[0], ((TextBox)btnData[1]).Text);
        }

        private void button_Click(object sender, EventArgs e) // save all
        {
            List<string> lines = new List<string>();
            for (int i = 0; i < tableLayoutPanel1.RowCount; i++)
            {
                lines.Add(((TextBox)tableLayoutPanel1.Controls[i * 2]).Text);
            }
            File.WriteAllLines(fileName, lines.ToArray());
        }

        private void button5_Click(object sender, EventArgs e) // get file
        {
            fileName = getFile.Text;
            if (File.Exists(fileName))
            {
                //shows message if testFile exist 
                MessageBox.Show("File " + fileName + " Exist ");
            }
            else
            {
                //create the file testFile.txt 
                File.Create(fileName);
                MessageBox.Show("File " + fileName + " created ");
            }

            LoadFile();
        }
    }
}