Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#Windows窗体,将文本框(文件内容)中的字符串替换为另一个字符串_C#_Arrays_String_File_Windows Forms Designer - Fatal编程技术网

c#Windows窗体,将文本框(文件内容)中的字符串替换为另一个字符串

c#Windows窗体,将文本框(文件内容)中的字符串替换为另一个字符串,c#,arrays,string,file,windows-forms-designer,C#,Arrays,String,File,Windows Forms Designer,我有一个文本框,其中包含加载文件的所有行。 看起来是这样的: 我可以在应用程序中使用以下命令加载包含特定字符串的文件的特定行: 在按下“编辑模块”按钮后,如果任何文本框发生更改,我将如何更新文件/主文本框 例如,我会将考试权重:“0.4”更改为考试权重:“0.6”,然后按“编辑模块”按钮编辑主文本框(文件内容)。这样我就可以用更新的内容保存文件 这是我用来根据文本框中的字符串从文件中获取特定行的代码: private void editModuleButton_Click(objec

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

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

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

例如,我会将考试权重:“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];
        }
    }
private void editModuleButton_单击(对象发送方,事件参数e)
{
引文=编辑模块mbobox.Text;
引文变化();
}
私人更改()
{
List matchedList=新列表();
字符串[]linesArr=File.ReadAllLines(文件名);
//查找匹配项
foreach(行中的字符串s)
{
如果(包含(引文))
{
matchedList.Add(s);//匹配
}
}
//输出
foreach(matchedList中的字符串s)
{
字符串行=s;
字符串[]lineData=引文行.Split(',');
selectedModuleLabel.Text=lineData[2];
moduleTitleTextBox.Text=lineData[3];
creditsTextBox.Text=lineData[4];
semestextbox.Text=lineData[5];
examWeightingTextBox.Text=lineData[6];
Text=lineData[7];
testWeightingTextBox.Text=lineData[8];
Text=lineData[9];
courseworkWeightingTextBox.Text=lineData[10];
courseworkMarkTexbox.Text=lineData[11];
}
}

如果有人有足够的代表性可以插入这篇文章的图像,那将是伟大的。谢谢

此解决方案可能并不完美,但应该适合您。您需要做的是,每当按下“编辑模块”按钮时,根据文本字段创建一个新字符串,并将其替换为原始行。首先声明一个字符串变量
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];
    }
如果您尚未在任何文本框/组合框中存储
阶段
,则在下一行相应地替换它们。现在在编辑按钮中单击[单击事件]写入:

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.
然后,在相同的单击事件中,您可以再次写入以下内容:

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:要获取行号,我建议您将每个
循环的
修改为
循环的
。还添加一个
int
变量来存储类中的行号,如:
private int LineBeingEdited=0

为每个
修改此

foreach (string s in linesArr)
    {
        if (s.Contains(citation))
        {
            matchedList.Add(s); //matched
        }
    }
要对循环执行
,请执行以下操作:

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
        }
     }
for(int a=0;a

使用上述方法时,考虑到始终存在一个单一匹配
LineBeingEdited
现在将具有行号,并且可以从类中的任何位置进行访问

此解决方案可能并不完美,但应该适合您。您需要做的是,每当按下“编辑模块”按钮时,根据文本字段创建一个新字符串,并将其替换为原始行。首先声明一个字符串变量
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];
    }
如果您尚未在任何文本框/组合框中存储
阶段
,则在下一行相应地替换它们。现在在编辑按钮中单击[单击事件]写入:

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.
然后,在相同的单击事件中,您可以再次写入以下内容:

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:要获取行号,我建议您将每个
循环的
修改为
循环的
。还添加一个
int
变量来存储类中的行号,如:
private int LineBeingEdited=0

为每个
修改此

foreach (string s in linesArr)
    {
        if (s.Contains(citation))
        {
            matchedList.Add(s); //matched
        }
    }
要对循环执行
,请执行以下操作:

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
        }
     }
for(int a=0;a

使用上述方法时,考虑到始终存在一个单一匹配<代码>行编辑
现在将具有行号,并且可以从类中的任何位置访问

首先,如果您正在阅读文本文件或始终只填充第一行或最后一行,如何填充右侧的文本框?就我个人而言,我会创建一个自定义类,包含所有条目,如模块、学分、学期等,并读取文本文件,将值存储在自定义类列表中,然后在ListView(而不是左侧的文本框)中填充值,如果用户单击ListView中的项,我可以知道所单击行的索引,并且可以在listview中编辑该项。将更新后的列表保存到一个新的文本文件是很简单的。这是一种非常奇怪的跟踪正在编辑的数据的方法。但是,如果您需要用来自其他控件的序列化数据替换多行文本框中的特定行,那么您可以