Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1754225
  • 博文数量: 787
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 5015
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-22 15:17
文章分类

全部博文(787)

文章存档

2008年(787)

我的朋友

分类:

2008-09-25 16:11:07

  [@more@]

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

namespace 多窗口
{
 ///


 /// Form1 的摘要说明。
 ///

 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Button button1;
  ///
  /// 必需的设计器变量。
  ///

  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  ///


  /// 清理所有正在使用的资源。
  ///

  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  ///


  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  ///

  private void InitializeComponent()
  {
   this.button1 = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(24, 72);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(192, 32);
   this.button1.TabIndex = 0;
   this.button1.Text = "显示子窗体";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(256, 117);
   this.Controls.Add(this.button1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.ResumeLayout(false);

  }
  #endregion

  ///


  /// 应用程序的主入口点。
  ///

  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  //主窗体按钮单击事件
  private void button1_Click(object sender, System.EventArgs e)
  {
      //动态创建一个子窗体
   Form form2=new Form();
   form2.ClientSize=new System.Drawing.Size(300,200);//窗体大小
   form2.BackColor=System.Drawing.Color.Yellow;//背景色
   form2.Text="数据输入窗口";

   //子窗体上的文本标签
   Label label1=new Label();
   label1.Text="输入文字";
   label1.Location=new System.Drawing.Point(24,24);//位置
   label1.Size=new System.Drawing.Size(168,23);//大小
           
   //子窗体上的文本输入框
   TextBox textBox1=new TextBox();
   textBox1.Location=new System.Drawing.Point(80,64);
   textBox1.Size=new System.Drawing.Size(100,20);
   textBox1.Text="初始文字";
            
   //子窗体上的按钮
   Button button2=new Button();
   button2.Location=new System.Drawing.Point(75,150);
   button2.Text="返回";
   button2.BackColor=System.Drawing.Color.Gray;
   button2.Size=new System.Drawing.Size(160,23);
   button2.DialogResult=System.Windows.Forms.DialogResult.OK;//点击按钮后的返回状态

   form2.Controls.Add(label1);//添加文本标签到子窗体
   form2.Controls.Add(textBox1);//添加文本输入框到子窗体
   form2.Controls.Add(button2);//添加按钮到子窗体
   DialogResult res=form2.ShowDialog();
   //当点击了子窗体上的按钮后
   if(res==System.Windows.Forms.DialogResult.OK)
   {
    //主窗体上按钮的文字变成子窗体上文本输入框里的文字
    button1.Text=textBox1.Text;
    //子窗体关闭
    form2.Close();
   }
  }
 }
}



--------------------next---------------------

阅读(949) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~