?
Solved

Extending Textbox in c#

Posted on 2005-04-13
27
Medium Priority
?
4,545 Views
Last Modified: 2008-01-09
Ok guys this is my first question posted to EE..

Anyway I am trying to extend the textbox, which I have done sort of. but what I want to do is do custom borders. So everytime that I have tried to date I do get my custom border, but I also get the default  border as well.

I have even tried in the onPaint method to not call the base.OnPaint.

Anyone have any examples, references that will help me in my quest that would be good.
0
Comment
Question by:cyberdevil67
  • 13
  • 11
  • 2
  • +1
27 Comments
 
LVL 6

Expert Comment

by:TDSnet
ID: 13779267
How much of the existing TextBox functionality do you need..?  Might be easier to just create a new control from scratch rather than one that inherits from the TextBox.

Cheers,
Jack
0
 
LVL 9

Author Comment

by:cyberdevil67
ID: 13779299
Ok, do you know how to do that? Without reinventing the textinput, and events rewriting and the properties?
0
 
LVL 11

Expert Comment

by:Agarici
ID: 13779387
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

by:TheAvenger
ID: 13779393
You can see in the following article what the problems of repainting a text box are and how this guy changed the behavior to create a transparent background. I think you can use a similar solution to create a custom border:

http://www.codeproject.com/cs/miscctrl/alphablendtextbox.asp
0
 
LVL 9

Author Comment

by:cyberdevil67
ID: 13779405
How does that work?

First of all every form control already has a region, secondly it will not stop the parent controls drawing? Seeing as I already have control of the region, I am able to do anything I want to draw to it, but I can't stop the base control from doing what it does in terms of drawing.

0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 13779428
Did you look at the link I sent you?
0
 
LVL 9

Author Comment

by:cyberdevil67
ID: 13780043
Yes,

I have already seen that code. And that code doesn't tell me any more than I already know or done. But thanks anyway, I have search codeproject and the net extensively, but there are no examples on doing custom drawing of the borderstyle. Thats what I am looking for here.
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 13780069
Well, that code shows you how to make full custom drawing. It uses an image to draw the text box and you can draw whatever you want on an image in C#
0
 
LVL 9

Author Comment

by:cyberdevil67
ID: 13780107
Actually you are wrong on that issue...

All it does is use a property to change the background colour and appy transparency, the backgroundstyle is  a different kettle of fish. I am able to do the same thing as I said I have searched codeproject for all the examples I could find, I came across this many times.

I have gone over this code with a fine tooth comb, and have not seen any evdidence that it can override the OnPaint event or allow a differnt form of borderstyle, thats what this question is about.

I can do the same thing this code does already and am not interested in links to code I have seen, I am looking at someone who can explain to me how or what I need to do.
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 13780128
I haven't seen the code itself but here is what the article says:

When the control needs to be painted, it does the following:

1. temporarily sets the UserPaint style to false
2. captures a bitmap of the original TextBox by sending a WM_PRINT message
3. switches back to UserPaint style
4. uses a ColorMap to convert the solid background into a transparent/alpha background
5. figures out where the caret should be and draws it if necessary
6. then copies the new alpha image to the PictureBox, which when drawn will be blended with the background

Point 2 creates an image, point 4 changes the image. So what you need it to replace point 4 with "draw new border" and leave the rest as it is.
0
 
LVL 9

Author Comment

by:cyberdevil67
ID: 13780159
i have read it...

And I have I read the code, and looked at the code many times in the last 6 months. Don't always read what you believe..

The code uses two controls to acheive what it does, that is a fudge I am not looking for. I am still looking for a way to override the borderstyle drawing, nothing more nothing less.
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 13780181
The same article mentions that the user painting of the text box is not possible and if such a workaround does not work for you, you need to create a new control, from scratch.
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 13780251
BTW what is your final goal? Wouldn't it be possible to:
- Make a new user control
- Put a text box on it with border style set to None
- Draw the border around the text box, either using OnPaint or other controls with the appropriate border

The only bad thing is that you will lose all the text box properties and will need to write things like:

public int TextLength { get {return this.textBox.TextLength;}}
0
 
LVL 9

Author Comment

by:cyberdevil67
ID: 13780254
Ok this right...

Then look at

www.purecomponents.com

They have a program that EXTENDS these normal controls, so it is possible.
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 13780279
I don't see a text box in this components. The statement was that overriding of TextBox especially does not work, not that all constrols are impossible to extend
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 13780290
You mentioned in the original post:

"So everytime that I have tried to date I do get my custom border, but I also get the default  border as well."

If you set the border type to none and then draw your own like you do? then the "default border" will actually be empty, or?
0
 
LVL 9

Author Comment

by:cyberdevil67
ID: 13780297
Look your waisting my time, I know its possible I have seen others do it.

The site I gave you has a component called EntrySetExtender, I have been beta testing it for over a month now and I can tell you it extends the textbox control and some others.

If you can't answer my question then don't continue. As I said I know its possible because I have seen $500 components/controls do what I am looking for if its not possible then how do they do it then?
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 13780308
You are very rude and will not get a lot of help here in this way. I tried to think together with you about different solutions but obviously you don't need that. I wish you a lot of luck.
0
 
LVL 9

Author Comment

by:cyberdevil67
ID: 13780311
Ok the border style set to none reduces the area in which to draw in, what this means is that the spacing around the control is reduced, I have tried that too.
0
 
LVL 11

Expert Comment

by:Agarici
ID: 13780342
i think the problem is you are drawing only the client area (i bet you use ClientRectangle to obtain the rectangle to draw)
this does not cover the non client area of the control :)

hth, A.

0
 
LVL 9

Author Comment

by:cyberdevil67
ID: 13780405
No I don't but if I could extend the client area then I might be on the right track.
0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 1000 total points
ID: 13780451
OK, another attempt to help. Try this code:

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

namespace WindowsApplication1
{
      public class MyTextBox : TextBox
      {
            public MyTextBox()
            {
                  this.BorderStyle = BorderStyle.None;
            }

            protected override void WndProc(ref Message messg)
            {
                  if (0x000F == messg.Msg)      // Paint message
                  {
                        Graphics g = Graphics.FromHwnd (this.Parent.Handle);
                        Rectangle r = new Rectangle (
                              this.Left - 1,
                              this.Top - 1,
                              this.Width + 1,
                              this.Height + 1);
                        g.DrawRectangle (new Pen (Color.Red), r);
                        g.Dispose();
                  }
                  else
                        base.WndProc(ref messg);
            }

      }
}
0
 
LVL 9

Author Comment

by:cyberdevil67
ID: 13780521
And where is my text input on that code, it draws a border with no text input. No to mention crashes my IDE after running it.
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 13780533
Do you set anything additionally? Like UserPaint? Because UserPaint must be false in this case
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 13780540
It works just find in my environment (WinXP Pro, VS 2003) and draws a red border around the text box
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 13780569
0
 
LVL 9

Author Comment

by:cyberdevil67
ID: 13780636
TheAvenger,

I really Appreciate your help on this, but your solution is what I have already done.

What I would like is to extend the borderstyle, and that include not reducing the spacing around the input area.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question