C#开发之——Button(12.9)

一 概述

  • 按钮主要用于提交页面的内容,或者是确认某种操作等

  • 按钮包括普通的按钮(Button)、单选按钮(RadioButton),本节主要讲解按钮(Button)

  • 按钮常用的属性包括在按钮中显示的文字(Text)以及按钮外观设置的属性,最常用的事件是单击事件

<!--more-->

二 实例  实现一个简单的用户注册功能,并将提交的注册信息显示在新窗体的文本框中 

2.1 功能分析

本例的用户注册界面中仅包括用户名和密码,通过单击“注册”按钮跳转到新窗体中并显示注册的用户名和密码,实现该功能分别使用 RegForm 窗体和 MainForm 窗体

2.2 界面布局及代码逻辑

2.2.1 RegForm

布局页面

 

代码逻辑

<span style="color:#333333">public partial class RegForm : Form
{
    public RegForm()
    {
        InitializeComponent();
    }
    //“确定”按钮的单击事件,用于判断注册信息并跳转到新窗口显示注册信息
    private void button1_Click(object sender, EventArgs e)
    {
        string name = textBox1.Text;
        string pwd = textBox2.Text;
        string repwd = textBox3.Text;
        if (string.IsNullOrEmpty(name))
        {
            MessageBox.Show("用户名不能为空!");
            return;
        }
        else if (string.IsNullOrEmpty(textBox2.Text))
        {
            MessageBox.Show("密码不能为空!");
            return;
        }
        else if (!textBox2.Text.Equals(textBox3.Text))
        {
            MessageBox.Show("两次输入的密码不一致!");
            return;
        }
        //将用户名和密码传递到主窗体中
        MainForm mainForm = new MainForm(name, pwd);
        mainForm.Show();
    }
    //“取消”按钮的事件,用于关闭窗体
    private void button2_Click(object sender, EventArgs e)
    {
        //关闭窗体
        this.Close();
    }
}</span>

2.2.2 MainForm

布局页面

 

代码逻辑

<span style="color:#333333">public partial class MainForm : Form
{
    public MainForm(string name,string pwd)
    {
        InitializeComponent();
        label2.Text = "用户名:"+ name;
        label3.Text = "密  码:"+pwd;
    }
}</span>

