在 C# 中将文本从一个表单 RichtextBox 移动到另一个表单上的另一个表单
我正在制作一个程序,允许用户通过单独的表单添加注释,以便他们能够将以该表单编写的文本添加回原始表单。目前,我无法将以一种形式的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的问题很可能是由于 richtextbox 控件的范围造成的。默认情况下,在 WinForms 中,当您在窗体中创建控件时,它的范围为 Private。这意味着该控件将无法从另一个窗体的代码直接访问。有几种方法可以解决这个问题。最简单的方法是在此声明中将其更改为更广泛的范围(公共/内部),这将允许您执行以下操作:
*这假设您的代码位于 Form1 中,而您的第二个表单名为 Form2。显然,这需要进行相应的改变。
这不被认为是好的做法,因为允许程序的任何其他部分编辑此表单中包含的控件存在潜在风险。将信息传递到表单的理想解决方案是通过构造函数、适当范围的方法、适当范围的属性,或者在某些情况下通过事件,具体取决于信息的类型、何时可以修改以及其用途。
在不了解有关您的具体情况的太多细节的情况下,我可能会建议使用一种方法,因为这将是最简单的情况。
这将允许其他窗体和控件调用此方法,从而更新 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:
*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.
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.
如果您想将值移动到新表单,您可以使用新表单的
Constructor
:如果您希望新表单将值返回到当前表单,您可以使用
Property
新形式:正在使用:
If you want to move a value to new Form you can use
Constructor
of new form:and if you want that new form return a value to current form you can use
Property
in new form:and in use:
我假设您是从主表单调用新表单。在这种情况下,您可以使用发送者来获取实例,或者在您的表单上,如果另一个表单是另一个子表单,您可以通过 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.
像这样:
如果你想附加文本,那么:
Like this:
And if you want to append text then: