Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.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#将表单方法放入用户控件C#_C#_Visual Studio 2008_User Controls_Streamreader_Streamwriter - Fatal编程技术网

C#将表单方法放入用户控件C#

C#将表单方法放入用户控件C#,c#,visual-studio-2008,user-controls,streamreader,streamwriter,C#,Visual Studio 2008,User Controls,Streamreader,Streamwriter,我正在准备第一年的考试,我知道的一个问题是“创建一个带有文本框和按钮的用户控件,并在表单上创建一个列表框”好的。现在需要做的是列表框列出目录中的文件。用户控件中的文本框读取此文件(.txt)的内容,用户可以编辑文本框,单击时的保存按钮必须覆盖原始文件。现在我使用StreamReader阅读整个文本框。但是我无法在用户控件中使用按钮从列表框中获取dir路径 我必须使用StreamWriter覆盖它,不能使用FileDialog。它必须从列表框中获取文件名,然后将其加载到StreamWriter d

我正在准备第一年的考试,我知道的一个问题是“创建一个带有文本框和按钮的用户控件,并在表单上创建一个列表框”好的。现在需要做的是列表框列出目录中的文件。用户控件中的文本框读取此文件(.txt)的内容,用户可以编辑文本框,单击时的保存按钮必须覆盖原始文件。现在我使用StreamReader阅读整个文本框。但是我无法在用户控件中使用按钮从列表框中获取dir路径

我必须使用StreamWriter覆盖它,不能使用FileDialog。它必须从列表框中获取文件名,然后将其加载到StreamWriter dir路径中,该路径需要位于UserControl中的btnSave中

我尝试用userControl
userControl1.Text
属性和我创建的属性做相反的操作。然后尝试像表单
Form1.ItemsName
中的userControl那样调用它,但没有成功,所以我将其删除。现在我有完整的编码块

我被难住了。我已经尽了一切努力让userControl中的btnSave能够从表单
列表框中获取数据。选择editem.Text

表单包括:listBox和UserControl

用户控件包括:文本框和按钮(btnSave)

表格1代码:

    private void Form1_Load(object sender, EventArgs e)
    {
        // Reads the content from the file, 
        //listing other files in dir.

        StreamReader sr = new StreamReader(@"C:\C# Resources\Numbers.txt");
        string line = sr.ReadLine();

        try
        {
            // Adds content from Numbers.txt into the ListBox
            while (line != null)
            {
                this.listBox1.Items.Add(line);

                line = sr.ReadLine();
            }
            sr.Close();
        }
        catch (Exception Exc)
        {
            MessageBox.Show(Exc.Message.ToString());
        }
        finally
        {
            sr.Close();
        }
    }

    private void listBox1_Click(object sender, EventArgs e)
    {
        // Reads the name of the file in the list box, looking for the file in the dir.
        StreamReader file = new StreamReader(@"C:\C# Resources\" + listBox1.SelectedItem.ToString() + ".txt");
        string all = file.ReadToEnd();

        //Loads the ListBox_click file's content into the userControl textBox
        userControl11.Text = all.ToString();
    }
}
用户控制代码:

    // Creates method so the it can be loaded into form
    public string Text
    {
        set
        {
            textBox2.Text = value;
        }
        get
        {
            return textBox2.Text;
        }
    }

    //Has to overwrite/Save the content changed by the user in TextBox to the same 
    //directory/file it came from.

    private void btnSave_Click(object sender, EventArgs e)
    {
        StreamWriter sw = new StreamWriter(@"C:\C# Resources\" + /*Method to get the file path from the Form1 ListBox*/ + ".txt");
    }
}

由于这是一项任务/项目工作,我将为您指出正确的方向

  • 向用户控件添加属性,如
    publicstringfiletowrite{get;set;}
  • 然后访问
    FileToWrite
    ,这将是写入的路径

您不能(或不应该)使用用户控件从调用表单代码。您需要另一种机制,表单应该随时让控件知道所选项,或者用户控件应该请求信息


这两项都可以通过事件来完成

试试这门课。谢谢你的帮助。但这不是一个项目或任何相关的课堂作业。这是自我训练。有人谈论过一个类似的问题,所以我不会作弊或做任何事情(如果我在课堂上作弊的话,这对我的生活没有帮助)。我们的教科书从不关注用户控制。问题不是创建集合或get。问题在于,当我必须从表单中调用ListBox.SelectedItem.Text到userControl中的btnSave时。我知道如何将用户控件调用到表单“userControl1”。但我不知道如何从表单调用到userControl。再次感谢:)
private void btnSave_Click(object sender, EventArgs e)
{
    StreamWriter sw = new StreamWriter(FileToWrite);
}