2.3 效果图

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.Drawing.Drawing2D; namespace KRT.Component.Common { /// /// 停靠面板控件 /// public class DockPanel : Panel { /// /// 关闭按钮 /// DockPanelCloseButton closeButton = new DockPanelCloseButton(); /// /// 浮动窗口 /// Form flowForm = new Form(); /// /// 分割栏 /// Splitter splitter = new Splitter(); /// /// 标题栏 /// Label titleBar = new Label(); /// /// 标题栏背景色 /// public Color TitleBackColor //using System.Drawing; { get { return titleBar.BackColor; } set { titleBar.BackColor = value; closeButton.BackColor = value; } } /// /// 标题栏前景色 /// public Color TitleForeColor { get { return titleBar.ForeColor; } set { titleBar.ForeColor = value; } } /// /// 标题栏文本 /// [Localizable(true)] public string TitleText { get { return titleBar.Text; } set { titleBar.Text = value; } } /// /// 标题栏可见 /// public bool TitleVisible { get { return titleBar.Visible; } set { titleBar.Visible = value; closeButton.Visible = value; } } /// /// 浮动窗口最大尺寸 /// Size flowFormMaxSize = new Size(640, 480); public Size FlowFormMaxSize { get { return flowFormMaxSize; } set { flowFormMaxSize = value; if (this.VisibleAll) { flowForm.Size = flowFormMaxSize; } } } /// /// 整个控件的隐藏、显示,包括浮动窗口状态下 /// bool visibleAll = true; public bool VisibleAll { get { return visibleAll; } set { visibleAll = value; if (flowForm.Controls.Count > 0) { flowForm.Visible = visibleAll; } else { this.Visible = visibleAll; } } } /// /// 控件初始化 /// public DockPanel() { //InitializeComponent(); flowForm.ShowInTaskbar = false; // 关闭按钮 closeButton.Location = new System.Drawing.Point(0, 0); closeButton.Name = "Close"; closeButton.Size = new System.Drawing.Size(16, 16); closeButton.TabIndex = 0; closeButton.BackColor = SystemColors.ControlDark; closeButton.Click += new EventHandler(closeButton_Click); // 浮动窗口 flowForm.FormBorderStyle = FormBorderStyle.SizableToolWindow; //VS2008自带的窗体设计器进行设置窗体的边框。 flowForm.ShowInTaskbar = false; //任务栏 flowForm.MaximizeBox = false; //最大化按钮 flowForm.Move += new EventHandler(flowForm_Move); //EventHandler是asp.net内置的委托,事件是特殊的委托。 flowForm.MouseCaptureChanged += new EventHandler(flowForm_MouseCaptureChanged); // flowForm.FormClosing += new FormClosingEventHandler(flowForm_FormClosing); // 标题栏 titleBar.AutoSize = false; //自动调整大小 titleBar.Dock = DockStyle.Top;//控件顶端;DockStyle是个枚举,有none, top, bottom... titleBar.Height = 18; titleBar.Padding = new Padding(3); titleBar.MouseDown += new MouseEventHandler(titleBar_MouseDown); titleBar.MouseMove += new MouseEventHandler(titleBar_MouseMove); titleBar.MouseUp += new MouseEventHandler(titleBar_MouseUp); //this.Padding = new Padding(1); this.Controls.Add(closeButton); this.Controls.Add(titleBar); this.ParentChanged += new EventHandler(DockPanel_ParentChanged); this.VisibleChanged += new EventHandler(DockPanel_VisibleChanged); this.MouseDown += new MouseEventHandler(titleBar_MouseDown); this.MouseMove += new MouseEventHandler(titleBar_MouseMove); this.MouseUp += new MouseEventHandler(titleBar_MouseUp); this.ControlAdded += new ControlEventHandler(DockPanel_ControlAdded); } /// /// 如果是用户单击浮动窗口的关闭按钮,隐藏浮动窗口 /// /// /// void flowForm_FormClosing(object sender, FormClosingEventArgs e) { if (e.CloseReason == CloseReason.UserClosing) { e.Cancel = true; flowForm.Visible = false; visibleAll = false; closeButton_Click(sender, e); } } /// /// 添加子控件时调整顺序 /// /// /// void DockPanel_ControlAdded(object sender, ControlEventArgs e) { if (e.Control != titleBar && e.Control != closeButton) { e.Control.BringToFront(); } } /// /// 同时修改Splitter的可视效果 /// /// /// void DockPanel_VisibleChanged(object sender, EventArgs e) { splitter.Visible = Visible; if (this.Parent != null && Visible) { if (!this.DesignMode) { if (this.Parent.Controls.IndexOf(splitter) < 0) { //splitter.Dock = this.Dock; //this.Parent.Controls.AddRange(new Control[] { splitter, this }); this.Parent.Controls.Add(splitter); int index = this.Parent.Controls.IndexOf(this); this.Parent.Controls.SetChildIndex(splitter, index); //this.Parent.Controls.SetChildIndex(this, index + 1); //foreach (Control item in this.Parent.Controls) //{ // Debug.WriteLine( // this.Parent.Controls.GetChildIndex(item).ToString() + " : " + // item.ToString()); //} } } } } /// /// 初始化Splitter /// /// /// void DockPanel_ParentChanged(object sender, EventArgs e) { //if (this.Parent != null) //{ // if (!this.DesignMode) // { // //Parent.Controls.Add(splitter); // splitter.Dock = this.Dock; // this.Parent.Controls.AddRange(new Control[] { splitter, this }); // } //} } /// /// 关闭事件 /// public event EventHandler CloseButtonClick; /// /// 单击关闭按钮,触发关闭事件 /// /// /// void closeButton_Click(object sender, EventArgs e) { VisibleAll = false; if (null != CloseButtonClick) { CloseButtonClick(sender, e); } } /// /// 浮动窗口开始拖动时触发 /// /// /// void flowForm_MouseCaptureChanged(object sender, EventArgs e) { if (!flowFormCapture) { flowFormCapture = true; } else { flowFormCapture = false; flowFormDock = true; } } bool flowFormDock = false; // 浮动窗口可以被停靠 bool flowFormCapture = false; // 浮动窗口开始拖动 /// /// 浮动窗口移动过程 /// /// /// void flowForm_Move(object sender, EventArgs e) { if (flowFormDock) { flowFormDock = false; switch (this.Dock) { case DockStyle.Left: if (flowForm.Location.X < Parent.Location.X) { ShowDockPanel(); } break; case DockStyle.Top: if (flowForm.Location.Y Parent.Location.X + Parent.Width) { ShowDockPanel(); } break; case DockStyle.Bottom: if (flowForm.Location.Y + flowForm.Height > Parent.Location.Y + Parent.Height) { ShowDockPanel(); } break; } } } /// /// 控件尺寸改变 /// /// protected override void OnSizeChanged(EventArgs e) { base.OnSizeChanged(e); closeButton.Location = new Point( ClientRectangle.Right - closeButton.Width - 1, 1); closeButton.Refresh(); } Point oldMouseLocation; // 鼠标位置 Rectangle rect; // 面板区域 bool mouseDown = false; // 鼠标按下 /// /// 鼠标按下事件 /// /// /// void titleBar_MouseDown(object sender, MouseEventArgs e) { if (this.Dock != DockStyle.None && this.Dock != DockStyle.Fill) { oldMouseLocation = e.Location; mouseDown = true; rect = this.RectangleToScreen(ClientRectangle); ControlPaint.DrawReversibleFrame(rect, Color.Black, FrameStyle.Thick); } } /// /// 鼠标移动事件 /// /// /// void titleBar_MouseMove(object sender, MouseEventArgs e) { if (mouseDown) { ControlPaint.DrawReversibleFrame(rect, Color.Black, FrameStyle.Thick); rect.Offset(e.X - oldMouseLocation.X, e.Y - oldMouseLocation.Y); oldMouseLocation = e.Location; ControlPaint.DrawReversibleFrame(rect, Color.Black, FrameStyle.Thick); } } /// /// 鼠标弹起事件 /// /// /// void titleBar_MouseUp(object sender, MouseEventArgs e) { if (mouseDown) { mouseDown = false; ControlPaint.DrawReversibleFrame(rect, Color.Black, FrameStyle.Thick); Rectangle rc = this.RectangleToScreen(ClientRectangle); Point pt = this.PointToScreen(e.Location); if (!rc.Contains(pt)) { ShowFlowForm(); } } } //protected override void OnPaint(PaintEventArgs e) //{ // base.OnPaint(e); // Pen borderPen = new Pen(borderColor); // Rectangle rc = new Rectangle( // e.ClipRectangle.Left, e.ClipRectangle.Top, // e.ClipRectangle.Right - 1, e.ClipRectangle.Bottom - 1); // e.Graphics.DrawRectangle(borderPen, rc); //} /// /// 显示浮动窗口 /// void ShowFlowForm() { flowForm.Show(Parent); flowForm.Location = new Point(rect.Left, rect.Top); Size newSize = new Size(rect.Width, rect.Height); if (newSize.Width > FlowFormMaxSize.Width) { newSize.Width = FlowFormMaxSize.Width; } newSize.Height = newSize.Height + (this.TitleVisible ? SystemInformation.CaptionHeight - this.titleBar.Height : SystemInformation.CaptionHeight); if (newSize.Height > FlowFormMaxSize.Height) { newSize.Height = FlowFormMaxSize.Height; } flowForm.Size = newSize; flowForm.Text = TitleText; while (this.Controls.Count > 2) { for (int i = 0; i < this.Controls.Count; i++) { if (this.Controls[i] != closeButton && this.Controls[i] != titleBar) { flowForm.Controls.Add(this.Controls[i]); break; } } } this.Visible = false; } /// /// 显示停靠面板 /// void ShowDockPanel() { this.Visible = true; while (flowForm.Controls.Count > 0) { this.Controls.Add(flowForm.Controls[0]); } flowForm.Hide(); } } }
原型模式是一种创建型设计模式,其提供了一种复制已有对象的方法来生成新对象的能力,而不必通过实例化的方式来创建对象。原型模式是通过克隆(浅复制或深复制)已有对象来创建新对象的,从而可以避免对象创建时的复杂过程。 在C#中,可以通过实现ICloneable接口来实现原型模式。ICloneable接口定义了Clone方法,该方法用于复制当前对象并返回一个新对象。需要注意的是,Clone方法返回的是Object类型,需要进行强制类型转换才能得到复制后的对象。 以下是一个简单的示例代码: ```csharp public class Person : ICloneable { public string Name { get; set; } public int Age { get; set; } public object Clone() { return MemberwiseClone(); } } // 使用示例 var person1 = new Person { Name = "Tom", Age = 20 }; var person2 = (Person)person1.Clone(); person2.Name = "Jerry"; Console.WriteLine(person1.Name); // 输出 "Tom" Console.WriteLine(person2.Name); // 输出 "Jerry" ``` 在上面的示例代码中,实现了一个Person类,并实现了ICloneable接口中的Clone方法来实现原型模式。复制对象时,使用MemberwiseClone方法进行浅复制,即只复制值类型的字段和引用类型字段的引用,而不复制引用类型字段所引用的对象。在使用示例中,首先创建一个Person对象person1,然后通过Clone方法复制一个新的对象person2,修改person2的Name属性后,输出person1和person2的Name属性,可以看到person1的Name属性并没有改变,说明person2是一个全新的对象。 需要注意的是,如果要实现深复制,即复制引用类型字段所引用的对象,需要在Clone方法中手动将引用类型字段复制一份。另外,使用原型模式时,需要注意复制后的对象和原对象之间的关系,如果复制后的对象修改了原对象的状态,可能会对系统产生意想不到的影响。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值