【问题标题】:C# Getting a Form method into a User Control C#C# 将 Form 方法放入用户控件 C#
【发布时间】:2013-04-10 16:58:29
【问题描述】:

我正在准备我的第一年考试,我知道的一个问题是“使用文本框和按钮创建一个用户控件,并在表单上创建一个列表框”好的。现在必须发生的是 listBox 列出目录中的文件。并且用户控件中的文本框读取该文件(.txt)的内容并允许用户编辑文本框,并且单击时的保存按钮必须覆盖原始文件。现在我使用 StreamReader 读取了整个文本框。但是我无法获取用户控件中的按钮来获取列表框的目录路径。

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

我尝试反之亦然,将 userControl userControl1.Text 属性与我创建的属性一起使用。然后尝试像 Form1.ItemsName 表单中的 userControl 一样调用它,但没有用,所以我把它刮掉了。现在我有完整的编码器块。

我被难住了。我已经尝试了所有方法来获取 userControl 中的 btnSave 以便能够从 listBox.SelectedItem.Text 表单中获取数据

表单组成:listBox和UserControl

userControl 包括:textBox 和 Button(btnSave)。

Form1代码:

    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");
    }
}

【问题讨论】:

标签: c# visual-studio-2008 user-controls streamreader streamwriter


【解决方案1】:

由于这是一项任务/项目工作,我只会为您指明正确的方向。

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


private void btnSave_Click(object sender, EventArgs e)
{
    StreamWriter sw = new StreamWriter(FileToWrite);
}

【讨论】:

  • 感谢您的帮助。但这不是一个项目或任何相关的课堂作业。这是自我训练。有人谈论过类似的问题,所以我没有作弊或其他任何事情(如果我在课堂上作弊,我不会在生活中帮助我)。我们的教科书从不关注用户控制。问题不在于创建集合或获取。问题出在我必须从表单调用 ListBox.SelectedItem.Text 到位于 userControl 中的 btnSave 中时。我知道如何将用户控件调用到 Form 'userControl1.' 但我不知道如何从 Form 调用到 userControl。再次感谢:)
【解决方案2】:

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

这两个都可以使用事件来完成

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多