Inheriting from a text box.....

  • Thread starter Thread starter UJ
  • Start date Start date
U

UJ

I have a user control that inherits from the standard windows text box. I
have a routine that adds stuff to it. I have compiled it into a dll. I have
added it to my app and I can debug into it. I can see that the text changes
but it doesn't actually update on the screen (the text itself is correct in
the routine in the object but the display is wrong.) I've tried invalidating
the object after I change the text but it still doesn't display right.

What do I need to do to make the screen update?

TIA - Jeff.
 
It all depends on how your inherited class worls. For example, did you
override the Paint method?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A brute awe as you,
a Metallic hag entity, eat us.
 
Bruce,

Here's my code:


using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace MarlinVisualControls
{
/// <summary>
/// Summary description for ResultMessages.
/// </summary>
public class ResultMessages : System.Windows.Forms.TextBox
{
private System.Windows.Forms.TextBox tbResultsMessages;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

private int mNumItemsDisplay = 20;

/// <summary>
/// Number of items that should be displayed in the text box.
Default is 20.
/// </summary>
public int prpNumItemsDisplay
{
get { return mNumItemsDisplay; }
set { mNumItemsDisplay = value; }
}

/// <summary>
/// Constructor.
/// </summary>
public ResultMessages()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();

// TODO: Add any initialization after the InitComponent call
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}

#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()
{
this.tbResultsMessages = new System.Windows.Forms.TextBox();
//
// tbResultsMessages
//
this.tbResultsMessages.Location = new System.Drawing.Point(17,
17);
this.tbResultsMessages.Multiline = true;
this.tbResultsMessages.Name = "tbResultsMessages";
this.tbResultsMessages.ReadOnly = true;
this.tbResultsMessages.ScrollBars =
System.Windows.Forms.ScrollBars.Both;
this.tbResultsMessages.TabIndex = 0;
this.tbResultsMessages.Text = "tbResultsMessages";
this.tbResultsMessages.WordWrap = false;
//
// ResultMessages
//
this.Multiline = true;
this.WordWrap = false;

}
#endregion

/// <summary>
/// OnPaint event. This was created by VS automatically.
/// </summary>
/// <param name="pe"></param>
protected override void OnPaint(PaintEventArgs pe)
{
// TODO: Add custom paint code here

// Calling the base class OnPaint
base.OnPaint(pe);
}


/// <summary>
/// Adds message to text box. Also makes sure there aren't to many
messages already. (With Refresh)
/// </summary>
/// <param name="pvsMessage">Text to add to the text box.</param>
public void AddItemToMessages ( string pvsMessage )
{
tbResultsMessages.Text = DateTime.Now.ToString() + " " +
pvsMessage.Trim() +
Environment.NewLine + tbResultsMessages.Text;
string[] lmsgs = tbResultsMessages.Text.Split('\n');
if ( lmsgs.Length > mNumItemsDisplay )
{
string lsNewMsgs = "";
for ( int i = 0; i < mNumItemsDisplay; i++ )
{
string lsNewStr = lmsgs.Replace("\n", "");
lsNewStr = lsNewStr.Replace("\r", "");
lsNewMsgs += lsNewStr + Environment.NewLine;
}
tbResultsMessages.Text = lsNewMsgs;
}
tbResultsMessages.Select(0, 0);
tbResultsMessages.Invalidate();
tbResultsMessages.Refresh();
Application.DoEvents();
}
}
}



TIA - Jeff.
 
Your biggest problem here is that you have _two_ text boxes in this
class.

The first is the object instance itself: "this". Your class _is_ a
TextBox.

The second is the one you declare _inside_ your class,
tbResultsMessages. This is a second, separate text box that is not
parented anywhere and so will not appear on any surface. I have no idea
how you convinced the Visual Studio Designer to put a TextBox on your
TextBox. This looks like a UserControl that was changed to inherit from
TextBox instead, which is another way to do the same thing (but more
laborious, since it requires duplicating all of the TextBox properties
in your UserControl).

So, you do a lot of stuff manipulating tbResultsMessages, but since
that text box doesn't appear anywhere on your screen, you can't see any
of the text.

Get rid of tbResultsMessages and the Designer-generated code, and
change references to tbResultsMessages in AddItemMessages to be "this."
You can get rid of OnPaint, as well: it's not needed. That leaves you
with something like this:

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;


namespace MarlinVisualControls
{
/// <summary>
/// Summary description for ResultMessages.
/// </summary>
public class ResultMessages : System.Windows.Forms.TextBox
{
private int mNumItemsDisplay = 20;

/// <summary>
/// Number of items that should be displayed in the text box.
Default is 20.
/// </summary>
[DefaultValue(20)]
public int prpNumItemsDisplay
{
get { return mNumItemsDisplay; }
set { mNumItemsDisplay = value; }
}

/// <summary>
/// Adds message to text box. Also makes sure there aren't to
many
messages already. (With Refresh)
/// </summary>
/// <param name="pvsMessage">Text to add to the text
box.</param>
public void AddItemToMessages ( string pvsMessage )
{
string newText = DateTime.Now.ToString() + " " +
pvsMessage.Trim() +
Environment.NewLine + this.Text;
string[] lmsgs = newText.Split('\n');
if ( lmsgs.Length > mNumItemsDisplay )
{
string lsNewMsgs = "";
for ( int i = 0; i < mNumItemsDisplay; i++ )
{
string lsNewStr = lmsgs.Replace("\n", "");
lsNewStr = lsNewStr.Replace("\r", "");
lsNewMsgs += lsNewStr + Environment.NewLine;
}
newText = lsNewMsgs;
}
this.Text = newText;
this.Select(0, 0);
}
}
}
 
Bruce,
OK - I get a big DUH! moment!. What you said makes absolutely perfect sense.
I guess I just assumed since I could 'see' the text box on the screen I
needed to add it manually (which is what I did).

You changes worked perfectly.

Thanks!
 

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