C# .NET 中的表单

发布于 2024-09-26 09:01:43 字数 112 浏览 3 评论 0原文

我有 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 技术交流群。

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

发布评论

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

评论(4

哎呦我呸! 2024-10-03 09:01:43

根据我的理解,您需要以另一种形式访问一种形式的数据。可能还有其他方法可以做到这一点,其中之一是您可以创建一个事件并创建一个事件处理程序,您可以在其中放置代码来写入文件。这可能会有所帮助。

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.

卖梦商人 2024-10-03 09:01:43

您可以使用文本框的 .Text 属性来获取在文本框中键入的文本。

例如

TextBox1.Text

有很多方法可以写入文本文件,所以我只给出 1 种方法。

// Compose a string that consists of three lines.
string myText= TextBox1.Text;

// Write the string to a file.
System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.txt");
file.WriteLine(myText);

file.Close();

摘自: 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.

// Compose a string that consists of three lines.
string myText= TextBox1.Text;

// Write the string to a file.
System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.txt");
file.WriteLine(myText);

file.Close();

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?

荒芜了季节 2024-10-03 09:01:43

将 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).

情痴 2024-10-03 09:01:43

要扩展 Ranhiru 的答案,最好放置 StreamWriter在 using 语句中创建,因为它是非托管资源:

// Attempt to write to the file
try
{
    // Compose a string that consists of three lines.
    string myText= TextBox1.Text;

    // Write the string to a file.
    using(System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.txt"));
    {
        file.WriteLine(lines);
    }
}
catch(IOException exception) //Catch and display exception
{
     MessageBox.Show(exception.Message());
}

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:

// Attempt to write to the file
try
{
    // Compose a string that consists of three lines.
    string myText= TextBox1.Text;

    // Write the string to a file.
    using(System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.txt"));
    {
        file.WriteLine(lines);
    }
}
catch(IOException exception) //Catch and display exception
{
     MessageBox.Show(exception.Message());
}

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

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