如何在C#中创建弹出式日历? [英] How to create a pop-up calendar in C#?

查看:448
本文介绍了如何在C#中创建弹出式日历?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

公主!您好!
我正在用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.

推荐答案

首先,您没有将Calendar实例添加到任何内容.方法Show无关紧要.它是Hide的反义词.您只需要为表单调用Show.您需要添加的内容:

First of all, you did not add an instance of calendar to anything. The method Show is just irrelevant. It''s the antonym of Hide. You only need to call Show for forms. What you need is adding:

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关系.

任何形式的弹出窗口都是有害的,尤其是您将要一遍又一遍地创建和插入日历实例.如果您不想永久地在窗体上看到它并在某些事件上对其进行更新(但是为什么不呢?),则可以添加一次并隐藏/显示,缩小父面板,将其隐藏在您可以按需选择,将其停靠……那么多明智的选择.弹出式窗口令人讨厌,不便,而且我想说……是幼稚的. :-)

—SA



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. :-)

—SA


MonthCalendar是您添加到表单中的控件,而不是表单本身.如果要显示它,则将Visible属性设置为true;
MonthCalendar is a control you add to your form not a form itself. If you want to display it then set the Visible property to true;


这篇关于如何在C#中创建弹出式日历?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