C# 设置文本框时未设置文本属性?

C# 设置文本框时未设置文本属性?,c#,winforms,C#,Winforms,这是最奇怪的事情,因为它在某些代码中对我有效,而不是在其他代码中。下面的代码在一个类中,它将TextBox子类化(注意:我不知道这是否重要,但我将Text属性子类化以设置/获取私有字段\u realText) 在下面的代码中,第一个base.Text=this.RealText可以正常工作!!!我们也在方法MaskData()中设置了它,它可以工作!!!!那么,为什么它在if(!field.isSecure)部分不起作用呢?(看看日志了解我的意思)。我尝试在base.Text=temp之后添加I

这是最奇怪的事情,因为它在某些代码中对我有效,而不是在其他代码中。下面的代码在一个类中,它将TextBox子类化(注意:我不知道这是否重要,但我将Text属性子类化以设置/获取私有字段\u realText)

在下面的代码中,第一个base.Text=this.RealText可以正常工作!!!我们也在方法MaskData()中设置了它,它可以工作!!!!那么,为什么它在if(!field.isSecure)部分不起作用呢?(看看日志了解我的意思)。我尝试在base.Text=temp之后添加Invalidate()、Update(),但没有帮助

代码:

日志

编辑:请注意,此方法始终从同一线程调用。它来自服务器通知,告诉我们在其他地方的手机上有一个铃声被按下,然后该线程调用BeginInvoke将其放入GUI/控件线程或其他什么

上述方法的上游代码是

    public void AppendDTMFDigit(string digit)
    {
        log.Info("specified="+field.MaxSpecified+" someone appending dtmf digit on field=" + field.Variable+" fieldMax="+field.Max+" len="+RealText.Length);
        if (field.MaxSpecified && this.RealText.Length >= field.Max)
            return; //shortcut out since we can't exceed max digits

        BeginInvoke(new MethodInvoker(delegate()
        {
            this.RealText = this.RealText + digit;
            log.Info("new realtext=" + this.RealText);
            SetupTextInBox();
        }
        )); 
    }
