Inherit Textbox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

This is my first attempt at inheriting a class. I want to inherit textbox
class to my derived class ClassNum.

ClassNum will override the TextChanged, Leave, KeyPress and Enter methods.

So, far I was able to inherit the textbox class to ClassNum, but I get
errors when I try to override the methods.

class ClassNum : System.Windows.Forms.TextBox
{
private void ClassNum_Enter(object sender, System.EventArgs e)
{
ClassNum.SelectAll();
}
}



Error: An object reference is required for the nonstatic field, method, or
property 'System.Windows.Forms.TextBoxBase.SelectAll()'
 
Mike,

When you say:

ClassNum.SelectAll();

It is assuming you want to call the static method, not the instance
method. Instead, use "this":

this.SelectAll();

This will call SelectAll on the instance.

Hope this helps.
 
Thanks that fixed the errors, but the code in the class does not run.

BTW this is for a win form.
Here is my class.

class ClassNum : System.Windows.Forms.TextBox
{
private void ClassNum_Enter(object sender, System.EventArgs e)
{
this.SelectAll();
}

private void ClassNum_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar))
{
if (!Char.IsControl(e.KeyChar))
e.Handled = true;
}
}

}


Here is the call to the class from the form class. I'm only showing code
that is relevate to the problem.

public class frmDataEntry : System.Windows.Forms.Form
{
internal ClassNum txtDealerNum;
this.txtDealerNum = new LicenseDealerSales.ClassNum();

this.grpSearchDealerNum.Controls.Add(this.txtDealerNum);

//
// txtDealerNum
//
this.txtDealerNum.Font = new System.Drawing.Font("Arial", 9F,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));
this.txtDealerNum.Location = new System.Drawing.Point(112, 24);
this.txtDealerNum.MaxLength = 6;
this.txtDealerNum.Name = "txtDealerNum";
this.txtDealerNum.Size = new System.Drawing.Size(88, 21);
this.txtDealerNum.TabIndex = 0;
this.txtDealerNum.Text = "";
this.txtDealerNum.Leave += new
System.EventHandler(this.txtDealerNum_Leave);
this.txtDealerNum.TextChanged += new
System.EventHandler(this.txtDealerNum_TextChanged);




Nicholas Paldino said:
Mike,

When you say:

ClassNum.SelectAll();

It is assuming you want to call the static method, not the instance
method. Instead, use "this":

this.SelectAll();

This will call SelectAll on the instance.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Mike L said:
This is my first attempt at inheriting a class. I want to inherit textbox
class to my derived class ClassNum.

ClassNum will override the TextChanged, Leave, KeyPress and Enter
methods.

So, far I was able to inherit the textbox class to ClassNum, but I get
errors when I try to override the methods.

class ClassNum : System.Windows.Forms.TextBox
{
private void ClassNum_Enter(object sender, System.EventArgs
e)
{
ClassNum.SelectAll();
}
}



Error: An object reference is required for the nonstatic field, method, or
property 'System.Windows.Forms.TextBoxBase.SelectAll()'
 
Please, when you post to newsgroups, include a complete description of
the problem.
...but the code in the class does not run.

What do you mean "does not run"? Does it die with an exception? If so,
what is the exception message? Does it simply act like a regular
TextBox and not do what you want? Does it do something but not the
right thing?

As well, please include a complete listing of your ClassNum class. What
you have posted here won't do anything, because the event handlers are
never connected to the events. Methinks that you didn't post the
constructor, which has all of the plumbing in it.

One tip, though: you may have better luck doing this:

protected override void OnEnter(System.EventArgs e)
{
base.OnEnter(e);
SelectAll();
}

protected override void OnKeyPress(KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
{
e.Handled = true;
}
base.OnKeyPress(e);
}

rather than hooking up to events. Overriding the "On" methods gives you
more control over the order in which things happen.
 
Overriding the method worked. Thanks for posting the code. I can't stand it
when a reply to my question is given WITHOUT code, like "post the
constructor" I spent 5 hours on the net and scanning through a C# book trying
to find out how to "post a constructor", and never found an answer, but now I
don't care because the sample code you provided solved my problem. Thanks
again for posting the code.
 
When I asked you to "post the constructor", I meant to post a message
to this UseNet group that included the constructor for your interited
TextBox. The class definition you gave didn't appear to be complete.
"Post" refers to posting a message to a newsgroup, not something you do
to your code.

Glad to be of help, nonetheless.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top