C# Windows Forms DateTimePicker, how to get the SelectedText, SelectionStart, and SelectionLength?

谁说胖子不能爱 提交于 2020-05-16 22:04:31

问题


The .NET DateTimePicker has Text, but I cannot figure out how to get and set the SelectionStart, SelectionLength, and SelectionText properties for the DateTimePicker.

For example, how can I programmatically select the 2 day digits in the date "11/17/2001"? and how can I programmatically determine the currently selection start position?

Essentially, what I would like to do is something like the following code:

// Select the day's text
((TextBox)myDateTimePicker).SelectionStart = 3;
((TextBox)myDateTimePicker).SelectionLength = 2;

// Get the text start position
return ((TextBox)myDateTimePicker).SelectionStart;


回答1:


The following code is the answer to my question. It is derived from the DateTimePicker get focused field question's answer.

public event System.EventHandler<EventArgs> Parent_ValueChanged;

public enum DatePart
{
  YEAR,
  MONTH,
  DAY
}

[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public DatePart SelectedPart { get; set; }

private DateTime m_PreviousValue = TimeTools.UnixBaseTime;
private bool m_CheckSelectedPart = false;

public void DateTimeField_GotFocus(object sender, EventArgs e)
{
  m_PreviousValue = this.Value;
  m_CheckSelectedPart = true;
  SendKeys.SendWait("{UP}");
  SendKeys.SendWait("{DOWN}");
  m_CheckSelectedPart = false;
}


public void DateTimeField_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
  switch (e.KeyCode)
  {
    case Keys.Space: 
      e.IsInputKey = true;
      break;
    case Keys.Tab:
      m_PreviousValue = this.Value;
      m_CheckSelectedPart = true;
      SendKeys.SendWait("{UP}");
      SendKeys.SendWait("{DOWN}");
      m_CheckSelectedPart = false;

      // Set e.IsInputKey to false to let Windows use the Tab 
      // to go to the previous or next component with TabStop = true
      if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
      {
        // false = exit to the left or up
        e.IsInputKey = !(SelectedPart == DatePart.MONTH);
      }
      else
      { 
        // false = exit to the right or down
        e.IsInputKey = !(SelectedPart == DatePart.YEAR);
      }
      break;
    default: break;
  }
}


public void DateTimeField_KeyDown(object sender, KeyEventArgs e)
{
  // Mimic Delphi's DateTimePicker behavior by advancing to the next part in the format
  // using Space or Tab
  if (e.KeyCode.Equals(Keys.Space) || e.KeyCode.Equals(Keys.Tab))
  {
    if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
    {
      SendKeys.Send("{left}");
    }
    else
    {
      SendKeys.Send("{right}");
    }
  }
}


private void DateTimeField_ValueChanged(object sender, EventArgs e)
{
  if ((m_CheckSelectedPart) && (sender is DateTimePicker dtp))
  {
    TimeSpan change = (dtp.Value - m_PreviousValue);
    var dayChange = Math.Abs(change.Days);
    if (dayChange == 1)
    {
      SelectedPart = DatePart.DAY;
    }
    else if (dayChange >= 365)
    {
      SelectedPart = DatePart.YEAR;
    }
    else
    {
      SelectedPart = DatePart.MONTH;
    }
    m_PreviousValue = dtp.Value;
  }

  // parent's ValueChanged event handler
  Parent_ValueChanged?.Invoke(sender, e);
}



来源:https://stackoverflow.com/questions/61492289/c-sharp-windows-forms-datetimepicker-how-to-get-the-selectedtext-selectionstar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!