如何访问和子窗口在C#中父窗口控制的变化值 [英] How to access and change value of parent window control from child window in C#

查看:198
本文介绍了如何访问和子窗口在C#中父窗口控制的变化值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我怎么能在父窗口的子窗口更改文本框的文本值。



即我父窗口有TextBox1中和按钮,子窗口有TextBox2中和按钮。
我需要当我进入子窗口的TextBox2中一些文本更新TextBox1中的价值。



我做了一些简单的函数来做到这一点逻辑的正确的,但其没有工作,我不知道为什么。



parent.cs

 命名空间digdog 
{
公共部分类的父:表
{
公共父()
{
的InitializeComponent();
}

公共无效changeText(字符串文本)
{
textbox1.Text =文本;
}

私人无效的button1_Click(对象发件人,EventArgs五)
{
//显示模式对话框
孩子的MyForm =新子();
myform.ShowDialog();

}

}
}

child.cs

 命名空间digdog 
{
公共部分子类:表
{

公共子()
{
的InitializeComponent();
}

私人无效的button1_Click(对象发件人,EventArgs五)
{
父mytexts =新的父();
mytexts.changeText(textbox2.Text);
}
}
}



任何想法可以理解
在此先感谢


解决方案

您正在创建另一个父窗口(这是不可见的),并改变其文本。 '真正的'父母需要由孩子来访问。你可以通过上在父母的button1_Click设置孩子的属性做到这一点。



例如




 公共父ParentWindow {;设置得到} $ p> 

在父母的button1_Click

 子的MyForm =新子(); 
child.ParentWindow =这一点;
m.ShowDialog();

在孩子的button1_Click

  ParentWindow.changeText(textbox2.Text)


Hi how can I change text value of text box in parent window from child window..

i.e I have parent window have textbox1 and button and child window has textbox2 and button. I need to update the value of textbox1 when I enter some text in child window's textbox2.

i did some simple function to do this logically its correct but its not working I have no idea why..

parent.cs

namespace digdog
{
    public partial class parent : Form
    {
        public parent()
        {
            InitializeComponent();
        }

        public void changeText(string text)
        {
            textbox1.Text = text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Display modal dialog
            child myform = new child();
            myform.ShowDialog();

        }

    }
}

child.cs

namespace digdog
{
    public partial class child : Form
    {

        public child()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
         parent mytexts = new parent();
         mytexts.changeText(textbox2.Text);
        }
    }
}

any ideas will be appreciated thanks in advance

解决方案

You are creating another 'parent' window (which is not visible) and changing its text. The 'real' parent needs to be accessed by the child. You could do this via a property on the child that is set in the parents button1_click.

e.g.

in child class

public parent ParentWindow {get;set;}

in parent button1_click

child myform = new child();
child.ParentWindow = this;
m.ShowDialog();

in child button1_click

ParentWindow.changeText(textbox2.Text)

这篇关于如何访问和子窗口在C#中父窗口控制的变化值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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