473,624 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.EventArg s e)
{
ClassNum.Select All();
}
}

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

Nov 17 '05 #1
5 7135
Mike,

When you say:

ClassNum.Select All();

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.co m

"Mike L" <Ca***@nospam.n ospam> wrote in message
news:9C******** *************** ***********@mic rosoft.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.EventArg s
e)
{
ClassNum.Select All();
}
}

Error: An object reference is required for the nonstatic field, method, or
property 'System.Windows .Forms.TextBoxB ase.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.EventArg s e)
{
this.SelectAll( );
}

private void ClassNum_KeyPre ss(object sender,
System.Windows. Forms.KeyPressE ventArgs e)
{
if (!Char.IsDigit( e.KeyChar))
{
if (!Char.IsContro l(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.txtDealerN um = new LicenseDealerSa les.ClassNum();

this.grpSearchD ealerNum.Contro ls.Add(this.txt DealerNum);

//
// txtDealerNum
//
this.txtDealerN um.Font = new System.Drawing. Font("Arial", 9F,
System.Drawing. FontStyle.Bold, System.Drawing. GraphicsUnit.Po int,
((System.Byte)( 0)));
this.txtDealerN um.Location = new System.Drawing. Point(112, 24);
this.txtDealerN um.MaxLength = 6;
this.txtDealerN um.Name = "txtDealerN um";
this.txtDealerN um.Size = new System.Drawing. Size(88, 21);
this.txtDealerN um.TabIndex = 0;
this.txtDealerN um.Text = "";
this.txtDealerN um.Leave += new
System.EventHan dler(this.txtDe alerNum_Leave);
this.txtDealerN um.TextChanged += new
System.EventHan dler(this.txtDe alerNum_TextCha nged);


"Nicholas Paldino [.NET/C# MVP]" wrote:
Mike,

When you say:

ClassNum.Select All();

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.co m

"Mike L" <Ca***@nospam.n ospam> wrote in message
news:9C******** *************** ***********@mic rosoft.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.EventArg s
e)
{
ClassNum.Select All();
}
}

Error: An object reference is required for the nonstatic field, method, or
property 'System.Windows .Forms.TextBoxB ase.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(KeyP ressEventArgs 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(KeyP ressEventArgs 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
2159
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 focus I lose the selected text. And this next line isn't setting the text in the cell. // code within the custom DataGridComboBoxColumn class this.TextBox.Text = this.ComboBox.SelectedItem.ToString();
2
1988
by: afatdog | last post by:
Form1: //----------------------------------------------------------------- public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private System.ComponentModel.IContainer components = null; public Form1() { // This call is required by the Windows Form Designer.
0
1217
by: afatdog | last post by:
Form1: //----------------------------------------------------------------- public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private System.ComponentModel.IContainer components = null; public Form1() { // This call is required by the Windows Form Designer.
4
428
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 to save the person's name in code behind protected void SavePersonInfo(Person p)
8
1692
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 property. I find that the auto inserted code (InitializeComponent) for that UserControl's property doesn't get update, and I got tons of errors from wherever I referenced it. Do I need to manually update InitializeComponent module in every...
11
1295
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'. These two controls I want to pack into something and use it as a whole in other forms. Is this done with a container, component? Plse supply a small example. Thanks a lot Frank
3
4663
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 controls to my new class ? Yoav.
0
8234
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8172
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8620
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8474
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7158
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4079
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.