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

             }

         }

    }

  • 相关阅读:
    File Size(4.12)
    Ownership of New Files and Directories(4.6)
    Changing User IDs and Group IDs & How saved setuserid works (8.11)
    Windows 下硬盘安装linux 系统
    access Function(4.7)
    File Access Permissions(4.5)
    link, unlink, remove, and rename Functions(4.15)
    文件特殊权限: SUID, SGID, SBIT
    ASP.NET与DreamweaverMx结合
    sqlserver的几个函数要记录
  • 原文地址:https://www.cnblogs.com/liehuo123/p/5562201.html
Copyright © 2011-2022 走看看