void __fastcall TCustomCommandOptionsDialog::BrowseButtonClick(TObject * Sender)
{
  TButton * Button = DebugNotNull(dynamic_cast<TButton *>(Sender));
  int OptionIndex = GetOptionIndex(Button);
  const TCustomCommandType::TOption & Option = FCommand->GetOption(OptionIndex);
  int ControlIndex = GetControlIndex(Button);
  THistoryComboBox * ComboBox = dynamic_cast<THistoryComboBox *>(FControls[ControlIndex]);

  std::unique_ptr<TOpenDialog> OpenDialog(new TOpenDialog(Application));

  UnicodeString Title;
  if (!Option.FileCaption.IsEmpty())
  {
    Title = Option.FileCaption;
  }
  else
  {
    UnicodeString Caption = Option.Caption;
    Caption = StripHotkey(Caption);
    if (!Caption.IsEmpty() && (Caption[Caption.Length()] == L':'))
    {
      Caption.SetLength(Caption.Length() - 1);
    }
    Title = FMTLOAD(EXTENSION_OPTIONS_BROWSE_TITLE, (Caption));
  }
  OpenDialog->Title = Title;

  UnicodeString Value;
  if (ComboBox->Text.IsEmpty())
  {
    Value = Option.FileInitial;
  }
  else
  {
    Value = ComboBox->Text;
  }
  UnicodeString ExpandedValue = ExpandEnvironmentVariables(Value);
  OpenDialog->FileName = ExpandedValue;
  UnicodeString InitialDir = ExtractFilePath(ExpandedValue);
  if (!InitialDir.IsEmpty())
  {
    OpenDialog->InitialDir = InitialDir;
  }
  OpenDialog->Filter = Option.FileFilter;
  OpenDialog->DefaultExt = Option.FileExt;

  if (OpenDialog->Execute())
  {
    if (OpenDialog->FileName != ExpandedValue)
    {
      ComboBox->Text = OpenDialog->FileName;
    }
    // If user just confirms the initial value, persist it
    else if (ComboBox->Text.IsEmpty())
    {
      DebugAssert(Option.FileInitial == Value);
      ComboBox->Text = Value;
    }
  }
}

void __fastcall TCustomCommandOptionsDialog::BrowseButtonClick(TObject * Sender)
{
  TButton * Button = DebugNotNull(dynamic_cast<TButton *>(Sender));
  int OptionIndex = GetOptionIndex(Button);
  const TCustomCommandType::TOption & Option = FCommand->GetOption(OptionIndex);
  int ControlIndex = GetControlIndex(Button);
  THistoryComboBox * ComboBox = dynamic_cast<THistoryComboBox *>(FControls[ControlIndex]);

  std::unique_ptr<TOpenDialog> OpenDialog(new TOpenDialog(Application));

  UnicodeString Title;
  if (!Option.FileCaption.IsEmpty())
  {
    Title = Option.FileCaption;
  }
  else
  {
    UnicodeString Caption = Option.Caption;
    Caption = StripHotkey(Caption);
    if (!Caption.IsEmpty() && (Caption[Caption.Length()] == L':'))
    {
      Caption.SetLength(Caption.Length() - 1);
    }
    Title = FMTLOAD(EXTENSION_OPTIONS_BROWSE_TITLE, (Caption));
  }
  OpenDialog->Title = Title;

  UnicodeString Value;
  if (ComboBox->Text.IsEmpty())
  {
    Value = Option.FileInitial;
  }
  else
  {
    Value = ComboBox->Text;
  }
  UnicodeString ExpandedValue = ExpandEnvironmentVariables(Value);
  OpenDialog->FileName = ExpandedValue;
  UnicodeString InitialDir = ExtractFilePath(ExpandedValue);
  if (!InitialDir.IsEmpty())
  {
    OpenDialog->InitialDir = InitialDir;
  }
  OpenDialog->Filter = Option.FileFilter;
  OpenDialog->DefaultExt = Option.FileExt;

  if (OpenDialog->Execute())
  {
    if (OpenDialog->FileName != ExpandedValue)
    {
      ComboBox->Text = OpenDialog->FileName;
    }
     
    else if (ComboBox->Text.IsEmpty())
    {
      DebugAssert(Option.FileInitial == Value);
      ComboBox->Text = Value;
    }
  }
}
