C# 禁止编辑和选择win文本框

C# 禁止编辑和选择win文本框,c#,vb.net,winforms,textbox,C#,Vb.net,Winforms,Textbox,在做一个客户项目时,他们提出了一个奇怪的请求。他们希望winform(vb.net或c#)中有一个功能,当显示模式打开时,没有文本框是可编辑和可选择的。我知道将enabled属性更改为false将解决此问题,但他们希望它处于活动状态,但不可选择。有人能告诉我一些关于这个的想法吗?我想你需要覆盖onPaint事件 如果我没有弄错,这应该很简单,不需要创建自定义控件,只需打开表单设计器文件并使用以下命令: Protected Overrides Sub OnPaint(ByVal e As Syst

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

我想你需要覆盖onPaint事件

如果我没有弄错,这应该很简单,不需要创建自定义控件,只需打开表单设计器文件并使用以下命令:

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

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

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

此控件不会阻止文本框控件的编程选择,但会阻止用户通过鼠标和键盘进行正常选择。使用
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

虽然TnTinMn已经解决了我的问题,但是我得到了一个关于保持文本框的文本颜色完整性的好链接,可能还有关于如何采取新方法来解决这个问题的令人惊奇的事情。 这里是链接, 作家迈克·汤姆林森(Mike Tomlinson)在他的文章中详细描述了他写的每一步

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

/*以下是新文本框控件的完整代码。
/*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
}
灵感来自迈克·汤姆林森和埃特沙姆的帖子*/ 使用制度; 使用系统图; 使用System.Windows.Forms; 使用系统组件模型; 命名空间MyTxtBoxControl { 公共类extTextBox:TextBox { #区域新的可利用变量 const int WM_ENABLE=0xa;//在vb和HA中 彩色前景色; 颜色(不可预着色);; 布尔值_ColorIsSaved=false; 布尔设置颜色=假; //包含默认文本 //当不输入任何文本时,将显示 字符串_DefaultText=“在此处输入文本”; #端区 #新textbox控件的区域构造函数 公共extTextBox() { base.Text=此项。\u DefaultText; this.\u foreColor=this.foreColor; //我的文本控制框事件处理程序 this.TextChanged+=新事件处理程序(onTextChanged); this.KeyPress+=新的KeyPressEventHandler(onKeyPress); this.LostFocus+=新事件处理程序(onTextChanged); this.VisibleChanged+=新事件处理程序(onVisibleChanged); } #端区 #区域事件处理程序方法 受保护的void onTextChanged(对象发送方,事件参数e) { 如果(!String.IsNullOrEmpty(this.Text)) { this.ForeColor=this.\u ForeColor; } 其他的 { this.TextChanged-=新事件处理程序(onTextChanged); base.Text=此项。\u DefaultText; this.TextChanged+=新事件处理程序(onTextChanged); } } 受保护的void onKeyPress(对象发送器、事件参数e) { //按第一个键时替换空文本 string strings=base.Text.Replace(this.\u DefaultText,string.Empty); this.TextChanged-=新事件处理程序(onTextChanged); this.Text=字符串; this.TextChanged+=新事件处理程序(onTextChanged); } 受保护的无效onVisibleChanged(对象发送方,事件参数e) { 如果(!(此颜色已保存且此颜色可见)) { _前景色=此。前景色; _foreColorDisabled=this.ForeColor; _ColorIsSaved=真; 如果(!(此.已启用)) { this.Enabled=true;//然后启用初始化属性 this.Enabled=false;//禁用 } setColor(); } } 受保护的覆盖无效OnForeColorChanged(事件参数e) { 基础。前颜色变化(e); 如果(!\u设置颜色) { _前景色=此。前景色; setColor(); } } 受保护的覆盖无效OnEnabledChanged(事件参数e) { 基础。已更改的唯一性(e); setColor(); } #端区 #区域设置颜色法 私有void setColor() { 如果(_颜色已保存) { _设置颜色=真; 如果(此.Enabled) { this.ForeColor=this.\u ForeColor; } 其他的 { this.ForeColor=this.\u ForeColor禁用; } _设置颜色=假; } } #端区 #区域文本框封装参数覆盖 受保护的重写CreateParams CreateParams { 得到 { CreateParams CP=默认值(CreateParams); 如果(!this.Enabled) { this.Enabled=true; CP=base.CreateParams; 此.Enabled=false; } 其他的 { CP=base.CreateParams; } 返回CP; } } #端区 #区域抑制WM_启用消息 受保护的覆盖无效WndProc(参考消息mesg) { 开关(mesg.Msg) { 案例WM_启用: //防止消息到达控件, //因此,默认过程不会更改颜色。
return;//
他们希望文本框处于活动状态,但不可选择
-如果用户无法选择或修改文本框的内容,则文本框不再处于活动状态,是吗?那么
Enabled=False
有什么问题吗?我也问过他们,但他们想要它。不管怎样,我都会问他们