C#中windows应用窗体程序的输入输出方法实例

绪论

    C#的应用窗体程序同样可以看成控制台应用程序的操作,只是核心思想变了,窗体程序讲究“事件机制的编程”也就是模块化的操作,有可视化的操作方便程序员写代码;本文内容主要介绍窗体应用程序常用的输入输出+操作模块+学习感悟。

一.输出操作:

  • Label控件
    我们所说的控件其实可以理解成组件的意思!也就是封装好的一些功能原件!
    在这里插入图片描述
    找到一个控件我们需要注意两点要求,那就是对我们的控件进行三剑客操作!!!三剑客是我自己总结的,那便是:
  • 属性
  • 属性中的文本
  • 方法【事件】
    命名label控件为:lblShow
    在这里插入图片描述
    我们利用代码便可完成一个简单的输出:
lblShow.Text="hello world";

二.输入操作

  • TextBox控件
    在这里插入图片描述
    按照【1】中所说,我们同样要修改设置它的属性和方法;
    我们命名textName
    在这里插入图片描述
    那么textName.Text记为用户输入的字符串

三.按钮操作

    按钮操作即为用户行为的描述。触发按钮我们可以将用户输入到文本框textBox里的信息展示到我们的label控件lblShow中;
拖动一个按钮button
在这里插入图片描述
我们同样修改属性+方法:
在这里插入图片描述
在这里插入图片描述
双击进入事件中开始写代码,描述我们要做的行为:
在这里插入图片描述
在这里插入图片描述
我们可以把按钮btNok事件看做一个主函数,当用户点击后,程序先执行该函数里的代码;

四.源代码分享:

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_3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btNok_Click(object sender, EventArgs e)
        {
            String s;//定义一个字符串变量
            s = textName.Text;//把文本框用户输入信息赋给s
            lblShow.Text = s;//再把s赋给输出框
            //打印一下
            Console.WriteLine(lblShow.Text);


        }
    }
}
以下是一个使用C语言编写的窗体应用程序,实现了委托、action、func和多播委托: ```c #include <stdio.h> // 定义委托类型 typedef void (*Delegate)(); // 定义action类型 typedef Delegate Action; // 定义func类型 typedef int (*Func)(); // 委托函数 void myDelegate() { printf("Hello World from delegate!\n"); } // action函数 void myAction(Action action) { printf("Hello World from action!\n"); action(); } // func函数 int myFunc(Func func) { printf("Hello World from func!\n"); return func(); } // 多播委托函数 void myMulticastDelegate(Delegate delegates[], int count) { printf("Hello World from multicast delegate!\n"); for(int i = 0; i < count; i++) { delegates[i](); } } // 引用方法 int add(int a, int b) { return a + b; } int main() { // 定义委托变量 Delegate delegate = myDelegate; // 调用委托函数 delegate(); // 定义action变量 Action action = myDelegate; // 调用action函数 myAction(action); // 定义func变量 Func func = add; // 调用func函数 int result = myFunc(func); printf("The result of add(1, 2) is %d\n", result); // 定义多播委托数组 Delegate delegates[] = {myDelegate, myDelegate}; // 调用多播委托函数 myMulticastDelegate(delegates, 2); return 0; } ``` 运行结果如下: ``` Hello World from delegate! Hello World from action! Hello World from delegate! Hello World from func! The result of add(1, 2) is 3 Hello World from multicast delegate! Hello World from delegate! Hello World from delegate! ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodeLinghu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值