Click to See Complete Forum and Search --> : Custom TextBox
alexin
April 10th, 2008, 06:51 PM
I need to override the painting behavior of the TextBox class. To be more precise, I don't want to paint the rectangular background and keep the cursor and characters.
I tried overriding both OnPaint() and OnPaintBackground methods, but the background is always painted. Am I missing another painting method?
zdavis
April 10th, 2008, 10:29 PM
did you check msdn to see what methods are available?
alexin
April 11th, 2008, 11:45 AM
Yes, I checked. So far I only found the two methods referred above to be related to drawing.
I made something similar with a Form, by overriding OnPaintBackground() to draw a custom background. Now, I extended the TextBox class and overrode the above methods with an empty implementation, expecting nothing to be drawn. But that's not the case, the TextBox is ormally drawn and so it seems the painting is handled elsewhere; I don't know where.
Any ideas?
Talikag
April 12th, 2008, 03:26 PM
Look at the events' list of the TextBox class. Try to find something that might fit (except OnPaint() and OnPaintBackground()).
Maybe there's a OnCreate event or whatever...
I guess the event you are looking for exists in every form's control...
alexin
April 13th, 2008, 01:00 PM
I'll try but that's a long shot. Where else could the drawing be handled? If not even the *Paint*() methods affect the drawing, another method would be misleading...
JonnyPoet
April 13th, 2008, 04:11 PM
I gooogled a bit around and found you have to set the styles to UserPaint
See the following example done in a standard windwsApplicationusing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TextBoxPaint {
public partial class Form1 : Form {
SpecialBox myBox = new SpecialBox();
public Form1() {
InitializeComponent();
myBox.Location = new Point(10, 10);
myBox.Multiline = true;
myBox.Size = new Size(500, 500);
// I set the background to dark red !!!
myBox.BackColor = Color.Red;
myBox.Text = "Hallo";
this.Controls.Add(myBox);
}
}
public class SpecialBox : TextBox {
public SpecialBox() {
// here we set the style and get a white Background
// and the overriden methods are accessed
this.SetStyle(ControlStyles.UserPaint, true);
this.Invalidate(true);
}
protected override void OnPaint(PaintEventArgs e) {
// your code follows here
base.OnPaint(e);
}
protected override void OnPaintBackground(PaintEventArgs pevent) {
Graphics g = pevent.Graphics;
// your code follows here
//base.OnPaintBackground(pevent);
}
}
}This works -tested
opedog
April 14th, 2008, 08:36 AM
To be more precise, I don't want to paint the rectangular background and keep the cursor and characters.
If I understand you correctly, then simply look at the BorderStyle property of the textbox. You're probably looking for BorderStyle.None.
This would be much easier (and less flickery) than drawing it out on your own.
JonnyPoet
April 14th, 2008, 02:18 PM
If I understand you correctly, then simply look at the BorderStyle property of the textbox. You're probably looking for BorderStyle.None.
Hee ??? How did you get this idea ? He said he doesn't want to get the background painted. So he needs to get the Style changed to have anownerdrawn behaviour. There he could decide what OnPaintBackground does.
opedog
April 15th, 2008, 08:48 AM
Hee ??? How did you get this idea ? He said he doesn't want to get the background painted. So he needs to get the Style changed to have anownerdrawn behaviour. There he could decide what OnPaintBackground does.
I translated rectangular background to be the Border around the textbox itself. I can't decipher whether he wants to just replace the background of the textbox with an image or color or whether he simply wanted to get rid of the border. Apologies if I mistranslated the scope of the problem.
JonnyPoet
April 15th, 2008, 01:35 PM
I translated rectangular background to ....Got your idea:D . Hmm.. we will see what he has wanted, as there is still no answer on this post from the originator.
alexin
April 16th, 2008, 05:01 AM
I tried that to set SetStyle to UserPaint and although the results are more concise with what I want, I'm not there yet. The border is still drawn, so I'll check the border property as suggested.
Actually, I'm doing a favor to a friend. He's coding a custom interface for an a desktop application and, as I'm quite experienced with GUIs, he asked me for help. The problem is I'm experienced with Java and I never had a look at C# before.
That said, he wants a custom text field drawn as an image (he made the layouts with an image editor). Of course, he also wants the characters and caret drawn, so I supposed it would just be a matter of overriding the correct method.
That did work on the frame; we set it non-decorated and overode OnPaintBackground() to display an image, like a skin.
I think you're more clarified now. Thanks for the answers!
alexin
April 22nd, 2008, 09:43 AM
What I want is something very similar to the following:
http://img236.imageshack.us/my.php?image=app2xd9.png
Using this code...using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
namespace EasyDesk{
class TextField : TextBox{
private Bitmap image;
public TextField(){
SetStyle(ControlStyles.UserPaint, true);
BorderStyle = BorderStyle.None;
Size = new Size(100, 50);
Invalidate(true);
Bitmap image = new Bitmap("EasyDesk/textfield.png");
}
protected override void OnPaintBackground(PaintEventArgs paintEvnt){
//paintEvnt.Graphics.DrawImage(image, 0, 0);
}
protected override void OnPaint(PaintEventArgs paintEvnt){
paintEvnt.Graphics.DrawImage(image, 0, 0);
}
}
}
the result is...http://img141.imageshack.us/my.php?image=appkh0.png
What do I need to do to make the text fields like they appear in the first image?
JonnyPoet
April 24th, 2008, 04:37 AM
...That said, he wants a custom text field drawn as an image (he made the layouts with an image editor). Of course, he also wants the characters and caret drawn..
This isn't how it is done: Carets have nothing to do with creating an ownerdrawn usercontrol. Thats done in setting the correct cursor and sure you can use an owner designed one. But thats very different from ownerdrawing the control. In the beginning you told you want to delete the background inside the control. Border has nothing to do with that. You wrote
I need to override the painting behavior of the TextBox class. To be more precise, I don't want to paint the rectangular background and keep the cursor and characters.
I tried overriding both OnPaint() and OnPaintBackground methods, but the background is always painted. ... Therefore I gave you the example you mentioned with the code. If you want to create your textbox totally different from any other one... reinventing the wheel :D - ok go on. But normally its easier to exactly define what is needed and wanted. Give a picture of how the result should look like and whats its properties, events and methods should be able to do and it would be much easier to help you. Doing this is not a wasted time. Its 'sometimes' ;) necessary to get the whole design defined before going on work. Try and error isn't the way to do it. You only will get useless answers and causing waste of our time. So please fully define your needs. And also define if it's done for Windows applications or if it should be used in the web, because controls done for the web are basically designed different. ( They have an other behaviour and there are other tools to be used like WPF, Silverlight...) I'm asking that because what you have shown is a web application !
alexin
April 24th, 2008, 06:01 AM
Thanks for the answer.
The application isn't web based but, yes, a regular Windows one.
The design is ready, we have all the layouts and other "things" prepared. Now it's time to implement it!
Give a picture of how the result should look like and whats its properties, events and methods should be able to do and it would be much easier to help you.
Concerning text fields (TextBox), we're aiming for a look that you can see in the first image of my previous post. The rest isn't a 'mockup', but the actual application.
For now, we need to transform only the look of a standard TextBox to match the one mentioned above. It's behavior is to remain unaltered.
Thanks for your time.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.