c #Windows Form,用另一个字符串替换文本框(文件内容)中的字符串

时间:2017-08-16 14:56:07

标签: c# arrays string file windows-forms-designer

我有一个文本框,其中包含已加载文件的所有行。 它看起来像这样:

enter image description here

我可以在app中加载包含特定字符串的特定字符串的特定行:

enter image description here

如果要更改任何文本框,我按“编辑模块”按钮后如何更新文件/主文本框。

例如,我会将考试加权:“0.4”更改为考试加权:“0.6”,然后按“编辑模块”按钮,这将编辑主文本框(文件内容)。然后,这将允许我使用更新的内容保存文件。

这是我用来根据文本框中的字符串从文件中获取特定行的代码:

    private void editModuleButton_Click(object sender, EventArgs e)
    {
        citation = editModuleComboBox.Text;

        citationChange();
    }

    private void citationChange()
    {
        List<string> matchedList = new List<string>();

        string[] linesArr = File.ReadAllLines(fileName);

        //find matches
        foreach (string s in linesArr)
        {
            if (s.Contains(citation))
            {
                matchedList.Add(s); //matched
            }
        }

        //output
        foreach (string s in matchedList)
        {
            string citationLine = s;
            string[] lineData = citationLine.Split(',');
            selectedModuleLabel.Text = lineData[2];
            moduleTitleTextBox.Text = lineData[3];
            creditsTextBox.Text = lineData[4];
            semesterTextBox.Text = lineData[5];
            examWeightingTextBox.Text = lineData[6];
            examMarkTextBox.Text = lineData[7];
            testWeightingTextBox.Text = lineData[8];
            testMarkTextBox.Text = lineData[9];
            courseworkWeightingTextBox.Text = lineData[10];
            courseworkMarkTexbox.Text = lineData[11];
        }
    }

如果有足够代表的人可以将图像插入此帖子,那就太棒了。感谢

1 个答案:

答案 0 :(得分:0)

这个解决方案可能不是完美的,但应该适合你。您需要做的是每当按下编辑模块按钮时,根据文本字段创建一个新字符串并将其替换为原始行。首先在类中声明一个字符串变量private string ChangedString = "";,然后:

    foreach (string s in matchedList)
    {
        string citationLine = s;
        string[] lineData = citationLine.Split(',');
        string Stream = lineData[0]; //Store this somewhere so that it can be accessed later
        string Stage = lineData[1]; //Store this somewhere so that it can be accessed later
        selectedModuleLabel.Text = lineData[2];
        moduleTitleTextBox.Text = lineData[3];
        creditsTextBox.Text = lineData[4];
        semesterTextBox.Text = lineData[5];
        examWeightingTextBox.Text = lineData[6];
        examMarkTextBox.Text = lineData[7];
        testWeightingTextBox.Text = lineData[8];
        testMarkTextBox.Text = lineData[9];
        courseworkWeightingTextBox.Text = lineData[10];
        courseworkMarkTexbox.Text = lineData[11];
    }

StreamStage存储在任何Textbox / ComboBox中,如果您还没有,请在以下行中相应地替换它们。现在在EditButton_Click [点击事件]中写道:

ChangedString  = Stream + "," + Stage + "," + selectedModuleLabel.Text + "," + moduleTitleTextBox.Text
                + "," + creditsTextBox.Text + "," + semesterTextBox.Text + "," +  examWeightingTextBox.Text + "," 
                + examMarkTextBox.Text + "," + courseworkWeightingTextBox.Text + "," + courseworkMarkTexbox.Text;

现在用原始行替换此字符串。

编辑:正如您将获得正在编辑的行号,将其存储在变量中,假设

int LineBeingEdited = 3 //Supposing line number three is being edited.

然后在同一个Click事件中你可以写下这个:

ChangedString  = Stream + "," + Stage + "," + selectedModuleLabel.Text + "," + moduleTitleTextBox.Text
                + "," + creditsTextBox.Text + "," + semesterTextBox.Text + "," +  examWeightingTextBox.Text + "," 
                + examMarkTextBox.Text + "," + courseworkWeightingTextBox.Text + "," + courseworkMarkTexbox.Text;

var lines = TextBox1.Lines;
lines[LineBeingEdited] = ChangedString;
TextBox1.Lines = lines;

编辑2:要获取行号,我建议您将for each循环修改为for循环。还要添加int变量以在类中存储行号,如:private int LineBeingEdited = 0;

修改此for each

foreach (string s in linesArr)
    {
        if (s.Contains(citation))
        {
            matchedList.Add(s); //matched
        }
    }

for循环:

for (int a = 0; a < linesArr.Length; a++)
     {
        if (s.Contains(citation))
        {
           matchedList.Add(linesArr[a]); //matched
           LineBeingEdited = a;
           break; //breaks the loop when a match is found
        }
     }

正在使用上述方法,考虑到始终存在单一匹配LineBeingEdited现在将拥有行号,可以从班级的任何地方访问

相关问题