在 C# 中将文本从一个表单 RichtextBox 移动到另一个表单上的另一个表单

发布于 2024-10-04 08:24:16 字数 127 浏览 3 评论 0原文

我正在制作一个程序,允许用户通过单独的表单添加注释,以便他们能够将以该表单编写的文本添加回原始表单。目前,我无法将以一种形式的 RichTextBox 编写的文本移动到主窗体中的另一个 RichTextBox。请帮忙,

谢谢

I am making a program that allows users to add notes via a separate form for them to be able to add that text written in that form back into the original form. At the moment i can't move the text written in one form richtextbox to the other richtextbox in the main form. Please Help,

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

荒路情人 2024-10-11 08:24:16

您的问题很可能是由于 richtextbox 控件的范围造成的。默认情况下,在 WinForms 中,当您在窗体中创建控件时,它的范围为 Private。这意味着该控件将无法从另一个窗体的代码直接访问。有几种方法可以解决这个问题。最简单的方法是在此声明中将其更改为更广泛的范围(公共/内部),这将允许您执行以下操作:

richTextBox1.Text = Form2.richTextBox1.Text;

*这假设您的代码位于 Form1 中,而您的第二个表单名为 Form2。显然,这需要进行相应的改变。

这不被认为是好的做法,因为允许程序的任何其他部分编辑此表单中包含的控件存在潜在风险。将信息传递到表单的理想解决方案是通过构造函数、适当范围的方法、适当范围的属性,或者在某些情况下通过事件,具体取决于信息的类型、何时可以修改以及其用途。

在不了解有关您的具体情况的太多细节的情况下,我可能会建议使用一种方法,因为这将是最简单的情况。

public void UpdateText(string value) {
   richTextBox1.Text = value;
}

这将允许其他窗体和控件调用此方法,从而更新 richTextBox 的文本。

我要指出的是,这些示例都不应该在生产代码中使用(我不展示如何处理跨线程问题或数据验证),但我认为从问题的性质来看,您这样做更多是作为学习练习。

Most likely your issue is going to be due to the scope of the richtextbox controls. By default in WinForms when you create a control in a form, it has the scope of Private. This means that the control will not be directly accessible from another form's code. There are several ways around this. The simplest would be to change it to a broader scope in this declaration (Public/Internal) which would than allow you to do something along the lines of this:

richTextBox1.Text = Form2.richTextBox1.Text;

*This assumes your code is in Form1 and your second form is named Form2. Obviously that would need to be changed accordingly.

This is not considered good practice because of potential risks associated with allowing any other section of your program to edit the controls contained on this form. Your ideal solutions for passing information to a form are either via the constructor, a properly scoped method, properly scoped property, or in some cases via an event depending on the type of information and when it can be modified and what it is used for.

Without knowing too many details about your specific case, I would probably recommend using a method because this would be the simplest case.

public void UpdateText(string value) {
   richTextBox1.Text = value;
}

This would allow other forms and controls to call this method which would in turn update the text of the richTextBox.

I will note that none of these examples should be used in production code (I do not show how to handle cross threading issues, or data validation), but I presume from the nature of the question you are doing this more as a learning exercise.

韶华倾负 2024-10-11 08:24:16

如果您想将值移动到新表单,您可以使用新表单的Constructor

public Form2(string text)
{
    textbox1.Text = text;
}

如果您希望新表单将值返回到当前表单,您可以使用Property新形式:

public string Value
{
    get
    {
        return textbox1.Text;
    }
}

正在使用:

using (var instance = new Form2())
{
    instance.Show();
    returnValue = instance.Value;
}

If you want to move a value to new Form you can use Constructor of new form:

public Form2(string text)
{
    textbox1.Text = text;
}

and if you want that new form return a value to current form you can use Property in new form:

public string Value
{
    get
    {
        return textbox1.Text;
    }
}

and in use:

using (var instance = new Form2())
{
    instance.Show();
    returnValue = instance.Value;
}
带上头具痛哭 2024-10-11 08:24:16

我假设您是从主表单调用新表单。在这种情况下,您可以使用发送者来获取实例,或者在您的表单上,如果另一个表单是另一个子表单,您可以通过 Parent 属性及其子表单引用另一个表单。

I assume that you are calling your new form from the main form. In this case you can either use the sender to get the instance or on your form you can refer to the other form via the Parent property and its children if the other form is another child.

多彩岁月 2024-10-11 08:24:16

像这样:

richTextBox1.Text = richTextBox2.Text;

如果你想附加文本,那么:

richTextBox1.Text += richTextBox2.Text;

Like this:

richTextBox1.Text = richTextBox2.Text;

And if you want to append text then:

richTextBox1.Text += richTextBox2.Text;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文