473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Inherit Textbox


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()'

Nov 17 '05 #1
5 7097
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]
- mv*@spam.guard.caspershouse.com

"Mike L" <Ca***@nospam.nospam> wrote in message
news:9C**********************************@microsof t.com...

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()'

Nov 17 '05 #2
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.txtDeale rNum);

//
// 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 [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"Mike L" <Ca***@nospam.nospam> wrote in message
news:9C**********************************@microsof t.com...

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()'


Nov 17 '05 #3
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.

Nov 17 '05 #4
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.
"Bruce Wood" wrote:
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.

Nov 17 '05 #5
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.

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jax | last post by:
I am using a class that inherits from the DataGridTextBoxColumn. It adds a combo box into the column where it displays a selection of choices. The problem I have is that when this comboBox loses...
2
by: afatdog | last post by:
Form1: //----------------------------------------------------------------- public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private...
0
by: afatdog | last post by:
Form1: //----------------------------------------------------------------- public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private...
4
by: David | last post by:
I have trying to have a webform inherit controls from another form and can't get it to work Say I have a form that saves the person's demographic info. ****one.aspx**** //I have an object...
8
by: Issac | last post by:
Hi, I created an Inherit UserControl which inherits textbox with additional property say 'Type'. I used in my forms and everything works fine. But afterward, I want to remove (or rename) such...
11
by: Frank | last post by:
Hello, plse help me on the way. I know how class textboxuser inherits from textbox works. But how is this done: a button and a textbox, if I push the button the textbox is filled with 'great'....
3
by: Yoavo | last post by:
Hi, I have a dialog with 2 TextBox controls. I want to add some functionality to one of the them. I created a class MyTextBox which inherits from TextBox. How can I connect one of the TextBox...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.