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

             }

         }

    }

  • 相关阅读:
    python 去重
    怎样稳稳获得年化高收益
    module_loader.py
    mac上安装ta-lib
    mac上安装memcache
    创建widget
    smartsvn 用法
    用nifi executescript 生成3小时间隔字符串
    TclError: no display name and no $DISPLAY environment variable
    【C#】详解C#序列化
  • 原文地址:https://www.cnblogs.com/liehuo123/p/5562201.html
Copyright © 2011-2022 走看看