ahmed_shad

Dear all

I am creating a usercontrol; this usercontrol will look like normal calendar control. It has a button and when you click on button it popup the calendar. So far it is working ok. But when I place this control on groupbox. If groupbox height is not quite high and when I click on usercontrol button to expand the calendar, as my usercontrol is child for groupbox so due to limited height of parent (groupbox) I am unable to see my full calendar.

Is there anyway I can make some changes in my usercontrol and it behaves like combobox. As in combobox if you expand it, dropdown portion of combobox remains top on groupbox and comes outside the boundaries of groupbox and you can see its full view.

Many thanks

Cheers

Muhammad



Re: Windows Forms General change Usercontrol's behaviour to get full view

Zhi-Xin Ye - MSFT

Add MonthCalendar to the Form's Control set instead of the UserControl's. And use BrightToFront() method to bright the MonthCalendar to front by z-order, which make it shows on top of the other controls.I wirte the following sample for your information:

Code Snippet

namespace Sample11

{

public partial class ZhixinYeCalendarControl : UserControl

{

public ZhixinYeCalendarControl()

{

InitializeComponent();

this.Load += new EventHandler(CalendarControl_Load);

}

private bool canDropDown;

private TextBox textBox1;

private Button button1;

private MonthCalendar calendar;

void CalendarControl_Load(object sender, EventArgs e)

{

this.canDropDown = true;

this.calendar = new MonthCalendar();

this.calendar.DateSelected += new DateRangeEventHandler(calendar_DateSelected);

this.calendar.Visible = false;

//set the location, important!

Point p = this.Parent.PointToScreen(this.Location);

p = this.FindForm().PointToClient(p);

this.calendar.Location = p;

this.calendar.Top = p.Y + this.Height;

//add the calendar to the control set of the parent form
//instead of the UserControl

this.FindForm().Controls.Add(this.calendar);

this.Width = this.calendar.Width;

this.Height = 21;

this.textBox1 = new TextBox();

this.textBox1.Left = 0;

this.textBox1.Top = 0;

this.textBox1.Width = this.Width - 20;

this.textBox1.Height = 21;

this.textBox1.Text = DateTime.Today.ToString();

this.Controls.Add(this.textBox1);

this.button1 = new Button();

this.button1.Left = this.textBox1.Width;

this.button1.Top = 0;

this.button1.Width = 20;

this.button1.Height = 21;

this.button1.Paint += new PaintEventHandler(button1_Paint);

this.button1.Click += new EventHandler(button1_Click);

this.Controls.Add(this.button1);

}

void calendar_DateSelected(object sender, DateRangeEventArgs e)

{

this.textBox1.Text = e.Start.ToString();

this.canDropDown = true;

this.calendar.Hide();

}

void button1_Paint(object sender, PaintEventArgs e)

{

ControlPaint.DrawComboButton(e.Graphics,

this.button1.ClientRectangle,

ButtonState.Normal);

}

private void button1_Click(object sender, EventArgs e)

{

if (canDropDown)

{

this.calendar.Show();

this.calendar.BringToFront();

}

else

{

this.calendar.Hide();

}

canDropDown = !canDropDown;

}

}

}






Re: Windows Forms General change Usercontrol's behaviour to get full view

ahmed_shad

Thank you Zhi-Xin Ye - MSFT for your help.

But if is required to place your component close to bottom border of the form. Then it doesn't show the full calendar. But on the other side Microsoft control works fine.

Any chance can we do the same please

Many thanks

Muhammad.





Re: Windows Forms General change Usercontrol's behaviour to get full view

Zhi-Xin Ye - MSFT

After more research on this, I figure out a better approach, which is to host the MonthCalendar control into a ToolStripControlHost object, then add the ToolStripControlHost object into the Items collection of a ToolStripDropDown.
So, the sample code would be something like this:

Code Snippet

public partial class ZhixinYeCalendarControl : UserControl

{

public ZhixinYeCalendarControl()

{

InitializeComponent();

this.Load += new EventHandler(CalendarControl_Load);

}

private bool canDropDown;

private TextBox textBox1;

private Button button1;

private MonthCalendar calendar;

ToolStripDropDown popup;

void CalendarControl_Load(object sender, EventArgs e)

{

this.canDropDown = true;

this.calendar = new MonthCalendar();

this.calendar.DateSelected += new DateRangeEventHandler(calendar_DateSelected);

popup = new ToolStripDropDown();

popup.Margin = Padding.Empty;

popup.Padding = Padding.Empty;

ToolStripControlHost host = new ToolStripControlHost(this.calendar);

host.Margin = Padding.Empty;

host.Padding = Padding.Empty;

popup.Items.Add(host);

this.Width = this.calendar.Width;

this.Height = 21;

this.textBox1 = new TextBox();

this.textBox1.Left = 0;

this.textBox1.Top = 0;

this.textBox1.Width = this.Width - 20;

this.textBox1.Height = 21;

this.textBox1.Text = DateTime.Today.ToString();

this.Controls.Add(this.textBox1);

this.button1 = new Button();

this.button1.Left = this.textBox1.Width;

this.button1.Top = 0;

this.button1.Width = 20;

this.button1.Height = 21;

this.button1.Paint += new PaintEventHandler(button1_Paint);

this.button1.Click += new EventHandler(button1_Click);

this.Controls.Add(this.button1);

}

void calendar_DateSelected(object sender, DateRangeEventArgs e)

{

this.textBox1.Text = e.Start.ToString();

this.canDropDown = true;

this.popup.Hide();

}

void button1_Paint(object sender, PaintEventArgs e)

{

ControlPaint.DrawComboButton(e.Graphics,

this.button1.ClientRectangle,

ButtonState.Normal);

}

private void button1_Click(object sender, EventArgs e)

{

if (canDropDown)

{

Point location;

location = this.Parent.PointToScreen(this.Location);

location = this.FindForm().PointToClient(location);

location.Y = location.Y + this.Height;

this.popup.Show(this.FindForm(),location);

}

else

{

this.popup.Hide();

}

canDropDown = !canDropDown;

}

}