c# - 分部类中的表单控件属性

标签 c# winforms

我尝试制作一些表单控件,如最小化、退出和拖动表单,但似乎不起作用。我认为部分类(class)有问题,但在搜索之后,我找不到解决这个问题的方法。

注意:出于某种原因,我无法删除命名空间和部分。我必须更改什么,也许声明等?

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    using System.Net.Sockets;
    using System.Net;

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

//Minimize (Not Work)
            private void Form1_Resize(object sender, EventArgs e)
            {
                if (FormWindowState.Minimized == this.WindowState)
                {
                    notifyTray.Visible = true;
                    notifyTray.ShowBalloonTip(500);
                    this.Hide();
                }

                else if (FormWindowState.Normal == this.WindowState)
                {
                    notifyTray.Visible = false;
                }
            }

//Exit (Not Work)
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                var window = MessageBox.Show("Wanna Close?", "Warning", MessageBoxButtons.YesNo);
                if (window == DialogResult.No) e.Cancel = true;
                else e.Cancel = false;
            }

//Drag (Not Work)
        public bool _dragging = false;
        public Point _offset;
        public Point _start_point = new Point(0, 0);

        void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            _dragging = true;  // _dragging is your variable flag
            _start_point = new Point(e.X, e.Y);
        }

        void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            _dragging = false;
        }

        void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (_dragging)
            {
                Point p = PointToScreen(e.Location);
                Location = new Point(p.X - this._start_point.X, p.Y - this._start_point.Y);
            }
        }

在 VB 中,该代码运行良好。

最佳答案

Tony 的意思是,在表单的设计器中,您可以 Hook 处理程序,例如

this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FormClosing); 

这就是它被钩住的地方。问题是你可能在几个文件中定义了它(或者没有钩住它们)。

尝试将类放在不同的命名空间中,并检查您的 InitializeComponent 是否转到放置 Hook 函数的设计器。

关于c# - 分部类中的表单控件属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21522901/

相关文章:

c# - 如何捕获取消拖动?

c# - 如何暂停创建新线程直到旧线程结束

c# - 在 C# 中从数组中查找和删除项目

c# - 如何递归搜索具有多个通配符的目录?

c# - 在 C# 中,如果类或方法未标记为密封或虚拟,那是什么?

c# - Entity Framework 在运行时将模型类映射到表

c# - 表格申请的字体选择

c# - Windows 应用程序设置 - 更新和数据库架构更改

c# - 接受多种类型的方法参数

c# - 如何启动包含引号的 URL?