By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,107 Members | 1,584 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,107 IT Pros & Developers. It's quick & easy.

Line wont show up

P: 1
When I create taskbox on the form the line beside the box wont show up.
it show up untill I have MouseEnter event occur.
Also when I have 2 taskbox on the form it does draw the line around the label
however the line wont draw when i have 1 taskbox move away from another taskbox. The line is gone at last sec when taskbox move away.

could someone please tell me where does I made mistake and how to fix it
thank you
Below is the code.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.Drawing;
  6.  
  7. namespace Task
  8. {
  9.     public class TaskBox: Label
  10.     {
  11.         private bool m_IsPressed;
  12.         private int curDifX,curDifY;
  13.         private const int STEPBOX_WIDTH = 105;
  14.         private const int STEPBOX_HEIGHT = 25;
  15.  
  16.         public event MouseRightKeyPressHandler MouseRightKeyPress;
  17.         public delegate void MouseRightKeyPressHandler(object sender, MouseEventArgs e);
  18.  
  19.         public event DeleteTaskHandler DeleteTask;
  20.         public delegate void DeleteTaskHandler(object sender);
  21.  
  22.  
  23.         public TaskBox(string name) {
  24.  
  25.             this.Text = name;
  26.             this.TextAlign = ContentAlignment.MiddleCenter;
  27.             this.BorderStyle = BorderStyle.None;
  28.             this.Size = new Size(STEPBOX_WIDTH, STEPBOX_HEIGHT);
  29.             this.MouseDown += new System.Windows.Forms.MouseEventHandler(this_MouseDown);
  30.             this.MouseMove += new System.Windows.Forms.MouseEventHandler(this_MouseMove);
  31.             this.KeyUp += new System.Windows.Forms.KeyEventHandler(this_KeyUp);
  32.             this.MouseUp += new System.Windows.Forms.MouseEventHandler(this_MouseUp);
  33.             this.MouseLeave += new EventHandler(this_MouseLeave);
  34.             this.MouseEnter += new EventHandler(this_MouseEnter);
  35.             this.BackColor = Color.FromKnownColor(KnownColor.Control);
  36.             this.LostFocus+=new EventHandler(this_LostFocus);
  37.             this.Paint+=new PaintEventHandler(TaskBox_Paint);
  38.  
  39.        }
  40.  
  41.           private void this_MouseDown(object sender, MouseEventArgs e)
  42.         {
  43.             m_IsPressed = true;
  44.             (sender as TaskBox).Select();
  45.             curDifX = Cursor.Position.X - (sender as Label).Left;
  46.             curDifY = Cursor.Position.Y - (sender as Label).Top;
  47.             (sender as TaskBox).BackColor = Color.Teal;
  48.             if (e.Button == MouseButtons.Right)
  49.             {
  50.                 if (MouseRightKeyPress != null)
  51.                 {
  52.  
  53.                     MouseRightKeyPress(this, e);
  54.                 }
  55.             }
  56.  
  57.         }
  58.  
  59.         private void this_MouseMove(object sender, MouseEventArgs e)
  60.         {
  61.             if (m_IsPressed == true)
  62.             {
  63.                 if (Cursor.Position.X - curDifX < 0)
  64.                 {
  65.                     (sender as TaskBox).Left = 0;
  66.                 }
  67.                 else
  68.                 {
  69.                     (sender as TaskBox).Left = Cursor.Position.X - curDifX;
  70.                 }
  71.  
  72.                 if (Cursor.Position.Y - curDifY < 0)
  73.                 {
  74.                     (sender as TaskBox).Top = 0;
  75.  
  76.                 }
  77.                 else
  78.                 {
  79.                     (sender as TaskBox).Top = Cursor.Position.Y - curDifY;
  80.                 }
  81.  
  82.                 (sender as TaskBox).BringToFront();
  83.  
  84.             }
  85.         }
  86.  
  87.         private void this_MouseUp(object sender, MouseEventArgs e)
  88.         {
  89.             (sender as TaskBox).BackColor = Color.Teal;
  90.             m_IsPressed = false;
  91.  
  92.         }
  93.  
  94.          private void this_MouseEnter(object sender, EventArgs e)
  95.         {
  96.             Graphics g = (sender as TaskBox).CreateGraphics();
  97.             g.DrawRectangle(Pens.Red, 0, 0, STEPBOX_WIDTH-1, STEPBOX_HEIGHT-1);
  98.         }
  99.         private void this_MouseLeave(object sender, EventArgs e)
  100.         {
  101.             Graphics g = (sender as TaskBox).CreateGraphics();
  102.             g.DrawRectangle(Pens.Black, 0, 0, STEPBOX_WIDTH-1, STEPBOX_HEIGHT-1);
  103.         }
  104.  
  105.         private void this_KeyUp(object sender, KeyEventArgs e)
  106.         {
  107.             if (e.KeyData == Keys.Delete)
  108.             {
  109.                 DeleteTask(this);
  110.             }
  111.         }
  112.  
  113.  
  114.         public void TaskBox_Paint(Object sender,PaintEventArgs e)
  115.         {
  116.             Graphics g = (sender as TaskBox).CreateGraphics();
  117.  
  118.  
  119.             if (m_IsPressed)
  120.             {
  121.                 g.DrawRectangle(Pens.Red, 0, 0, STEPBOX_WIDTH - 1, STEPBOX_HEIGHT - 1);
  122.             }
  123.             else
  124.             {
  125.                 g.DrawRectangle(Pens.Black, 0, 0, STEPBOX_WIDTH - 1, STEPBOX_HEIGHT - 1);
  126.             }
  127.  
  128.         }
  129.  
  130.         private void this_LostFocus(object sender, EventArgs e)
  131.         {
  132.  
  133.             (sender as TaskBox).BackColor = Color.FromKnownColor(KnownColor.Control);
  134.         }
  135.  
  136.     }
  137. }
Jan 17 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.