简体   繁体   English

局部类中的表单控件属性

[英]Form Control Properties in Partial Class

I try to make some form control like Minimize, Exit and drag form but seems not work. 我尝试做一些表单控件,如“最小化”,“退出”和“拖动表单”,但似乎不起作用。 I think the problem with partial class but after searching, I can't find solution to make this work. 我认为局部类存在问题,但是经过搜索后,我找不到解决方案来实现此目的。

Note: I can't remove namespace and partial for some reason. 注意:由于某些原因,我无法删除名称空间和部分名称空间。 What must I change, maybe declare etc? 我必须更改什么,也许要声明等等?

    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);
            }
        }

In VB that code works fine. 在VB中,该代码可以正常工作。

What Tony means is that in the Designer of your form you hook the handlers such as Tony的意思是,在表单的设计器中,您要钩住处理程序,例如

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

and this is where it's hooked.The problem is that probably you have it defined in several files (or didn't hook them). 问题是您可能已在多个文件中定义了它(或没有将它们钩住)。

Try to put the class in a different namespace and check if your InitializeComponent goes to the designer where you put the hooked functions. 尝试将类放在不同的名称空间中,并检查InitializeComponent转到放置挂钩函数的设计器中。

The "partial" keyword indicates that the code for a class can be found in multiple files. “ partial”关键字指示可以在多个文件中找到一个类的代码。 The forms designer in Visual Studio automatically creates a Form1.Designer.cs class where it puts the code to create controls you dragged onto the form. Visual Studio中的表单设计器会自动创建一个Form1.Designer.cs类,在该类中,代码将用于创建您拖动到表单上的控件。

  • When you try to remove the partial keyword from Form1 , the compiler will tell you that Form1.Designer.cs contains a partial definition for the same class. 当您尝试从Form1删除partial关键字时,编译器将告诉您Form1.Designer.cs包含同一类的部分定义。
  • When you change the namespace, the two become separate classes, but Form1.cs calls InitializeComponent() which is defined in the other class. 当您更改名称空间时,这两个将成为单独的类,但是Form1.cs调用在另一个类中定义的InitializeComponent()

(Posted a solution on behalf of the question author) (代表问题作者发布了解决方案)

Thank you for @Tony for point me. 谢谢你@Tony给我的建议。 I need to add event handler manually in C# like 我需要像这样在C#中手动添加事件处理程序

 this.Closing += Form1_FormClosing; //for close button 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM