Adding Method to TextBox

  • Thread starter Thread starter Casper
  • Start date Start date
C

Casper

Hi .Net experts,

I am trying to build a user control of textbox and would like to add a
public method to the object like

public void CheckOptions(string str)
{
// codes here
}

So over the WinForm, I would expect the following to work

txtMane.CheckOptions("Hello"); //txtMane is the reference to the user
control

But it does not. How can I make it happen?

Thanks

Casper
 
Hi .Net experts,

I am trying to build a user control of textbox and would like to add a
public method to the object like

public void CheckOptions(string str)
{
// codes here
}

So over the WinForm, I would expect the following to work

txtMane.CheckOptions("Hello"); //txtMane is the reference to the user
control

But it does not. How can I make it happen?

Thanks

Casper

What does not work? Can't you compile it? Do you get a runtime error? Give
some more information.
 
You need to create a control that inherits from TextBox. You then add
your appropriate methods. Possibly overriding existing membeers
depending upon what you need to do. Then instead of using the TExtbox
you find your control in the toolbox after adding a reference to it.
Due to several major flaws in VS.NET's design it is best to create a
seperate Windows Control Library project in the same solution and add a
project reference and then add the dll to your toolbox. This for the
most part is being fixed in vs2k5 so that the controls automatically
show up in vs.net's toolbox.

Hope this helps

#region "Using Directives"
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
#endregion

namespace WindowsApplication2
{
#region "Summary - Description for CustomControl1"
/// <summary>
/// Summary description for CustomControl1.
/// </summary>
#endregion
[ToolboxItem(true)]
public class CustomControl1 : TextBox
{
#region "Variables - Form Designer Controls"
private System.ComponentModel.Container components = null;
#endregion
#region "Constructors"
public CustomControl1()
{
InitializeComponent();
}
#endregion
#region "Destructors"
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
#endregion
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
#region "Methods - Overridden"
protected override void OnPaint(PaintEventArgs pe)
{
// TODO: Add custom paint code here

// Calling the base class OnPaint
base.OnPaint(pe);
}
#endregion
public void CheckOptions()
{
// Your code implementations goes here
}
}
}
 
Marc, Claudio, & Others:

I wonder if I could expand the question a bit. I have an idea for
automating the population of many of the controls on a form. My intention is
to place a unique keyword in the Tag property. Then one set of [inherited]
code will look for the presence of this keyword, and if present, will use it
to retrieve data from a centralized location.

To accomplish this, ideally I'd like to alter the default "Load" method of
the base Control object. That way, this method will be inherited by all of
the native controls.

Is there a simple way to accomplish this?

Robert Werner
MWTech
 
Back
Top