You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TwwDateTimePicker双控件日期联动选择异常问题咨询

Hey there! Let's get your TwwDateTimePicker date range linkage working perfectly. I'll walk you through the exact steps and code to make this happen, including handling the scenario you mentioned where you first select today on the picker, choose a date 7 days out on the second one, then adjust the first picker later.

Core Approach

The key here is to listen for changes on the first TwwDateTimePicker and dynamically update the minimum and maximum allowed dates on the second picker based on the selected date. We'll also add logic to handle cases where the second picker's current date falls outside the new range after a change.

Step-by-Step Implementation

When your form loads, set the first picker to today's date and initialize the second picker's range right away:

procedure TYourForm.FormCreate(Sender: TObject);
begin
  // Set first picker to today's date on form load
  wwDateTimePicker1.Date := Date;
  // Trigger the change event to set up the second picker's initial range
  wwDateTimePicker1Change(wwDateTimePicker1);
end;

2. Add the OnChange Event Handler for the First Picker

This is where the magic happens. Add this event handler to your first TwwDateTimePicker:

procedure TYourForm.wwDateTimePicker1Change(Sender: TObject);
var
  SelectedDate: TDate;
  NewMinDate, NewMaxDate: TDate;
begin
  // Grab the currently selected date from the first picker
  SelectedDate := wwDateTimePicker1.Date;

  // Calculate the new range for the second picker
  // When first picker is today, Min = Today+1, Max = Today+15
  // For any other selected date, we apply the same logic: Min = SelectedDate+1, Max = SelectedDate+15
  NewMinDate := SelectedDate + 1;
  NewMaxDate := SelectedDate + 15;

  // Update the second picker's date range
  wwDateTimePicker2.MinDate := NewMinDate;
  wwDateTimePicker2.MaxDate := NewMaxDate;

  // Handle the case where the second picker's current date is now outside the new range
  // If it's earlier than the new Min, set it to the new Min
  if wwDateTimePicker2.Date < NewMinDate then
    wwDateTimePicker2.Date := NewMinDate
  // If it's later than the new Max, set it to the new Max
  else if wwDateTimePicker2.Date > NewMaxDate then
    wwDateTimePicker2.Date := NewMaxDate;
end;

3. Test the Scenario You Mentioned

Let's walk through your specific workflow to confirm it works:

  • When you first open the form, the first picker is set to today, and the second picker's range is Today+1 to Today+15.
  • You select Today+7 on the second picker (which is within the valid range).
  • If you now change the first picker to, say, Tomorrow, the second picker's range updates to Day After Tomorrow to Tomorrow+15 (Today+16). Your selected date (Today+7 = Tomorrow+6) is still within this new range, so it stays selected.
  • If you change the first picker to a date far in the future (like Today+20), the second picker's range becomes Today+21 to Today+35. Your original selected date (Today+7) is now below the new minimum, so the second picker will automatically jump to Today+21 (the new MinDate).
Key Notes
  • Make sure both TwwDateTimePickers have their Date property set correctly initially (no invalid dates).
  • If you need a different range rule for non-today dates (e.g., a wider/narrower window), just adjust the NewMinDate and NewMaxDate calculations in an if/else block. For example:
    if SelectedDate = Date then
    begin
      NewMinDate := Date + 1;
      NewMaxDate := Date + 15;
    end
    else
    begin
      // Custom range for non-today dates
      NewMinDate := SelectedDate + 2;
      NewMaxDate := SelectedDate + 30;
    end;
    

内容的提问来源于stack exchange,提问作者meherabhi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:42:52