简体   繁体   English

c#从其他形式获取价值

[英]c# getting value from other form

Situation i have: textbox(to input your name) on form1. 情况我有:text1(输入你的名字)在form1上。 From that form1 on button click i go to form2. 从按钮上的那个form1点击我转到form2。 From form2 button click to form3. 从form2按钮单击到form3。 On form3 on button click i need to writte me in empty textbox value from textbox on form1 that user wrote down. 在按钮上的form3上单击我需要在用户写下的form1上的文本框中写入空文本框值。 example: on form1 in textbox1 i write my name "Djuzla". 例如:在textbox1中的form1上,我写下了我的名字“Djuzla”。 When i go to form3 and click button to see what name i wrote in form1 it should show in empty textbox3 form3 "Djuzla". 当我转到form3并单击按钮以查看我在form1中写的名称时,它应显示在空textbox3 form3“Djuzla”中。

I'm stuck with this few hours now, and it stupid problem but i have no idea what to do next.. tried all from zillion theards on net :p 我现在停留了这几个小时,这是一个愚蠢的问题,但我不知道下一步该做什么...在网上尝试了所有来自zillion theards:p

您可以修改表单的构造函数以再接受一个参数来保存textBox的值。

Store the value in a property. 将值存储在属性中。 If you are developing for asp.net, use session or ViewState. 如果您正在为asp.net开发,请使用session或ViewState。 Did I understand your question? 我理解你的问题吗?

您可以从页面访问PreviousPage属性,或设置会话变量(或cookie),然后在页面加载等时检索它,但最直接的解决方案是使用页面上的PreviousPage属性访问上一页的数据。

I've answered similarly... Since you are going from Form 1 to 2 to 3, you could basically take the same approach in this solution to cascade the value out and back as needed, not just one way... 我已经回答了类似的问题......既然你从表格1到2到3,你基本上可以采用相同的方法在这个解决方案中根据需要将值分级并返回,而不仅仅是单向...

My Other Solution to similar question... 我对类似问题的其他解决方案......

The solutions that pass a value around can get quite sloppy because there's no guarantee that the value inside the textbox won't change after you've already passed it somewhere. 传递一个值的解决方案可能会非常草率,因为无法保证文本框中的值在您已经传递到某个地方后不会发生变化。 Instead, it's more sensible to pass a function around which resolves the value from the TextBox when called, which will result in always getting the lastest value. 相反,传递一个函数更为明智,该函数在调用时解析TextBox中的值,这将导致始终获得最新值。

void button1_Click() {
    form2 = new Form2(() => textBoxName.Text);
}

class Form2 : Form {
    ...
    public Form2(Func<String> nameResolver) {
        form3 = new Form3(nameResolver);
    }

    void button1_Click(...) {
       form3.Show()
    }
}

class Form3 : Form {
    Func<String> nameResolver;

    public Form3(Func<string> nameResolver) {
        this.nameResolver = nameResolver;
    }

    void button1_Click(...) {
        this.textBoxName = nameResolver.Invoke();
    }
}

If you end up requiring more than one or two components shared between the various forms, it might be better to simply pass the forms themselves to child forms. 如果您最终需要在各种表单之间共享多于一个或两个组件,那么将表单本身简单地传递给子表单可能更好。 Eg. 例如。

void button1_Click(...) {
    form2 = new Form2(this);
}

class Form2 : Form {
    Form1 parent;
    public Form2(Form1 parent) {
        this.parent = parent;
    }

    void button1_Click(...) {
        form3 = new Form3(parent);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM