• 窗体之间的交互


    实验内容:

    写一个注册窗口,然后在另一个窗体中显示注册内容。

     先写个注册窗体:7个lable,5个textbox,1个combobox,1个picturebox,3个button和1个openfiledialog

    按下图进行摆放

    Form1.cs 代码如下:

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

    namespace 实验1
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button3_Click(object sender, EventArgs e)
    {
    DialogResult result = textureFileOpen.ShowDialog();
    if(result==DialogResult.OK)
    {
    string fileName = textureFileOpen.FileName;
    pictureBox1.Load(fileName);
    button3.Text = "更改头像";
    }
    }

    private void button2_Click(object sender, EventArgs e)
    {

    /////    清空数据      /////////
    textBox1.Text = null;
    textBox2.Text = null;
    textBox3.Text = null;
    textBox4.Text = null;
    textBox5.Text = null;
    comboBox1.Text = null;
    pictureBox1.Image = null;
    button3.Text = "上传头像";
    }

    private void button1_Click(object sender, EventArgs e)
    {

    //////////安全性检查///////////////
    if (textBox1.Text == "" || textBox1.Text == "" || textBox1.Text == "" || textBox1.Text == ""||comboBox1.Text=="")
    {
    MessageBox.Show("你还有内容没填好!","警告:",MessageBoxButtons.OKCancel);
    }
    else if(textBox2.Text!=textBox3.Text)
    {
    MessageBox.Show("密码不一致,请再次确定!","提示:",MessageBoxButtons.OK);
    }else
    {

    Form2 f2= new Form2();
    f2.Owner = this;
    f2.Show();
    }
    }

    private void textBox3_TextChanged(object sender, EventArgs e)
    {
    label8.Text = "显示密码";
    }

    private void label8_Click(object sender, EventArgs e)
    {

    if(label8.Text == "显示密码")
    {
    textBox3.UseSystemPasswordChar = false;
    textBox2.UseSystemPasswordChar = false;
    label8.Text = "隐藏密码";

    }
    else if (label8.Text == "隐藏密码")
    {
    label8.Text = "显示密码";
    textBox2.UseSystemPasswordChar = true;
    textBox3.UseSystemPasswordChar = true;
    }

    }

    private void button4_Click(object sender, EventArgs e)
    {
    DialogResult result = MessageBox.Show("你确定要退出吗?","提示:",MessageBoxButtons.OKCancel);
    if(result==DialogResult.OK)
    {
    this.Close();
    }

    }

    private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
    {

    ///////控制只能输入数值 8是backspace键的ASII码//////////
    if (!(Char.IsNumber(e.KeyChar))&&e.KeyChar!=(char)8)
    {
    e.Handled = true;
    }
    }
    }
    }

    注意:

    1.picturebox有个sizemode属性,该属性是控制图像的显示:

    Normal:表示图像被置于picturebox的左上角如果图像比它包含的picturebox大,则该图像将被剪裁掉.

    stretchImage:表示picturebox中的图像被拉伸或收缩,以适合picturebox的大小

    Autosize:表示调整picturebox大小,使其等于所包含图像的大小。

    CenterImage:如果picturebox比图像大,则图像将局中显示,如果picturebox比图像小,则将图像居于picturebox 的中心,而边缘将被剪裁掉

    zoom:表示图像大小按原有的大小比例增加或减小.

    2.在Form的属性中有canclebutton这个属性,当该属性为(无)时,按下Esc键可退出程序.

    另一个窗体form2:

    10个lable,1个picturebox,1个textbox,1个button

    如下图摆放:

     Form2代码:

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

    namespace 实验1
    {
    public partial class Form2 : Form
    {
    public Form2()
    {
    InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    { //把窗体1的控件实例化传递
    TextBox Tex_0 = Application.OpenForms["Form1"].Controls["textBox1"] as TextBox;  
    TextBox Tex_1 = Application.OpenForms["Form1"].Controls["textBox4"] as TextBox;
    TextBox Tex_2 = Application.OpenForms["Form1"].Controls["textBox5"] as TextBox;
    ComboBox Tex_3 = Application.OpenForms["Form1"].Controls["comboBox1"] as ComboBox;
    PictureBox pic = Application.OpenForms["Form1"].Controls["pictureBox1"] as PictureBox;

    //信息的传递
    if(Tex_0!=null)
    {
    label9.Text = "尊敬的" + Tex_0.Text + "你好,请核对一下以下信息!";
    label2.Text = Tex_0.Text;
    }
    if (Tex_1!= null)
    {
    label5.Text = Tex_1.Text;
    }
    if (Tex_2 != null)
    {
    textBox.Text = Tex_2.Text;
    }
    if (Tex_3 != null)
    {
    label3.Text = Tex_3.Text;
    }
    if(pic!=null)
    {
    pictureBox1.Image = pic.Image;
    }
    }

    private void textBox5_TextChanged(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
    this.Close();
    }
    }
    }

     注意:

    1.textbox的readonly属性设置成true(只能读取)

    2. textbox的Scrollbars设置成vertical(垂直滚动条)【Horizontal(水平滚动条),both(水平加垂直滚动条)】

  • 相关阅读:
    【SignalR学习系列】3. SignalR实时高刷新率程序
    【SignalR学习系列】4. SignalR广播程序
    【SignalR学习系列】5. SignalR WPF程序
    python gb2312 转换为 utf-8
    爬虫 需要什么样的 CPU,内存 和带宽
    TypeError: sequence item 0: expected string, Tag found
    MySQL 数据的 截取,数据清洗
    MySQL (1366, "Incorrect string value: '\xF0\x9F\x8E\xAC\xE5\x89...' for column 'description' at row 1")
    微博爬虫 ----- 微博发布时间清洗
    ReferenceError: weakly-referenced object no longer exists Python kafka
  • 原文地址:https://www.cnblogs.com/lichuangblog/p/6073428.html
Copyright © 2020-2023  润新知