窗体实验之文本框实例

实验要求:
1.单击按钮能将文本框复制内容传给标签
2.标签字体16,红色

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

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Font = new System.Drawing.Font("宋体",16);
            label1.ForeColor = Color.Red;
        }

        private void label1_Click(object sender, EventArgs e)
        {
            button1.Text = "复制选择文本至标签";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = textBox1.SelectedText;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是示例代码实现: 在1中添加一个按钮和一个文本框: ```C# public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 form2 = new Form2(); // 创建2的实例 form2.SetText(textBox1.Text); // 调用2的方法,将1的文本框的值传递过去 form2.ShowDialog(); // 显示2 textBox1.Text = form2.GetText(); // 获取2文本框的值,并赋值给1的文本框 } } ``` 在2中添加一个文本框和一个按钮: ```C# public partial class Form2 : Form { public delegate void SetTextDelegate(string text); // 委托类型,用于传递1的文本框的值 public delegate string GetTextDelegate(); // 委托类型,用于传递2文本框的值 private SetTextDelegate setTextDelegate; // 声明委托变量,用于传递1的文本框的值 private GetTextDelegate getTextDelegate; // 声明委托变量,用于传递2文本框的值 public Form2() { InitializeComponent(); } public void SetText(string text) { setTextDelegate = new SetTextDelegate(SetTextMethod); // 创建委托实例 this.Invoke(setTextDelegate, text); // 调用委托方法,将1的文本框的值传递过来 } public string GetText() { getTextDelegate = new GetTextDelegate(GetTextMethod); // 创建委托实例 return (string)this.Invoke(getTextDelegate); // 调用委托方法,获取2文本框的值并返回 } private void button1_Click(object sender, EventArgs e) { this.Close(); // 关闭2 } private void SetTextMethod(string text) { textBox1.Text = text; // 将1的文本框的值赋值给2的文本框 } private string GetTextMethod() { return textBox1.Text; // 获取2的文本框的值并返回 } } ``` 运行程序后,单击1中的按钮,会弹出2,2的文本框中会显示1中的文本框的值。在2中单击关闭按钮,会将2的文本框的值赋值给1的文本框中,并关闭2。最终效果如下图所示: ![C#委托实现文本框值传递](https://img-blog.csdn.net/20180321231017792?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmFpZHVhYnNlbnRlcnJhZGlvbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/85)

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值