DEV Community

Ma Yiqiao
Ma Yiqiao

Posted on

To-do for Windows 10 & Windows 11(给Windows 10 和 Windows 11的待办)

To-Do

待办

===

步骤概述:


  1. 创建一个新的WinForms项目。
  2. 设计主窗体(Form)以显示任务列表及添加新任务的输入框。
  3. 编写事件处理程序来管理任务的增删改查操作。

4. 使用Visual Studio自带的工具将项目发布为可执行文件 (.exe)。

下面是一个简单的To-Do应用的核心部分——主要窗体的设计与功能实现代码:
Below is the core part of a simple To-Do application - the design and functional implementation code of the main window:

//To-Do
//csharp(C#)
using System;
using System.Windows.Forms;

namespace ToDoApp
{
    public partial class MainForm : Form
    {
        // 构造函数,在窗体初始化时调用
        public MainForm()
        {
            InitializeComponent();

            // 初始化控件属性
            this.Text = "简易To-Do列表";
            this.Width = 400;
            this.Height = 300;

            // 创建并设置按钮
            Button addButton = new Button { Text = "添加", Location = new System.Drawing.Point(50, 200), Width = 80 };
            addButton.Click += AddButton_Click; // 绑定点击事件

            // 创建并设置文本框用于输入任务内容
            TextBox taskInputBox = new TextBox { Location = new System.Drawing.Point(50, 170), Width = 280 };

            // 创建并设置列表框用于显示任务列表
            ListBox tasksListBox = new ListBox { Location = new System.Drawing.Point(50, 30), Size = new System.Drawing.Size(280, 130) };

            // 将控件添加到窗体上
            Controls.Add(addButton);
            Controls.Add(taskInputBox);
            Controls.Add(tasksListBox);

            // 将控件保存为类成员变量以便后续使用
            _addButton = addButton;
            _taskInputBox = taskInputBox;
            _tasksListBox = tasksListBox;
        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            string taskText = _taskInputBox.Text.Trim(); // 获取并清理输入的任务文本

            if (!string.IsNullOrEmpty(taskText))
            {
                _tasksListBox.Items.Add(taskText); // 将任务添加到列表中
                _taskInputBox.Clear(); // 清空输入框
            }
        }

        private Button _addButton; // 添加任务按钮
        private TextBox _taskInputBox; // 输入任务内容的文本框
        private ListBox _tasksListBox; // 显示任务列表的列表框
    }

    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm()); // 运行主窗体
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

===
如何编译并生成 .exe 文件?

How to compile and generate an .exe file?

  1. 打开 VS Code 并创建一个新的 Windows Forms App (.NET Framework) 项目。
  2. 复制上述代码 到 MainForm.cs 中,替换原有代码。
  3. 在解决方案资源管理器中,右键点击项目名称 -> 发布 -> 启动 -> 按照向导完成发布过程。
  4. 发布完成后,可以在指定路径找到生成的 .exe 文件。

注意:实际开发过程中,您可能需要根据具体需求进一步完善此应用,比如增加删除任务、修改任务状态等功能;同时为了更好的用户体验,还可以考虑优化UI设计。

以上就是一个基于C# WinForms的简单To-Do应用的基本框架,您可以在此基础上继续扩展和完善。
The above is the basic framework of a simple To-Do application based on C# WinForms, which you can further expand and improve upon.

供参考
For reference

Good by~See U next time~

Top comments (0)