Skip to content

![high](https://www.gstatic.com/codereviewagent/high-priority.svg) #4

@Jakobish

Description

@Jakobish

high

The logic in Refresh-SiteCombo has two issues:

  1. If a previously selected site is removed, the line $state.SiteCombo.Text = $currentValue will throw an exception because the DropDownStyle is DropDownList and the value is no longer in the Items collection.
  2. If no site was selected ($currentValue is empty), after the refresh the first site in the list becomes selected (because Initialize-SiteComboBox sets SelectedIndex = 0). The expected behavior would be to keep no site selected.

This can be fixed by adjusting the logic to handle these cases correctly.

function Refresh-SiteCombo {
    param(
        [ValidateSet("Source", "Destination")] [string]$Side
    )

    $state = $providerUiStates[$Side]
    if (-not $state -or $state.CurrentMode -ne "Site") { return }

    $currentValue = $state.SiteCombo.Text
    if (Initialize-SiteComboBox -Side $Side) {
        if ($currentValue) {
            $index = $state.SiteCombo.Items.IndexOf($currentValue)
            if ($index -ge 0) {
                $state.SiteCombo.SelectedIndex = $index
            }
            # else: The previously selected site is gone. The selection will
            # default to index 0 from Initialize-SiteComboBox, which is a reasonable fallback.
        }
        else {
            # No site was selected before, so reset selection.
            $state.SiteCombo.SelectedIndex = -1
        }
    }
}

Originally posted by @gemini-code-assist[bot] in #1 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions