简体   繁体   中英

How do I access a Control type variable's possible properties?

I have a Control type variable that will be set to either a combo box or a text box.I cannot access the selection start property from the control variable. This must be because that data type is a parent to controls that do not have that property. How can I get around this to access the selection start property?

The code:

private void createText(string lowerCaseChar, string upperCaseChar)
        {
            Control FocusedTextComboBox;

            switch (lastTextComboBoxFocused)
            {
                case 54:
                    FocusedTextComboBox = SearchTextBox;
                    break;
                case 4:
                    FocusedTextComboBox = VendorComboBox;
                    break;
                case 6:
                    FocusedTextComboBox = VendorComboBox;
                    break;
                case 5:
                    FocusedTextComboBox = DeptComboBox;
                    break;
            }

            if (SearchTextBox.SelectionStart == 0 && SearchTextBox.Text != "")
            {
                switch (shift)
                {
                    case true:
                        FocusedTextComboBox.Text += upperCaseChar;
                        break;
                    case false:
                        FocusedTextComboBox.Text += lowerCaseChar;
                        break;
                }
            }
            else
            {
                int SelectionStartNumber = FocusedTextComboBox.SelectionStart;

                switch (shift)
                {
                    case true:
                        FocusedTextComboBox.Text = FocusedTextComboBox.Text.Insert(FocusedTextComboBox.SelectionStart, upperCaseChar);
                        break;
                    case false:
                        FocusedTextComboBox.Text = FocusedTextComboBox.Text.Insert(FocusedTextComboBox.SelectionStart, lowerCaseChar);
                        break;
                }
                FocusedTextComboBox.SelectionStart = SelectionStartNumber + 1;
            }

            FocusedTextComboBox.Focus();
        }

You are correct that it is a data type issue. You need to cast the object type to textbox to be able to access the selection start property.

if(FocusedTextComboBox is TextBox)
   SelectionStartNumber = (FocusedTextComboBox as TextBox).SelectionStart

Extending user3529814's answer, declare a local variable as a TextBox and cast your control so you can use it throughout the block:

        if (FocusedTextComboBox is TextBox)
        {
            TextBox tb = (TextBox)FocusedTextComboBox;

            if (SearchTextBox.SelectionStart == 0 && SearchTextBox.Text != "")
            {
                switch (shift)
                {
                    case true:
                        tb.Text += upperCaseChar;
                        break;
                    case false:
                        tb.Text += lowerCaseChar;
                        break;
                }
            }
            else
            {
                int SelectionStartNumber = tb.SelectionStart;

                switch (shift)
                {
                    case true:
                        tb.Text = tb.Text.Insert(tb.SelectionStart, upperCaseChar);
                        break;
                    case false:
                        tb.Text = tb.Text.Insert(tb.SelectionStart, lowerCaseChar);
                        break;
                }
                tb.SelectionStart = SelectionStartNumber + 1;
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM