简体   繁体   English

如何访问控件类型变量的可能属性?

[英]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. 您需要将object类型转换为textbox ,才能访问选择开始属性。

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: 扩展user3529814的答案,将局部变量声明为TextBox并转换控件,以便可以在整个块中使用它:

        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;
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果对象的引用变量是超类类型,是否可以访问子类的属性? - Is it possible to access properties of a subclass if the object's reference variable is a superclass type? 如何使用实时按钮访问另一个表单的属性? - How do I access another form's properties with a button live? 我如何checkbox.Checked使用“控制”类型的变量? - How do I checkbox.Checked using a variable of type “Control”? C# 如何“一般地”将 UI 控件引用传递给 class 模块并访问它的属性? - C# How can I pass a UI control reference to a class module "generically" and access it' s properties? 如何通过用户控件标记访问母版页属性? - How do I get access to master page properties via user control markup? 在共享webforms代码隐藏以进行用户控制时,如何访问公共属性 - How do I access public properties when sharing webforms codebehind for user control 如何在用户控件中访问子控件的HtmlControl.Style? - How do I access sub-control's HtmlControl.Style in user control? 如何从aspx页面访问silverlight控件的属性和方法? - How to access a silverlight control's properties and methods from an aspx page? 如果我以编程方式加载它,如何访问用户控件属性? - How to access user control properties if i load it programatically? 从UserControl的属性访问内部控件的所有属性 - Access from UserControl's properties to all of a inside control's properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM