简体   繁体   English

在winforms中创建日历的最佳方法是什么?

[英]What is the best way to approach the creation of a calendar in winforms?

I need to develop several calendar type controls in winforms. 我需要在winforms中开发几个日历类型控件。 These calendars are all vertical in nature. 这些日历本质上都是垂直的。 The concept is that the calendar will have a column for each day (in a month view), hour (in a day view) or week (in a yearly view). 概念是日历将包含每天(月视图),小时(日视图)或周(年视图)的列。 I've attached a sample (rather crude) image of the month view. 我附上了一个月视图的样本(相当粗略)的图像。

在此输入图像描述

So far I've had a good crack at this using a table layout panel, with many panels and labels. 到目前为止,我已经使用表格布局面板进行了很好的破解,其中包含许多面板和标签。 This works, however it's messy and is very slow to load. 这可行,但它很乱,加载速度很慢。 It also comes with a raft of problems that I've tried to overcome. 它还带来了一系列我试图克服的问题。 I've attached a screenshot of how it looks at the moment. 我附上了截图,了解它当下的样子。

在此输入图像描述

I've come up against problems in rendering (the well known stuttering problem), which I've gotten over by hiding the grid while its being built, and then showing it when its done. 我遇到了渲染问题(众所周知的口吃问题),我在构建时隐藏了网格,然后在完成后显示它。 Also, I've had problems with getting the table to resize correctly (the number of columns changes depending on the number of days in the month), as you can see in the screenshot the bottom row takes up all space. 此外,我在使表格正确调整大小时遇到​​了问题(列数会根据当月的天数而变化),正如您在屏幕截图中看到的那样,底行会占用所有空间。 As you can imagine, this is all very nasty and seems to me to be rather hacky. 你可以想象,这一切都非常讨厌,在我看来相当hacky。

I've tried WPF a bit to see if I can overcome the problems, but it seems that though it may render a bit better, the issues with columns and row sizing are the same. 我已经尝试过WPF,看看我是否可以克服这些问题,但似乎虽然它可能会更好一点,但列和行大小调整的问题是相同的。 Is there some other approach I could take to avoid these issues, or is it best to bite the bullet and just switch to WPF? 我可以采取其他方法来避免这些问题,还是最好咬紧牙关并切换到WPF? I find it double frustrating because it would be quite an easy job to get this done in HTML using a table. 我发现它令人沮丧,因为使用表格在HTML中完成这项工作将非常容易。

To reiterate the issues that I'm having are: 重申我所遇到的问题:

  1. Columns are not automatically resizing as I would expect (the last column just fills the space making for some odd UI) 列不会像我期望的那样自动调整大小(最后一列只填充了一些奇怪UI的空间)
  2. Rows are similar, with the bottom row taking up all the empty space. 行类似,底行占用所有空白区域。
  3. Slow rendering 慢渲染
  4. When re-sizing the control have to hide the grid otherwise get lots of stuttering 当重新调整控件大小时必须隐藏网格,否则会出现大量口吃
  5. The hacky nature of the table layout panel 表布局面板的hacky性质

I appreciate any feedback users have on how I should best approach the problem. 我感谢用户对如何最好地解决问题的任何反馈。

I don't have a lot of experience with Table Layout Panel, but after taking a quick look at it there seems to be the options of Fixed Size for a fixed number of columns/rows (which you would change in code) and a Percent size setting for each column. 我没有很多表布局面板的经验,但在快速浏览一下之后,似乎有固定大小的选项,用于固定数量的列/行(您可以在代码中更改)和百分比每列的大小设置。 For example, when loading a month with 31 days, you would create 31 columns with a percent width of about 3.2258. 例如,在加载31天的月份时,您将创建31列,其宽度百分比约为3.2258。 You could probably do something like this: 你可能会做这样的事情:

int numColumns = 31;
tableLayoutPanel1.ColumnCount = numColumns;
tableLayoutPanel1.ColumnStyles.Clear();

for (int i = 0; i < numColumns; i++)
{
    tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100f / numColumns));
}

This would mean as you resize the window the each day stays the same size and they all expand to fit the width of the control. 这意味着当您调整窗口大小时,每天保持相同的大小,并且它们都会扩展以适应控件的宽度。 If that size is too small for you, and you need the columns to be a minimum width, you can put the TableLayoutPanel inside a control which has AutoScroll enabled, and set the minimum size of the TableLayoutPanel like so: 如果该大小对您来说太小,并且您需要将列设置为最小宽度,则可以将TableLayoutPanel放在启用了AutoScroll的控件内,并设置TableLayoutPanel的最小大小,如下所示:

int minColumnWidth = 20;
tableLayoutPanel1.MinimumSize = new Size(numColumns * minColumnWidth, 0);

WPF could do this better but if you aren't experienced with it, it will probably take much longer to do. WPF可以做得更好,但如果你没有经验,可能需要更长的时间。

In Windows Forms, you could always draw the Calendar manually using the Graphics class and OnPaint overriding in a custom control. 在Windows窗体中,您始终可以使用Graphics类手动绘制日历,并在自定义控件中覆盖OnPaint。 This would avoid the flickering and slow issues that child controls pose, and should be easier to learn than WPF. 这样可以避免儿童控件造成的闪烁和缓慢问题,并且比WPF更容易学习。

已经差不多一年了,但是因为我遇到了你的问题,我想分享一个很好的Outlook风格的Winforms日历议程 ,我在CodeProject上找到了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 最佳方法,用于在Winforms中加入控件 - Best approach, for joining controls in winforms 在WinForms中显示和编辑大量十六进制数据的最佳方法是什么? - What is the best way to display and edit a large array of hex data in WinForms? 在Winforms C#中的GUI之间传递变量的最佳方法是什么? - What is best way to pass variables between GUI in Winforms C#? 让 WinForms 应用程序永久运行的最佳方式是什么 - What is the best way to have a WinForms application running eternally WPF和WinForms控件之间进行对话的最佳方式是什么? - What is the best way to talk between WPF and WinForms controls? 使用命令行参数自动化WinForms应用程序的最佳方法是什么? - What't the best way to automate a WinForms app with command line arguments? 进行此简单的TicTacToe练习的最佳方法是什么? - What's the best way to approach this simple TicTacToe exercise? 在创建时将多个文件附加到实体的最佳方法是什么? - What's the best way to attach multiple files to a entity on creation? 在WPF中,创建类似于Outlook 2007的日历视图的最佳方法是什么? - In WPF, what is the best way to create a calendar view similar to Outlook 2007? 将日历缩小为仅包含必要的日历对象的最佳方法是什么(取决于开始日期和结束日期) - What's the best way to shrink a calendar to only contain the necessary calendar objects depending on a start and end date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM