【问题标题】:Disabling a win text box from editing and selecting禁止 win 文本框编辑和选择
【发布时间】:2017-08-30 20:55:06
【问题描述】:

做一个客户的项目,他们提出了一个奇怪的要求。他们希望在 winform(vb.net 或 c#)中有一个功能,当显示模式打开时,没有文本框可以编辑和选择。我知道将 enabled 属性更改为 false 将解决它,但他们希望它处于活动状态但不可选择。谁能给我一些想法?

【问题讨论】:

  • they want it to be active but not selectable - 如果用户不能选择或修改文本框的内容,那么它就不再是活跃的了,是吗?那么Enabled = False 有什么问题呢?
  • 我也问过他们,但他们想要。无论如何,我认为我必须为他们创建一个自定义文本框控件,因为他们希望文本的颜色保持不变,我的客户非常喜欢 chrimson 颜色:)。自定义文本框控件可能会解决我的问题。然后,我将禁用该框,并添加一个新属性以在禁用文本框时设置文本颜色。我认为这是正确的解决方案,你说呢?
  • 我不知道...这听起来像是一个骇人听闻的解决方法,试图在被禁用时保持文本完整(甚至不知道这是否可能,它是负责绘制它的运行时大大地)。如果是我,我会创建一个自定义TextBox,设置ReadOnly = True 并尝试阻止选择。这个答案应该可以转换为 VB.NET,例如使用 Telerik: stackoverflow.com/a/31419879/3740093
  • 这似乎回答了阻止文本框选择:stackoverflow.com/questions/13256720/…
  • 感谢该链接,是的,我认为创建自定义文本框是解决此类奇怪客户请求的唯一方法。虽然该功能不是那么有用,但他们希望在他们的应用程序中使用它。我将要求他们多付一分钱作为额外的请求履行。再次感谢,完成工作后我会在这里加载自定义代码。

标签: c# vb.net winforms textbox


【解决方案1】:

我从您对另一个答案的评论中看到您选择了控件交换解决方案。但是,我认为以下内容可能对寻求类似行为的人有用。

此控件不会阻止对 TextBox 控件的编程选择,但会阻止用户通过鼠标和键盘进行正常选择。使用Selectable 属性启用/禁用选择。

Public Class TB : Inherits TextBox
    Public Sub New()
        SetStyle(ControlStyles.Selectable, False)
    End Sub

    Public Property Selectable As Boolean
        Get
            Return GetStyle(ControlStyles.Selectable)
        End Get
        Set(value As Boolean)
            SetStyle(ControlStyles.Selectable, value)
        End Set
    End Property

    Private Const WM_MOUSEACTIVATE As Integer = &H21
    Private Const NOACTIVATEANDEAT As Integer = &H4

     Protected Overrides Sub WndProc(ByRef m As Message)
        If m.Msg = WM_MOUSEACTIVATE AndAlso Not Me.Selectable Then
            m.Result = New IntPtr(NOACTIVATEANDEAT)
            Return
        End If
        MyBase.WndProc(m)
     End Sub
End Class

【讨论】:

  • 谢谢,我的想法是正确的,这对任何需要这种实现的人都有用。
  • 我在这里发布了一个代码作为文本框控件的最终代码。您可以考虑查看它以进行任何改进或任何必要的更正。提前致谢。
【解决方案2】:

我猜你需要重写 onPaint 事件!

如果我没记错的话,这应该是这么简单,不需要创建自定义控件,只需打开表单设计器文件并使用以下内容:

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)
        TextBox1.BackColor = Color.White
End Sub

您可以循环所有应该很容易的控件。

【讨论】:

  • 我有一个很好的解决方案,也有些有趣,虽然它会添加一些代码行,但不像编写一个新控件。将有一个文本框和标签以及一个与文本框大小相同的面板。当编辑模式打开时,用户将看到文本框来编辑他想要的任何内容,然后当显示模式打开时,用户将看到标签和面板,其中标签将具有相同的颜色文本框的文本颜色和文本框的值相同。一个不错的解决方法,但自定义文本框会比这更好。无论如何谢谢。
【解决方案3】:

虽然 TnTinMn 已经解决了我的问题,但我得到了一个关于保持文本框的文本颜色完整的很好的链接,并且可能是关于如何采用新方法来解决这个问题的令人惊叹的事情。 链接在这里, https://www.experts-exchange.com/articles/10842/A-New-Approach-for-Custom-Colors-in-a-Disabled-VB-Net-WinForms-TextBox.html 作者 Mike Tomlinson 非常详细地介绍了他在帖子中所写的每一个步骤。

TnTinMn 方法对于解决这类问题也很有用。

【讨论】:

    【解决方案4】:
    /*Here is the full code of new text box control.
    Inspired by the Post made by Mike Tomlinson and Ehtesham*/
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.ComponentModel;
    namespace MyTxtBoxControl
    {
    public class extTextBox:TextBox
    {
    #region New Utilisable Variables
        const int WM_ENABLE = 0xa; //in vb &HA
        Color _foreColor;
        Color _foreColorDisabled;
        Boolean _ColorIsSaved = false;
        Boolean _settingColor = false;
        //included a default text
        //that will be shown when no text will be entered
        String _DefaultText = "Enter Text Here";
    #endregion
    #region Constructor of the new textbox control
        public extTextBox()
        {
            base.Text = this._DefaultText;
            this._foreColor = this.ForeColor;
            //My Text Control Box event handlers
            this.TextChanged += new EventHandler(onTextChanged);
            this.KeyPress += new KeyPressEventHandler(onKeyPress);
            this.LostFocus += new EventHandler(onTextChanged);
            this.VisibleChanged+=new EventHandler(onVisibleChanged);
        }
    #endregion
    #region Event Handler Methods
        protected void onTextChanged(object sender, EventArgs e)
            {
                if (!String.IsNullOrEmpty(this.Text))
                {
                    this.ForeColor = this._foreColor;
                }
                else
                {
                    this.TextChanged-=new EventHandler(onTextChanged);
                    base.Text = this._DefaultText;
                    this.TextChanged+=new EventHandler(onTextChanged);
                }
            }
            protected void onKeyPress(object sender, EventArgs e)
            {
                //Replaces empty text as the firt key will be pressed
                string strings = base.Text.Replace(this._DefaultText, string.Empty);
                this.TextChanged -= new EventHandler(onTextChanged);
                this.Text=strings;
                this.TextChanged += new EventHandler(onTextChanged);
            }
            protected void onVisibleChanged(object sender, EventArgs e)
            {
                if (!(this._ColorIsSaved & this.Visible))
                {
                    _foreColor = this.ForeColor;
                    _foreColorDisabled = this.ForeColor;
                    _ColorIsSaved = true;
    
                    if (!(this.Enabled))
                    {
                        this.Enabled = true; //Enable to initialize the property then
                        this.Enabled = false;//disabling 
                    }
                    setColor();
                }
            }
    
            protected override void OnForeColorChanged(EventArgs e)
            {
                base.OnForeColorChanged(e);
                if (!_settingColor)
                {
                    _foreColor = this.ForeColor;
                    setColor();
                }
            }
            protected override void OnEnabledChanged(EventArgs e)
            {
                base.OnEnabledChanged(e);
                setColor();
            }
    #endregion
    #region Setcolor method
            private void setColor()
            {
                if (_ColorIsSaved)
                {
                    _settingColor = true;
                    if (this.Enabled)
                    {
                        this.ForeColor = this._foreColor;
                    }
                    else
                    {
                        this.ForeColor = this._foreColorDisabled;
                    }
                    _settingColor = false;
                }
            }
    #endregion
    #region TextBox Encapsulation Parameter overriding 
            protected override CreateParams CreateParams
            {
                get
                {
                    CreateParams CP = default(CreateParams);
                    if (!this.Enabled)
                    {
                        this.Enabled = true;
                        CP = base.CreateParams;
                        this.Enabled = false;
                    }
                    else
                    {
                        CP = base.CreateParams;
                    }
                    return CP;
                }
            }
    #endregion
    #region supressing WM_ENABLE message
            protected override void WndProc(ref Message mesg)
            {
                switch (mesg.Msg)
                {
                    case WM_ENABLE:
                   // Prevent the message from reaching the control,
                   // so the colors don't get changed by the default procedure.
                        return;// <-- suppress WM_ENABLE message
                }
                base.WndProc(ref mesg);//system you can do the rest disable process
            }
    #endregion
    #region User Defined Properties
            ///<summery>
            ///Property to set Text
            ///</summery>
            [Browsable(true)]
            [Category("Properties")]
            [Description("Set TextBox Text")]
            [DisplayName("Text")]
            public new String Text
            {
                get
                {
                    //It is required for validation for TextProperty
                    return base.Text.Replace(this._DefaultText, String.Empty);
                }
                set
                {
                    base.Text = value;
                }
            }
            ///<summery>
            ///Property to get or Set Default text at Design/Runtime
            ///</summery>
            [Browsable(true)]
            [Category("Properties")]
            [Description("Set default Text of TextBox, it will be shown when no text will be entered by the user")]
            [DisplayName("Default Text")]
            public String DefaultText
            {
                get
                {
                    return this._DefaultText;
                }
                set
                {
                    this._DefaultText = value;
                    base.OnTextChanged(new EventArgs());
                }
            }
    #endregion
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-23
      • 2012-06-26
      相关资源
      最近更新 更多