Reputation: 45
I have created a custom control called MyDateTime as a datetimepicker in c#.net. it builds successful and it runs ok.but when I click on the button it freezes.I must display a panel on below but it does not.I check the code and it is ok .Apparently there is a runtim e error .please help me to solve it
code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;
namespace WindowsFormsApplication5
{
public partial class MyDateTime : UserControl
{
private Button DropButton;
private MaskedTextBox maskedTextBox1;
private MonCal monCal = null;
public class MonCalEventArgs : EventArgs
{
private DateTime selectedDate;
public MonCalEventArgs(DateTime sentSelectedDate)
{
selectedDate = sentSelectedDate;
}
public string SelectedDate { get { return selectedDate.ToString("MM/dd/yyyy"); } }
}
public class HighlightedDates
{
public DateTime Date;
public Point Position = new Point(0, 0);
public Color DateColor;
public Color BoxColor;
public Color BackgroundColor;
public Boolean Bold;
public HighlightedDates(DateTime date)
{
this.Date = date;
this.DateColor = this.BoxColor = this.BackgroundColor = Color.Empty;
this.Bold = true;
}
public HighlightedDates(DateTime date, Color dateColor, Boolean bold)
{
this.Date = date;
this.BackgroundColor = this.BoxColor = Color.Empty;
this.DateColor = dateColor;
this.Bold = bold;
}
public HighlightedDates(DateTime date, Color dateColor, Color boxColor,
Color backgroundColor, Boolean bold)
{
this.Date = date;
this.DateColor = dateColor;
this.BoxColor = boxColor;
this.BackgroundColor = backgroundColor;
this.Bold = bold;
}
}
public MyDateTime()
{
InitializeComponent();
this.SuspendLayout();
maskedTextBox1 = new MaskedTextBox();
maskedTextBox1.Location = new Point(0, 0);
maskedTextBox1.Mask = "00/00/0000";
maskedTextBox1.Name = "maskedTextBox1";
maskedTextBox1.Size = new Size(139, 20);
maskedTextBox1.TabIndex = 0;
maskedTextBox1.ValidatingType = typeof(DateTime);
DropButton = new Button();
DropButton.Size = new Size(16, 16);
DropButton.FlatStyle = FlatStyle.Standard;
DropButton.BackColor = Color.White;
DropButton.BackgroundImage = Image.FromFile(@"C:\Users\JAVAD\documents\visual studio 2015\Projects\WindowsFormsApplication5\WindowsFormsApplication5\CalendarIcon.ico");
DropButton.BackgroundImageLayout = ImageLayout.Tile;
DropButton.Location = new Point(maskedTextBox1.Width - 20, 0);
DropButton.Click += new EventHandler(DropButton_Click);
DropButton.Cursor = Cursors.Arrow;
maskedTextBox1.Controls.Add(DropButton);
maskedTextBox1.Text = DateTime.Now.ToString("MM/dd/yyy");
this.Controls.Add(maskedTextBox1);
this.ResumeLayout();
// InitializeComponent();
}
void DropButton_Click(object sender, EventArgs e)
{
string a= maskedTextBox1.Text;
string []aa= a.Split('/');
List<HighlightedDates> hlDates = new List<HighlightedDates>();
hlDates.Add(new HighlightedDates(new DateTime(int.Parse(aa[2]), int.Parse(aa[0]), int.Parse(aa[1])),
Color.Red, Color.Blue, Color.Pink, true));
monCal = new MonCal(hlDates);
monCal.monCalControlHandler +=
new MonCal.MonCalControlHandler(monCal_monCalControlHandler);
monCal.Location = new Point(20, 20);
this.Controls.Add(monCal);
}
void monCal_monCalControlHandler(object sender, MonCalEventArgs e)
{
maskedTextBox1.Text = e.SelectedDate;
this.Controls.Remove(monCal);
monCal = null;
}
internal class MonCal : MonthCalendar
{
protected static int WM_PAINT = 0x000F;
private Rectangle dayBox;
private int dayTop = 0;
private SelectionRange range;
private List<HighlightedDates> highlightedDates;
public delegate void MonCalControlHandler(object sender, MonCalEventArgs e);
public event MonCalControlHandler monCalControlHandler;
public MonCal(List<HighlightedDates> HighlightedDates)
{
this.ShowTodayCircle = false;
this.highlightedDates = HighlightedDates;
range = GetDisplayRange(false);
SetDayBoxSize();
SetPosition(this.highlightedDates);
}
private void SetDayBoxSize()
{
int bottom = this.Height;
while (HitTest(1, dayTop).HitArea != HitArea.Date &&
HitTest(1, dayTop).HitArea != HitArea.PrevMonthDate) dayTop++;
while (HitTest(1, bottom).HitArea != HitArea.Date &&
HitTest(1, bottom).HitArea != HitArea.NextMonthDate) bottom--;
dayBox = new Rectangle();
dayBox.Size = new Size(this.Width / 7, (bottom - dayTop) / 6);
}
private void SetPosition(List<HighlightedDates> hlDates)
{
int row = 0, col = 0;
hlDates.ForEach(delegate (HighlightedDates date)
{
if (date.Date >= range.Start && date.Date <= range.End)
{
TimeSpan span = date.Date.Subtract(range.Start);
row = span.Days / 7;
col = span.Days % 7;
date.Position = new Point(row, col);
}
});
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_PAINT)
{
Graphics g = Graphics.FromHwnd(this.Handle);
PaintEventArgs pea =
new PaintEventArgs(g, new Rectangle(0, 0, this.Width, this.Height));
OnPaint(pea);
}
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
Graphics g = pe.Graphics;
Rectangle backgroundRect;
highlightedDates.ForEach(delegate (HighlightedDates date)
{
backgroundRect = new Rectangle(
date.Position.Y * dayBox.Width + 1,
date.Position.X * dayBox.Height + dayTop,
dayBox.Width, dayBox.Height);
if (date.BackgroundColor != Color.Empty)
{
using (Brush brush = new SolidBrush(date.BackgroundColor))
{
g.FillRectangle(brush, backgroundRect);
}
}
if (date.Bold || date.DateColor != Color.Empty)
{
using (Font textFont =
new Font(Font, (date.Bold ? FontStyle.Bold : FontStyle.Regular)))
{
TextRenderer.DrawText(g, date.Date.Day.ToString(), textFont,
backgroundRect, date.DateColor,
TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
}
}
if (date.BoxColor != Color.Empty)
{
using (Pen pen = new Pen(date.BoxColor))
{
Rectangle boxRect = new Rectangle(
date.Position.Y * dayBox.Width + 1,
date.Position.X * dayBox.Height + dayTop,
dayBox.Width, dayBox.Height);
g.DrawRectangle(pen, boxRect);
}
}
});
}
protected override void OnDateSelected(DateRangeEventArgs drevent)
{
base.OnDateSelected(drevent);
MonCalEventArgs args = new MonCalEventArgs(drevent.Start);
monCalControlHandler(this, args);
this.Dispose();
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
base.OnPaintBackground(pevent);
}
}
}
}
Upvotes: 0
Views: 2470
Reputation: 814
Your program is stuck inside:
while (HitTest(1, dayTop).HitArea != HitArea.Date && HitTest(1, dayTop).HitArea != HitArea.PrevMonthDate) dayTop++; while (HitTest(1, bottom).HitArea != HitArea.Date && HitTest(1, bottom).HitArea != HitArea.NextMonthDate) bottom--;
That's because HitTest(1, dayTop).HitArea
returns a value of HitArea.Nowhere
. that means all of your conditions in the while loops will be always right, thus causing the program to freeze because it's stuck there looping mindlessly forever.
I'm not sure what you're trying to achieve but a simple fix would be to add the following line to your while loops:
while (HitTest(1, dayTop).HitArea != HitArea.Nowhere &&
HitTest(1, dayTop).HitArea != HitArea.Date &&
HitTest(1, dayTop).HitArea != HitArea.PrevMonthDate)
{
dayTop++;
}
while (HitTest(1, dayTop).HitArea != HitArea.Nowhere &&
HitTest(1, bottom).HitArea != HitArea.Date &&
HitTest(1, bottom).HitArea != HitArea.NextMonthDate)
{
bottom--;
}
Upvotes: 2