更多信息:如果我将所有客户端代码更改为停止使用Text属性和RealText属性,然后停止覆盖Text属性,则效果良好。(显然,我不想这样,因为现在我不能在不改变大量引用RealText属性的客户端代码的情况下,轻松地从控件切换到TextBox再切换回来……呃,可能不得不接受这种情况……似乎有点奇怪

更多信息:调试器介入其中,这很奇怪

两件非常奇怪的事情

  • 它进入的是“接球手”,而不是“接球手”
  • 它进入我的文本属性而不是TextBox的文本属性
  • grrrrr,为什么…听起来像个大错误,对吗?我的意思是,基本。文本应该指超类的基本,对吗?迪安·希勒刚刚编辑

    添加文本方法属性代码

        public override string Text
        {
            get
            {
                return RealText;
            }
            set
            {
                if (value == null)
                    throw new ArgumentException("Not allowed to set RealText to null.  Set to empty string instead");
                RealText = value;
            }
        }
    

    此用户控件的完整源代码,除非在此处注释掉Text属性,否则将无法工作(您必须使用RealText属性,并将该属性公开给客户端,因为base.Text似乎无法正常工作)

    使用系统;
    使用System.Collections.Generic;
    使用系统组件模型;
    使用系统图;
    使用系统数据;
    使用System.Linq;
    使用系统文本;
    使用System.Windows.Forms;
    使用log4net;
    使用IntraNext.Win32.Config;
    命名空间AlpineAccess.Plugins.SecureTalkPlugin
    {
    公共部分类SecureTextBox:TextBox
    {
    private static readonly ILog log=LogManager.GetLogger(typeof(SecureTextControl));
    私有静态只读颜色Color\u FOCUSED=Color.Yellow;
    专用静态只读颜色聚焦手动模式=Color.PaleGreen;
    私人焦点事件焦点监听器;
    私人领域;
    私有bool-isInManualMode;
    私有事件处理程序文本已更改;
    私有字符串不动文本;
    公共安全文本框()
    {
    初始化组件();
    RealText=“”;
    }
    内部无效初始化(字段、FocusEvent focusList、EventHandler textChanged)
    {
    this.focusListener=focusList;
    this.textChanged=textChanged;
    this.field=字段;
    this.Enter+=新事件处理程序(SecureTextBox\u Enter);
    this.Leave+=新事件处理程序(SecureTextBox\u Leave);
    this.TextChanged+=新事件处理程序(SecureTextBox\u TextChanged);
    MenuItem mnuItemNew=新MenuItem();
    mnuItemNew.Text=“&Clear”;
    //这个文本框已经有一个上下文菜单了,但是这里我们给它分配了一个新的上下文菜单
    //我们的文本框替换了我们不想要的剪切、粘贴等内容的旧文本框
    //代理正在使用。。。
    this.ContextMenu=新建ContextMenu();
    this.ContextMenu.MenuItems.Add(mnuItemNew);
    mnuItemNew.Click+=新建事件处理程序(清除\u单击);
    切换模式();
    }
    void SecureTextBox\u TextChanged(对象发送方,事件参数e)
    {
    if(isInManualMode)//确保在手动模式下,我们在realText字段中保持最新更改
    RealText=文本;
    textChanged(发送者,e);
    }
    无效清除\u单击(对象发送者,事件参数e)
    {
    ClearAll();
    }
    内部无效设置手动模式(bool手动)
    {
    如果(isInManualMode==inManual)
    return;//我们不在乎是否没有变化,所以返回;
    isInManualMode=InManualMode;
    切换模式();
    }
    void SecureTextBox_Leave(对象发送方,事件参数e)
    {
    log.Info(“exiting=“+field.Variable”);
    focusListener(field.Variable,false,this.RealText);
    BeginInvoke(新方法调用程序(委托)()
    {
    ChangeBackground();
    }
    ));            
    }
    void SecureTextBox\u输入(对象发送方,事件参数e)
    {
    log.Info(“输入=”+字段变量);
    focusListener(field.Variable,true,this.RealText);
    BeginInvoke(新方法调用程序(委托)()
    {
    ChangeBackground();
    }
    ));
    }
    私有void SwitchModes()
    {
    SetupTextInBox();
    ChangeBackground();
    }
    私有void SetupTextInBox()
    {
    if(isInManualMode)
    {
    this.ReadOnly=false;
    base.Text=RealText;
    }
    如果(!field.IsSecure)
    {
    this.ReadOnly=true;
    字符串temp=RealText;
    base.Text=temp;
    使无效();
    log.Info(“txt=“+base.Text+”temp=“+temp”);
    }
    else//不是手动模式,并且是安全的,所以请屏蔽它并使其为只读
    {
    this.ReadOnly=true;
    MaskData();
    }
    }
    私有void MaskData()
    {
    
        public void AppendDTMFDigit(string digit)
        {
            log.Info("specified="+field.MaxSpecified+" someone appending dtmf digit on field=" + field.Variable+" fieldMax="+field.Max+" len="+RealText.Length);
            if (field.MaxSpecified && this.RealText.Length >= field.Max)
                return; //shortcut out since we can't exceed max digits
    
            BeginInvoke(new MethodInvoker(delegate()
            {
                this.RealText = this.RealText + digit;
                log.Info("new realtext=" + this.RealText);
                SetupTextInBox();
            }
            )); 
        }
    
        public override string Text
        {
            get
            {
                return RealText;
            }
            set
            {
                if (value == null)
                    throw new ArgumentException("Not allowed to set RealText to null.  Set to empty string instead");
                RealText = value;
            }
        }
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using log4net;
    using IntraNext.Win32.Config;
    
    namespace AlpineAccess.Plugins.SecureTalkPlugin
    {
    
        public partial class SecureTextBox : TextBox
        {
            private static readonly ILog log = LogManager.GetLogger(typeof(SecureTextControl));
    
            private static readonly Color COLOR_FOCUSED = Color.Yellow;
            private static readonly Color COLOR_FOCUSED_MANUALMODE = Color.PaleGreen;
    
            private FocusEvent focusListener;
            private Field field;
            private bool isInManualMode;
            private EventHandler textChanged;
            private string RealText;
    
            public SecureTextBox()
            {
                InitializeComponent();
                RealText = "";
            }
    
            internal void Initialize(Field field, FocusEvent focusList, EventHandler textChanged)
            {
                this.focusListener = focusList;
                this.textChanged = textChanged;
                this.field = field;
    
                this.Enter += new EventHandler(SecureTextBox_Enter);
                this.Leave += new EventHandler(SecureTextBox_Leave);
                this.TextChanged += new EventHandler(SecureTextBox_TextChanged);
    
                MenuItem mnuItemNew = new MenuItem();
                mnuItemNew.Text = "&Clear";
    
                //THIS textbox HAS a context menu ALREADY BUT here we assign a new context menu to
                //our text box to replace the old one which had cut, paste etc. that we don't want
                //the agent using...
                this.ContextMenu = new ContextMenu();
                this.ContextMenu.MenuItems.Add(mnuItemNew);
    
                mnuItemNew.Click += new EventHandler(clear_Click);
    
                SwitchModes();
            }
    
            void SecureTextBox_TextChanged(object sender, EventArgs e)
            {
                if(isInManualMode) //make sure if in manual mode, we keep changes up to date in realText field
                    RealText = Text;
                textChanged(sender, e);
            }
    
            void clear_Click(object sender, EventArgs e)
            {
                ClearAll();
            }
    
            internal void SetManualMode(bool inManual)
            {
                if (isInManualMode == inManual)
                    return; //we don't care if there is no change so return;
    
                isInManualMode = inManual;
                SwitchModes();
            }
    
            void SecureTextBox_Leave(object sender, EventArgs e)
            {
                log.Info("exiting=" + field.Variable);
                focusListener(field.Variable, false, this.RealText);
                BeginInvoke(new MethodInvoker(delegate()
                {
                    ChangeBackground();
                }
                ));            
            }
    
            void SecureTextBox_Enter(object sender, EventArgs e)
            {
                log.Info("entering=" + field.Variable );
                focusListener(field.Variable, true, this.RealText);
                BeginInvoke(new MethodInvoker(delegate()
                {
                    ChangeBackground();
                }
                ));
            }
    
            private void SwitchModes()
            {
                SetupTextInBox();
                ChangeBackground();
            }
    
            private void SetupTextInBox()
            {
                if (isInManualMode)
                {
                    this.ReadOnly = false;
                    base.Text = RealText;
                }
                else if (!field.IsSecure)
                {
                    this.ReadOnly = true;
                    string temp = RealText;
                    base.Text = temp;
                    Invalidate();
                    log.Info("txt=" + base.Text + " temp=" + temp);
                }
                else //not manual mode and IsSecure so mask it and make it readonly
                {
                    this.ReadOnly = true;
                    MaskData();
                }
            }
    
            private void MaskData()
            {
                log.Debug("mask=" + this.field.NumBeginDigitsToMaskSpecified + " num=" + field.NumBeginDigitsToMask + " txtLen=" + RealText.Length);
                int numDigitsToMask = RealText.Length;
                if (this.field.NumBeginDigitsToMaskSpecified && this.field.NumBeginDigitsToMask < RealText.Length)
                {
                    int numDigits = this.field.NumBeginDigitsToMask;
                    string maskedPart = "".PadLeft(numDigits, '●');
                    string unmasked = RealText.Substring(numDigits);
                    string full = maskedPart + unmasked;
                    base.Text = full;
                }
                else
                {
                    log.Debug("masking all digits");
                    base.Text = "".PadLeft(RealText.Length, '●');
                }
            }
    
            private void ChangeBackground()
            {
                if (isInManualMode)
                    SetManualModeColor();
                else
                    SetNonManualModeColor();
            }
    
            private void SetNonManualModeColor()
            {
                if (this.Focused)
                    this.BackColor = COLOR_FOCUSED;
                else
                    this.BackColor = Control.DefaultBackColor;
            }
    
            private void SetManualModeColor()
            {
                if (this.Focused)
                    this.BackColor = COLOR_FOCUSED_MANUALMODE;
                else
                    this.BackColor = Control.DefaultBackColor;
            }
    
            public void AppendDTMFDigit(string digit)
            {
                log.Info("manualmode="+isInManualMode+" specified=" + field.MaxSpecified + " someone appending dtmf digit on field=" + field.Variable + " fieldMax=" + field.Max + " len=" + RealText.Length);
    
                if (isInManualMode)
                    return;
                else if (field.MaxSpecified && RealText.Length >= field.Max)
                    return; //shortcut out since we can't exceed max digits
    
                BeginInvoke(new MethodInvoker(delegate()
                {
                    RealText = RealText + digit;
                    SetupTextInBox();
                }
                )); 
            }
    
            internal void ClearAll()
            {
                log.Info("Cleared textbox for =" + field.Variable);
                base.Text = "";
                RealText = "";
                SetError("");
            }
    
            public override string Text
            {
                get
                {
                    return RealText;
                }
                set
                {
                    if (value == null)
                        throw new ArgumentException("Not allowed to set RealText to null.  Set to empty string instead");
                    RealText = value;
                }
            }
    
            /**
             * Set to "" to clear the error or set anything to make valid
             */
            public void SetError(string error)
            {
                if (!this.IsHandleCreated)
                    return;
    
                SecureTextBox box = this;
                //set to "" to clear the error
                BeginInvoke(new MethodInvoker(delegate()
                {
                    errorProvider1.SetError(box, error);
                }));
            }
        }
    }
    
    RealText = Text;
    
     void SecureTextBox_TextChanged(object sender, EventArgs e)
     {
         if (isInManualMode) //make sure if in manual mode, we keep changes up to date in realText field
         RealText = Text; <---- **THIS CALLS THE OVERIDDEN 'Text' BELOW**
    
         ... snip
    
    public override string Text 
    {
        get { return RealText; }
    }
    
     void SecureTextBox_TextChanged(object sender, EventArgs e)
     {
         if (isInManualMode) //make sure if in manual mode, we keep changes up to date in realText field
         RealText = base.Text; <--- THIS?
    
    private string _realText;
    
    private string RealText;