【问题标题】:c# Windows Form, replace string in textbox (file content) with another stringc# Windows Form,用另一个字符串替换文本框中的字符串(文件内容)
【发布时间】:2017-08-16 14:56:06
【问题描述】:

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

我可以在应用程序中加载包含特定字符串的文件的特定行:

如果有任何文本框会被更改,我将如何在按下“编辑模块”按钮后更新文件/主文本框。

例如,我将考试权重:“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];
        }
    }

如果有足够代表的人可以将图片插入这篇文章,那就太好了。谢谢

【问题讨论】:

  • 首先,如果您正在阅读文本文件,或者您总是只填充第一行或最后一行,如何填充右侧的文本框?就我个人而言,我会创建一个包含所有条目(如模块、学分、学期等)的自定义类,并读取文本文件,将值存储在自定义类列表中,然后在 ListView(而不是左侧的文本框)中填充值,如果用户单击 ListView 中的项目,我可以知道单击的行的索引,我可以在列表视图中编辑该项目。将更新后的列表保存到新的文本文件将很简单。
  • 这是一种非常奇怪的方法来跟踪正在编辑的数据。但是,如果您需要用来自其他控件的序列化数据替换多行文本框中的特定行,那么您将需要跟踪需要替换的行。然后你基本上用它自己替换整个字符串,很可能在一个循环中换出那条线。不将数据作为一个大文本 blob 而是作为内存对象的集合来跟踪可能会更容易。然后输入可以只更新当前对象的属性。
  • 我使用我在问题中提供的代码填充右侧的文本框,它读取所有行,然后在 ComboBox 中选择任何内容,它使用该字符串在文件中搜索它,返回整行,并填充文本框。另外,我已经有一个包含学分等的类。像这样:paste.ofcode.org 这就是应用程序的工作方式(填充 texbox):i.gyazo.com/5c032f5c3fba468486b39ca8f2033a44.mp4
  • 如果使用 ListView 会更好更容易的话,那就太麻烦了。我一定会这样做,以免自己混淆@ErAcube。
  • 这是我第一次处理文件,您可以在其中阅读、编辑、保存等。所以这可能是一团糟:Z

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


【解决方案1】:

此解决方案可能并不完美,但应该适合您。您需要做的是,每当按下 Edit Module 按钮时,根据文本字段创建一个新字符串并将其替换为原始行。先在类内部声明一个字符串变量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 存储在任何文本框/组合框中,则在以下行中相应地替换它们。现在在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 现在将具有行号,并且可以从类中的任何位置访问

【讨论】:

  • 好的,这很好用。我可以编辑并保存文件,然后重新打开它,并进行更改。如何获取选中的行号?那么'lineBeingEdited'会改变吗?
  • in if (s.Contains(citation)) { matchedList.Add(s); } 我希望matchedList 总是只得到一行,对吗?
  • 我想是的,(它不是直接是我的代码,我稍微调整了一下),但我很确定它只存储正在使用的行,因为它只将该行显示为细绳。编辑:它存储与“引文”字符串匹配的行,我很确定
  • @Dr4ken 嘿,我已经更新了我的答案以获取行号。如果这在您的情况下效果很好,请接受我的回答。会帮助我处理我的回购点:)
  • 嘿,感谢您更新您的答案,不幸的是,在您的 for 循环中,我收到此错误:“名称 's' 在当前上下文中不存在”。它指的是foreach (string s in linesArr),因为它在foreach 中有string s,但是在for 循环中它不存在。
猜你喜欢
  • 2023-04-08
  • 2011-07-01
  • 2019-03-21
  • 2018-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-06
  • 2019-12-28
相关资源
最近更新 更多