473,385 Members | 1,317 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,385 software developers and data experts.

Custom Textbox Class No Worky

Hello:

This should be a simply question for most of you guys. Obviously, I'm
new to C#. I'm trying to add a base form objects base class to my
project, so I can pre-configure textboxes, combos, etc, with a blue
backcolor, to be used on all my forms. I compiled this, but when I
use it on my form, the custom control properties are not being set.

What am I missing here? Many thanks!

Steven

using System;
using System.Windows.Forms;
using System.Drawing;

namespace Chrismon.Form.Objects
{

public class ChrismonTextBox : TextBox
{
public ChrismonTextBox()
{
TextBox ChrismonTextBox = new TextBox();
ChrismonTextBox.BackColor = Color.Blue;
ChrismonTextBox.Width = 200;
ChrismonTextBox.Height = 40;
}

}
}

Nov 15 '05 #1
5 3277
You're actually creating a local object in your constructor which is
probably not what you intended.

Try this..

public ChrismonTextBox() : base () // This is not absolutely necessary,
though
{
this.BackColor = Color.Blue;
this.Width = 200;
this.Height = 40;
}

-vJ
<Steven C> wrote in message
news:7m********************************@4ax.com...
Hello:

This should be a simply question for most of you guys. Obviously, I'm
new to C#. I'm trying to add a base form objects base class to my
project, so I can pre-configure textboxes, combos, etc, with a blue
backcolor, to be used on all my forms. I compiled this, but when I
use it on my form, the custom control properties are not being set.

What am I missing here? Many thanks!

Steven

using System;
using System.Windows.Forms;
using System.Drawing;

namespace Chrismon.Form.Objects
{

public class ChrismonTextBox : TextBox
{
public ChrismonTextBox()
{
TextBox ChrismonTextBox = new TextBox();
ChrismonTextBox.BackColor = Color.Blue;
ChrismonTextBox.Width = 200;
ChrismonTextBox.Height = 40;
}

}
}

Nov 15 '05 #2
You are actually instantiating a new copy, and setting that object's
properties.

Try this instead -- Note the "this.xxx" call, instead of creating a new
object and setting properties on this. Note the call to the base class's
constructor by adding ": base()"

using System;
using System.Windows.Forms;
using System.Drawing

namespace Chrismon.Form.Objects
{
public class ChrismonTextBox : TextBox
{
public ChrismonTextBox() : base()
{
this.BackColor = Color.Blue;
}
}
}
<Steven C> wrote in message
news:7m********************************@4ax.com...
Hello:

This should be a simply question for most of you guys. Obviously, I'm
new to C#. I'm trying to add a base form objects base class to my
project, so I can pre-configure textboxes, combos, etc, with a blue
backcolor, to be used on all my forms. I compiled this, but when I
use it on my form, the custom control properties are not being set.

What am I missing here? Many thanks!

Steven

using System;
using System.Windows.Forms;
using System.Drawing;

namespace Chrismon.Form.Objects
{

public class ChrismonTextBox : TextBox
{
public ChrismonTextBox()
{
TextBox ChrismonTextBox = new TextBox();
ChrismonTextBox.BackColor = Color.Blue;
ChrismonTextBox.Width = 200;
ChrismonTextBox.Height = 40;
}

}
}

Nov 15 '05 #3
Hi Steve,

<Steven C> wrote in message
news:7m********************************@4ax.com...
Hello:

This should be a simply question for most of you guys. Obviously, I'm
new to C#. I'm trying to add a base form objects base class to my
project, so I can pre-configure textboxes, combos, etc, with a blue
backcolor, to be used on all my forms. I compiled this, but when I
use it on my form, the custom control properties are not being set.

What am I missing here? Many thanks!

<snip>

In your current constructor code you are creating, modifying and dumping
a TextBox object that has no connection to the ChrismonTextBox object being
constructed. The constructor should look like this:

public ChrismonTextBox()
{
this.BackColor = Color.Blue;
this.Width = 200;
this.Height = 40;
}

It would also probably be a good idea to override the properties and
supply a new DefaultValueAttribute for each. Not doing this will mean that
the property values in the designer's property grid will appear bold (as if
the user changed them), and if the user "resets" the property value via the
designer, the value will be the default from the base TextBox class.

Regards,
Dan
Nov 15 '05 #4
Awesome! That did the trick. All three of your comments were much
appreciated.

Thanks again;
Steven

Nov 15 '05 #5
<Steven C> wrote:
I'm trying to add a base form objects base class
to my project, so I can pre-configure textboxes,
combos, etc, with a blue backcolor,


If all you want to do is apply certain settings to controls, I'd suggest an
alternate approach that doesn't require any inherited classes. Inheritance
is best used when you really need to extend functionality.

private void Form1_Load(object sender, System.EventArgs e)
{
MakeControlsBlue(this);
}

private void MakeControlsBlue(Control ctl)
{
// Loop over all the child controls
foreach (Control ctlChild in ctl.Controls)
{
// We want textboxes and checkboxes to be blue
if (ctlChild is TextBox || ctlChild is CheckBox)
{
ctlChild.BackColor = Color.SkyBlue;
}

// Deal with any controls of the child control
if (ctlChild.HasChildren)
{
MakeControlsChildrenBlue(ctlChild);
}
}
}

P.

--
www.CL4.org
Nov 15 '05 #6

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

Similar topics

3
by: Frank Wisniewski | last post by:
I have the following persudo code: //My Form Class class Form1 { //Local Variable for my custom object private MyCustomObject1 //Constructor for Form Class public Constructor{
10
by: Rigs | last post by:
Hi, I have a textbox with a Custom Validator that utilizes the OnServerValidate method for that textbox. This works fine, however the method only executes when data exists in that textbox after...
1
by: Sanjay Pais | last post by:
I built a custom control for all the basic web.ui.controls like textbox, label, checkbox etc etc. I added my custom attribute called ApplySecurity to the html in the page. However, when I cycle...
4
by: Antuane | last post by:
i'm trying to create a custom textbox class, by simply creating a new class & inheriting from the textbox class. But i don't have a UI of this class. I.e., how can i set up the default text, color...
1
by: rn5a | last post by:
I want to create a custom control that encapsulates a Button & a TextBox. When the Button is clicked, the user is asked a question using JavaScript confirm (which shows 2 buttons - 'OK' &...
0
by: rn5a | last post by:
A custom control is derived from the WebControl class & encapsulates a TextBox & a Button. When the Button is clicked, the user is shown the JavaScript confirm dialog with the 'OK' & 'Cancel'...
15
by: rizwanahmed24 | last post by:
Hello i have made a custom control. i have placed a panel on it. I want this panel to behave just like the normal panel. The problem i was having is that the panel on my custom control doesnt...
2
by: Michal Valent | last post by:
I would like to fire some custom server control event before Page_Load event like this (from the trace of an aspx page) : Trace Information Category Message From First(s) From Last(s) aspx.page...
4
by: Jeff | last post by:
hi asp.net 2.0 I have created a custom web control, here is it's header: <%@ Control Language="C#" AutoEventWireup="true" CodeFile="EditUserBox.ascx.cs" Inherits="Controls_EditUserBox" %> ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.