Readonly propert of a Textbox

  • Thread starter Thread starter Ryan
  • Start date Start date
R

Ryan

How do I override a Readonly property of a textbox control in a custom
Textbox control?
 
I have created a custom Textbox control. When this control is placed on a
form; the ReadOnly property is not available.
I have implemented some properties, code below, but don't know exactly how
to implement Readonly property:


using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace MyControls
{

public partial class MyTextBox : UserControl
{
public string currentText;
public delegate void TextChangedHandler(object MyTextBox,EventArgs e);
public event TextChangedHandler OnTextChange;
public MyTextBox()
{
InitializeComponent();
baseTextBox.Text = "";

}
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);

}
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);

}
 
I have created a custom Textbox control. When this control is placed on a
form; the ReadOnly property is not available.
I have implemented some properties, code below, but don't know exactly how
to implement Readonly property:

using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace MyControls
{

    public partial class MyTextBox : UserControl
    {
        public string currentText;
        public delegate void TextChangedHandler(object MyTextBox,EventArgs e);
        public event TextChangedHandler OnTextChange;
        public MyTextBox()
        {
            InitializeComponent();
            baseTextBox.Text = "";

        }
        protected override void OnGotFocus(EventArgs e)
        {
                base.OnGotFocus(e);

        }
        protected override void OnLostFocus(EventArgs e)
        {
                base.OnLostFocus(e);

        }






- Show quoted text -

And how your control is a textbox? You are deriving from UserControl,
no Textbox
 
By the "baseTestBox", it rather looks like you have *encapsulated* a
TextBox - i.e. you have a UserControl that *contains* a TextBox. In
which case, just add a pass-thru ReadOnly property:

public bool ReadOnly
{
get {return baseTextBox.ReadOnly;}
set {baseTextBox.ReadOnly = value;}
}

For info, the overrides you have added don't do anything, so should be
removed. There is no need to declare a custom delegate - this
signature is the same as EventHandler, which is expected by several
binding frameworks. The event could again be a pass-thru, except this
would obscure the sender. For info, the event should be called
TextChanged if you want binding notifications to work.

But I suspect you might get more joy by simply inheriting from
TextBox.

Marc
 
Back
Top