zoukankan      html  css  js  c++  java
  • C#操作非本窗体的控件

    在一个窗体中刷新另一个窗体,并改变另一个窗体中的文本框的内容。

    程序运行时,显示的是窗体Form1,上面有一个文本框textBox1和一个命令按钮button1,

    如图:

     

    为工程添加一个窗体Form2,上面只有一个命令按钮button1,如图:

    解决方案浏览器如图:

    单击Form1中的命令按钮,将显示Form2,然后单击Form2上的命令按钮,

    将更新Form1的标题为“你更新了本窗体的标题!”,更新Form1中的文本框的内容为:“你更新了本文本框的内容!”,并刷新Form1。

    如图:

     

    Form1的代码如下:

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

     

    namespace C_Sharp中窗口A如何刷新窗口B

    {

         public partial class Form1 : Form

         {

             public Form1()

             {

                  InitializeComponent();

             }

             private void button1_Click(object sender, EventArgs e)

             {

                  Form2 form2 = new Form2();    //构造form2

                  form2.Owner = this;           //令form2的父窗体为Form1

                  form2.ShowDialog();           //显示form2

             }

         }

    }

    Form2的代码如下:

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

     

    namespace C_Sharp中窗口A如何刷新窗口B

    {

         public partial class Form2 : Form

         {

             public Form2()

             {

                  InitializeComponent();

             }

             private void button1_Click(object sender, EventArgs e)

             {

                  this.Owner.Text = "你更新了本窗体的标题!";       //更新Form1的标题

                  // 更新Form1的文本框

                  this.Owner.Controls["textBox1"].Text = "你更新了本文本框的内容!";

                  this.Owner.Refresh();     //刷新Form1

             }

         }

    }

  • 相关阅读:
    【纯水题】POJ 1852 Ants
    【树形DP】BZOJ 1131 Sta
    【不知道怎么分类】HDU
    【树形DP】CF 1293E Xenon's Attack on the Gangs
    【贪心算法】CF Emergency Evacuation
    【思维】UVA 11300 Spreading the Wealth
    【树形DP】NOI2003 逃学的小孩
    【树形DP】BZOJ 3829 Farmcraft
    【树形DP】JSOI BZOJ4472 salesman
    【迷宫问题】CodeForces 1292A A NEKO's Maze Game
  • 原文地址:https://www.cnblogs.com/liehuo123/p/5562201.html
Copyright © 2011-2022 走看看