保存文本框的代码 [英] save the code of textbox

查看:65
本文介绍了保存文本框的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须找到代码  如何保存Form2中的按钮文件框(它属于Form1)的上下文?你能帮帮我吗?

解决方案

您好,



  • 在form1上选择有问题的TextBox。
  • 选择属性,选择   修饰符和从私有更改为公共。



  • 创建form2,将所有者设置为form1(使用'this')。

使用System; 
使用System.Windows.Forms;

命名空间WindowsFormsApp3
{
公共部分类Form1:表格
{
public Form1()
{
InitializeComponent() ;
}

private void button1_Click(object sender,EventArgs e)
{
var f = new Form2();
f.Show(this);
}
}
}

然后在form2中将form1上的textbox1的内容写入文件,在这种情况下我写入与应用程序相同的文件夹中的文件。

使用System; 
使用System.IO;
使用System.Windows.Forms;

命名空间WindowsFormsApp3
{
public partial class Form2:Form
{
public Form2()
{
InitializeComponent() ;
}

private void button1_Click(object sender,EventArgs e)
{
File.WriteAllText(Path.Combine(
AppDomain.CurrentDomain.BaseDirectory, "MyFile.txt",((Form1)Owner).textBox1.Text);
}
}
}

关键部分,设置所有者,如第一个代码块所示。通过&(;(Form1)所有者)引用文本框





I have to find the code how to save the context of a textbox(it belongs to Form1) in a file with button in a Form2?Could you help me?

解决方案

Hello,

  • Select the TextBox in question on form1.
  • Select properties, select   modifiers and change from private to public.

  • Create form2, set the owner to form1 (using 'this').

using System;
using System.Windows.Forms;

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var f = new Form2();
            f.Show(this);
        }
    }
}

Then in form2 write the contents of textbox1 on form1 to a file, in this case I write to a file in the same folder as the app.

using System;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApp3
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            File.WriteAllText(Path.Combine(
                AppDomain.CurrentDomain.BaseDirectory,"MyFile.txt"), ((Form1)Owner).textBox1.Text);
        }
    }
}

Key parts, setting the owner as shown in the first code block. Referencing the text box via ((Form1)Owner)


这篇关于保存文本框的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