Windows窗口程序设计入门(C#版)

这次用C#写个简单的窗口程序,这篇文章完全是面向新手的。

我简单说明一下我们要实现的功能:

有两个窗体Form1和Form2,这两个窗体里面都有一个TextBox和一个Button。

①当单击Form1里面的Button时,加载出Form2,同时Form2里的TextBox内容和Form1里的TextBox的内容一致;

②当单击Form2里面的Button时,销毁Form2,同时Form1里的TextBox内容变成Form2里TextBox的内容。

我们启动VS,并绘制两个如下图所示的窗体

绘制完成后,我们来到Form1.cs中,右击TextBox选择属性

在弹出的窗口中将其Modifiers属性设置为public

完成后也要将Form2的TextBox也进行同样的设置。只有这样两个窗体中的TextBox才可以相互“看得见”。

设置完成后,我们便可以进行我们的代码设计了!

首先我们来到Form1.cs中,双击Button1,进入代码设计窗口,我们可以看到VS已经帮我们把框架搭好了,直接上手就好喽!

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

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form_two = new Form2();
            //实例化Form2
            form_two.Show();
            //将实例化的对象显示出来
            form_two.textBox1.Text = this.textBox1.Text;
            //将Form1里TextBox的内容赋值给Form2里TextBox
        }
    }
}

这样我们运行一下,发现功能①已经可以实现了!

下面继续实现②中的功能吧,我们选中Form2.cs,双击Button1,进入代码设计页面

首次我们要先声明一个Form1的对象

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 WindowsFormsApplicationTest
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        public Form1 form_one;
        //声明Form类变量
        private void button1_Click(object sender, EventArgs e)
        {
        
        }
    }
}

然后我们要回到Form1的代码设计窗口,为form_one赋值。

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

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form_two = new Form2();
            //实例化Form2
            form_two.Show();
            //将实例化的对象显示出来
            form_two.textBox1.Text = this.textBox1.Text;
            //将Form1里TextBox的内容赋值给Form2里TextBox
            form_two.form_one = this;
            //将form_one指向Form1窗口
        }
    }
}

然后我们再次回到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 WindowsFormsApplicationTest
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        public Form1 form_one;
        //声明Form类变量
        private void button1_Click(object sender, EventArgs e)
        {
            form_one.textBox1.Text = this.textBox1.Text;
            //将Form2里TextBox的内容赋值给Form1里TextBox
            this.Close();
            //将Form2销毁
        }
    }
}

运行一下,会发现我们的功能已经全部实现了!


喜欢的话,关注一下吧!!!

  • 23
    点赞
  • 119
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Windows程序设计入门是指学习如何使用Windows操作系统中的API和工具开发应用程序的过程。以下是一些入门的步骤: 1. 学习编程语言:Windows程序可以使用多种编程语言编写,例如C++、C#、Visual Basic等。因此,学习一种编程语言是必须的。推荐优秀的编程书籍,例如《C++ Primer》、《C#入门经典》等。 2. 熟悉Windows API:Windows API是Windows操作系统提供的一组函数调用,用于访问系统资源和执行操作。学习Windows API的使用是Windows程序设计的核心。Microsoft的官方文档提供了详细的API参考和示例代码,可以帮助开发者快速上手。 3. 使用集成开发环境(IDE):IDE是一种软件工具,可以帮助开发者编写、调试和测试程序。Windows程序设计中常用的IDE有Visual Studio、Code::Blocks、Dev-C++等。 4. 编写简单的应用程序:通过编写简单的应用程序,例如控制台程序、窗口程序等,来熟悉Windows API的使用和程序开发流程。 5. 学习GUI编程:GUI(图形用户界面)是Windows程序的重要组成部分。学习如何使用Windows API或GUI库(例如MFC、WinForms、WPF等)来设计和开发GUI是Windows程序设计的重要步骤。 6. 学习调试和测试:调试和测试是程序开发不可避免的环节。学习如何使用IDE提供的调试工具和测试框架,以及如何编写测试用例和进行单元测试等,可以提高程序的质量和稳定性。 总之,Windows程序设计入门需要学习编程语言、Windows API、GUI编程以及调试和测试等方面的知识。通过不断练习和实践,可以逐渐掌握Windows程序设计的技巧和方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

道人禅(armey)

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

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

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

打赏作者

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

抵扣说明:

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

余额充值