AlexDev
AlexDev

Reputation: 85

trouble with combobox selectedIndex

Okay, the user chooses something from the child window's combobox, presses the button and in the main window canvas is filled with a color. that is the point, but the same thing happens if a user presses "X" also. I want for the computer to see the difference in "X" and the button i created for that purpose.

Questionnaire q = new Questionnaire();
        q.ShowDialog();


        var color = q.comboBox1.SelectedIndex;
        switch (color)
        {
            case 0:
                Canvas.Background = new SolidColorBrush(Color.FromArgb(255, 0, 128, 0));
                break;
            case 1:
                Canvas.Background = new SolidColorBrush(Color.FromArgb(255, 128, 128, 128));
                break;
            case 2:
                Canvas.Background = new SolidColorBrush(Color.FromArgb(255, 211, 211, 211));
                break;
            case 3:
                Canvas.Background = new SolidColorBrush(Color.FromArgb(255, 255, 127, 80));
                break;
            case 4:
                Canvas.Background = new SolidColorBrush(Color.FromArgb(255, 128, 128, 0));
                break;
            case 5:
                Canvas.Background = new SolidColorBrush(Color.FromArgb(255, 255, 222, 173));
                break;

this code is written in MainWindowButton_ClickEvent which opens up the child Window. Also i have tried with Window_Closed and Window_Closing to fix the problem by setting SelectedIndex to -1, But all it does is always return -1 and not filling canvas regardless of the button pressed. I have also tried with DialogResult to false. None of it work.

Upvotes: 0

Views: 63

Answers (1)

mm8
mm8

Reputation: 169400

Try to set the DialogResult property of the Questionnaire window before you close it in the button click event handler:

private void CloseButton_Click(object sender, RoutedEventArgs e)
{
    DialogResult = true;
    Close();
}

You can then check the value that is returned from the DialogResult method to determine whether the button was clicked:

Questionnaire q = new Questionnaire();
bool? result = q.ShowDialog();

if(result.HasValue && result.Value)
{
    //button was clicked
    var color = q.comboBox1.SelectedIndex;
    switch (color)
    ...
}

Upvotes: 1

Related Questions