Form Control Properties in Partial Class
Clash Royale CLAN TAG#URR8PPP
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.
Have you added event handlers for all these events (in code or using designer)? Are they called?
– Tony
Feb 3 '14 at 8:51
I try to make label give text "OK" if the method is executed, but there is no string in label. So I assume the event somehow not works. I try in VB, its work. No Error and warning. Thank you for your fast response.
– Colour Dalnet
Feb 3 '14 at 8:55
@Tony : Im sorry, Im kinda interest with your comment, normally in VB I just use that code without add event handlers. How I can check the event handlers for that code?
– Colour Dalnet
Feb 3 '14 at 8:59
3 Answers
3
What Tony means is that in the Designer of your form you hook the handlers such as
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
yes, thank you.. I get it now... I just starting to convert all my VB code to C#... Its work now... How I close this question?
– Colour Dalnet
Feb 3 '14 at 9:29
You already marked it as solved, but i'm not sure if you can close it since other people may find it useful... but i'm also new so maybe there is a way. Sorry
– chuel
Feb 3 '14 at 9:33
Owh ok, thank you for your help.
– Colour Dalnet
Feb 3 '14 at 9:34
The "partial" keyword indicates that the code for a class can be found in multiple files. 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.
Form1.Designer.cs
partial
Form1
Form1.Designer.cs
Form1.cs
InitializeComponent()
(Posted a solution on behalf of the question author)
Thank you for @Tony for point me. I need to add event handler manually in C# like
this.Closing += Form1_FormClosing; //for close button
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Are the code in the method is getting executed? Debug and check. If not hook up the handlers to their corresponding events.
– Junaith
Feb 3 '14 at 8:48