学习型 ASP/PHP/ASP.NET 主机 30元/年全能 ASP/PHP/ASP.NET 主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付
轻松建立自己的群组,招兵买马   
发新话题
打印

子表单如果控制主表单的控件?

子表单如果控制主表单的控件?

初学,请大家指点,代码如下:

想实现在form2上用一个button修改form1上的一个textbox1的text值。谢谢
表单form1代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {

        }

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

表单form2代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace mycon
{
    public partial class Form2 : Form
    {
        public Form1 pParent = null;
        public Form2(Form1 main)
        {
            InitializeComponent();
            pParent = main;
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.pParent.Text = "4344343";
            //以下不能访问具体控件,不知道怎么弄了

           // this.pParent.textbox1.text = "要修改这儿";

        }
    }
}

TOP

可以用构造函数传值过来
form1单机事件
private void button1_Click(object sender, EventArgs e)
        {
            Form2  form2 = new Form2 (textbox1);
            form2.Show();
        }
form2 要下面这样
textbox te;

public Form2(textbox t)
        {
            InitializeComponent();
          te=t;
        }






private void button1_Click(object sender, EventArgs e)
        {
           te=这里是你要赋的值 ;
this.close();
这样就可以传给form1的text1.text

        }

TOP

再问一下,如果我的主表单form1是的控件是一个tabcontrol控件,我在form2中的一个按钮中希望关闭form1上的tabcontrol上的一个页或对tabcontrol进行其他操作,应该如何做,这里可不能传值了。
我现在是找不到这个控件。无法对它操作。

TOP

发新话题