C# .NET 中的表单
我有 2 个文本框 &当我单击 Form1 的按钮时,Form1 上有一个按钮,Form2 上有一个文本框1 我想保存包含在 Form2 的 textbox1 中的文件名以及 Form1 中的文本框信息
I have 2 textbox's & one button on Form1 and one textbox1 on Form2 when I Click on button of Form1 I want to save with file name which contains in textbox1 of Form2 with information of textboxes in Form1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据我的理解,您需要以另一种形式访问一种形式的数据。可能还有其他方法可以做到这一点,其中之一是您可以创建一个事件并创建一个事件处理程序,您可以在其中放置代码来写入文件。这可能会有所帮助。
As per my understanding you need to access data of one form in another form. There may be other ways to do this, one of them is you can make an event and make an event handler where you can put the code to write the file. This may help.
您可以使用文本框的 .Text 属性来获取在文本框中键入的文本。
例如
TextBox1.Text
有很多方法可以写入文本文件,所以我只给出 1 种方法。
摘自: MSDN
所以如果您了解这两种方法,我想剩下的就是你的逻辑了。
您还有什么想要实现的目标吗?
You can use the .Text property of the Text Box to get the text which is in typed in the TextBox.
For example
TextBox1.Text
There are many methods to write to a text file so i'll give only 1 method.
Taken from: MSDN
So if you know about these two methods, I think the rest is your logic.
Is there anything else that you want to achieve?
将 Ranhiru Cooray 的代码(将文本写入文件)放入附加到 按钮的
OnClick
事件(单击按钮时调用)。Put Ranhiru Cooray's code (which writes text into a file) into an event handler attached to the
OnClick
event of the button (which is invoked when the button is clicked).要扩展 Ranhiru 的答案,最好放置 StreamWriter在 using 语句中创建,因为它是非托管资源:
using 语句 确保在 StreamWriter 上调用 Dispose,无论无论成功还是失败。可以在“Finally”块中添加对 Dispose 的调用,但此方法更短、更清晰。
更多信息和完整示例可以从 MSDN 中找到
To extend Ranhiru's answer, its best to put the StreamWriter creation in a using statement due to it being an unmanaged resource:
The using statement ensures that Dispose is called on the StreamWriter regardless of whether it succeeds or fails. It is possible to add the call to Dispose in the "Finally" block but this method is shorter and cleaner.
More information and a complete example can be found from MSDN