问题描述
公主!您好!
我正在用C#为WinXP编写Windows窗体应用程序.在主窗体上,我有一个MaskedTextBox控件,用于由用户输入日期.在文本框旁边有一个按钮,应通过按下该按钮来调用弹出式日历.然后,用户选择一个日期,日历关闭,日期显示在文本框中.
按下按钮时很容易显示普通形式:
Privet! Hello!
I''m writing a Windows Forms application in C# for WinXP. On a main form I have a MaskedTextBox control for a date to be entered by a user. Beside the text box there''s a button which should call a pop-up calendar by pressing on it. A user then picks up a date, the calendar closes, and the date appears in the text box.
An ordinary form shows up easily when the button is pressed:
private void btnBeginDate_Click(object sender, EventArgs e)
{
Form frm = new Form();
frm.Show(); // The 'frm' form appears
}
但是MonthCalendar控件不会以这种方式显示:
But the MonthCalendar control won''t show up in such manner:
private void btnBeginDate_Click(object sender, EventArgs e)
{
MonthCalendar MCalendar = new MonthCalendar();
MCalendar.Show(); // Nothing visible happens
}
那么我应该如何实现呢?我无法打开弹出式日历.
在此先感谢您.
So how should I implement this? I can''t open a pop-up calendar.
Thanks in advance.
推荐答案
MonthCalendar MonthCalendar = new MonthCalendar();
Panel PanelCalendar = new Panel();
//...
MonthCalendar.//... setup all kind of properties you may need: dock, size, options...
//...
PanelCalendar.Controls.Add(MonthCalendar); //that's is
//or:
//MonthCalendar.Parent = PanelCalendar; //same thing
所有控件都按层次结构相互插入,直到父窗体为止.表单之间没有功能上的父子关系,但是,如果使用多个关系,请使用OwnedForm/Owner
关系.
任何形式的弹出窗口都是有害的,尤其是您将要一遍又一遍地创建和插入日历实例.如果您不想永久地在窗体上看到它并在某些事件上对其进行更新(但是为什么不呢?),则可以添加一次并隐藏/显示,缩小父面板,将其隐藏在您可以按需选择,将其停靠……那么多明智的选择.弹出式窗口令人讨厌,不便,而且我想说……是幼稚的. :-)
All controls are inserted in each other in hierarchy, up to the parent form. There is no functional parent-child relationship between forms though, instead, use OwnedForm/Owner
relationship if you use more than one.
Any kind of pop-up is bad, especially you are going to create and insert an instance of your calendar over and over. If you don''t want to see it on the form permanently and update it on some events (but why not?), you can add it once and hide/show, shrink the parent panel, hide it in a tab of TabControl
which you can select on demand, dock it… so many sensible options. Pop-ups are intrusive, inconvenient and, I would say… childish. :-)
这篇关于如何在C#中创建弹出式日历?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!