找回密码
 立即注册

QQ登录

只需一步,快速开始

viviant

注册会员

7

主题

15

帖子

50

积分

注册会员

积分
50

活字格认证

viviant
注册会员   /  发表于:2015-7-2 16:02  /   查看:8005  /  回复:4
Date控件获得焦点时,光标是在年上(CursorPosition=Era)
请问当点击Spin的上下箭头时,光标移到月上,月份减少或增加,应该怎么弄。
谢谢

4 个回复

倒序浏览
Leo
超级版主   /  发表于:2015-7-2 16:57:00
沙发
居然真的看到有人在用这个神器了。你们是从4.0之前版本迁移过来的项目吗?

关于你提到的问题。示例解决代码如下:
  1.     public partial class Form1 : Form
  2.     {
  3.         private Date _date1;
  4.         public Form1()
  5.         {
  6.             InitializeComponent();
  7.             //
  8.             // Normally init some Interop.Date.
  9.             //
  10.             Date date1 = new Date();
  11.             this.Controls.Add(date1);
  12.             this._date1 = date1;
  13.             _date1.Width += 17;
  14.             _date1.Spin.Visible = Visibility.ShowAlways;
  15.             _date1.Format.Pattern = "ggg ee年MM月dd日";
  16.             //
  17.             // Magic starts from here:
  18.             //
  19.             var gcDateTime = _date1.InnerEditor;
  20.             for (var i = 0; i < gcDateTime.SideButtons.Count; i++)
  21.             {
  22.                 var sideButton = gcDateTime.SideButtons[i] as GrapeCity.Win.Editors.SpinButton;
  23.                 if (sideButton != null)
  24.                 {
  25.                     sideButton.IsDefaultBehavior = false;
  26.                     sideButton.SpinDown += delegate
  27.                     {
  28.                         MoveCaretToDateTimeMonthField(gcDateTime);
  29.                         gcDateTime.ShortcutSpinDown();
  30.                     };
  31.                     sideButton.SpinUp += delegate
  32.                     {
  33.                         MoveCaretToDateTimeMonthField(gcDateTime);
  34.                         gcDateTime.ShortcutSpinUp();
  35.                     };
  36.                 }
  37.             }
  38.         }
  39.         private static void MoveCaretToDateTimeMonthField(GrapeCity.Win.Editors.GcDateTime gcDateTime)
  40.         {
  41.             var monthField = gcDateTime.Fields.Find<GrapeCity.Win.Editors.Fields.DateMonthField>();
  42.             if (monthField != null)
  43.             {
  44.                 var selectionStart = 0;
  45.                 foreach (GrapeCity.Win.Editors.Fields.DateField field in gcDateTime.Fields)
  46.                 {
  47.                     if (field == monthField)
  48.                     {
  49.                         break;
  50.                     }
  51.                     selectionStart += field.Text.Length;
  52.                 }
  53.                 gcDateTime.Select(selectionStart, 0);
  54.             }
  55.         }
  56.     }
复制代码

主要思想是禁用默认的Spin行为,自己挂事件处理Spin行为。在处理函数中,随心所欲地将光标移动到合适的位置。

所使用到的主要黑技术有
1. InnerEditor对象其实封装了真正的GrapeCity.Win.Editors.GcDateTime实例
2. Spin按钮的行为其实是通过IsDefaultBehavior搞定的。
3. 怎么找计算光标的目标位置

希望你喜欢。

评分

参与人数 1金币 +500 收起 理由
Alice + 500 非常精彩的方案,奖励500金币

查看全部评分

回复 使用道具 举报
Alice
社区贡献组   /  发表于:2015-7-2 17:42:00
板凳
回复 1楼viviant的帖子

感谢@Leo版主的详细和精彩的回复。
@viviant 如果2楼的方案依然有问题,可以提出来讨论。
请点击评分,对我的服务做出评价!  5分为非常满意!

葡萄城控件服务团队

官方网站: http://www.gcpowertools.com.cn
回复 使用道具 举报
viviant
注册会员   /  发表于:2015-7-3 10:06:00
地板
回复 2楼Leo的帖子

我们确实是之前版本迁移过来的项目。
问题解决了
谢谢
回复 使用道具 举报
Alice
社区贡献组   /  发表于:2015-7-3 11:44:00
5#
回复 4楼viviant的帖子

好的。
谢谢反馈。
此问题关闭,如果有新问题欢迎提出来讨论。
请点击评分,对我的服务做出评价!  5分为非常满意!

葡萄城控件服务团队

官方网站: http://www.gcpowertools.com.cn
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部