Re-Inventing The Label Control - c#

I need to reinvent/recreate the Label Control from scratch to add my own mojoness to it. Yes, I know what you're thinking (and if you're not thinking that, shouldn't you be?).
Can somebody point me in the right direction?
Thank you.
The whole purpose for recreating the label is that I want full control over how it is drawn onto screen, and so that I can have KeyDown Event Handlers for it, too. For example, the user can edit the contents of a label the same way they would edit the contents of a TextBox control.
Also, I cannot just simply use a TextBox control, as it would require almost, if not more work to get my desired result.

Why not just extend the current one?
class MyMojoLabel : Label // Kind of thing

A good starting point would maybe to understand how Microsoft implemented the label.
To get a better in-depth look you should take a look with Reflector into the class or debugging the source code of the Label control.

public class MyLabel : System.Windows.Forms.Label
{
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
// or leave base out
// you would determine these values by the length of the text
e.Graphics.DrawEllipse(new System.Drawing.Pen(System.Drawing.Color.Red),
0, 0, 50, 12);
}
protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
{
base.OnKeyDown(e);
// although a label has a KeyDown event I don't know how it would
// receive focus, maybe you should create a text box that looks
// like a label
}
}
How about this?

Create a class that inherits from Control. Use the SetStyle() call to enable user painting and double-buffering, and override the OnPaint(), and any other methods that you need.
class MyLabel : System.Windows.Forms.Control
{
public MyLabel()
{
this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
ControlPaint.DrawBorder3D( e.Graphics, this.ClientRectangle, Border3DStyle.Etched, Border3DSide.All);
e.Graphics.DrawString(this.Text, this.Font, Brushes.Red, 0, 0);
}
}

Creating your own label control is simple enough, you just need to start with Control and override OnPaint(). What is going to byte is to turn it into a control that also has focusing behavior. And allows the user to edit the text. By the time you're done, you'll have re-invented the TextBox control. Which is a lot harder than it looks.
Focus on the focusing first, that's the trickiest problem. It isn't very likely that the user will want to focus the control frequently. Maybe some kind of secret handshake, like a double-click. When you detect one, you could create a TextBox control and put it in front of the label. And dispose it when it loses focus, updating the label's Text property. Or a simple context menu that displays a small editing dialog.
An example that uses the double-click to edit approach:
using System;
using System.Windows.Forms;
class MyLabel : Label {
private TextBox mEditor;
protected override void OnDoubleClick(EventArgs e) {
if (mEditor == null) {
mEditor = new TextBox();
mEditor.Location = this.Location;
mEditor.Width = this.Width;
mEditor.Font = this.Font;
mEditor.Text = this.Text;
mEditor.SelectionLength = this.Text.Length;
mEditor.Leave += new EventHandler(mEditor_Leave);
this.Parent.Controls.Add(mEditor);
this.Parent.Controls.SetChildIndex(mEditor, 0);
mEditor.Focus();
}
base.OnDoubleClick(e);
}
void mEditor_Leave(object sender, EventArgs e) {
this.Text = mEditor.Text;
mEditor.Dispose();
mEditor = null;
}
protected override void Dispose(bool disposing) {
if (disposing && mEditor != null) mEditor.Dispose();
base.Dispose(disposing);
}
}

Related

Propagate event to all controls

I'm using the following piece of code, for documentation, error handling and/or logging. It's saves an image of the UserControl or Form when I click it pressing Control+Alt+Shift:
public Image GetImage()
{
Bitmap oBmp = new Bitmap(this.Width, this.Height);
this.DrawToBitmap(oBmp, new Rectangle(0, 0, oBmp.Width, oBmp.Height));
return (Image)oBmp;
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
bool bControl = false;
bool bShift = false;
bool bAlt = false;
bControl = (Control.ModifierKeys & Keys.Control) == Keys.Control;
bShift = (Control.ModifierKeys & Keys.Shift) == Keys.Shift;
bAlt = (Control.ModifierKeys & Keys.Alt) == Keys.Alt;
if (bControl && bShift && bAlt)
{
GetImage().Save(this.Name.TimedLocalFileName("png"), ImageFormat.Png);
}
}
Right now, I'm coding it in every UserControl, in the base form and so. It's easy to do because I'm using a code Snippet. But it has obvious setbacks.
The same piece of code in a lot of places (maintainability); and
Works only when I click on the base control and not it's childs (if an UserControl has a Label, this doesn't works.
I've been for a few days analyzing GlobalHooks (mostly here: CodeProject, but my head is not helping me.
Any suggestion will be very much appreciated.
Note: TimedLocalFileName is an extension method that returns a String in format <ControlName>_<Culture>_<YYYYMMDD>_<HHMMSS>.<FileExtension>
Create a base UserControl and name it BaseUserControl and derive all your user control from BaseUserControl then you can put all the logic inside the base user control.
Inside the BaseUserControl, using a recursive method, handle MouseDown event of all child controls and redirect them to OnMouseDown of this, like this post.
Override OnHanldeCrated and call that recursive method to wire up events.
Here is the base control:
using System;
using System.Drawing;
using System.Windows.Forms;
public class BaseUserControl : UserControl
{
void WireMouseEvents(Control container)
{
foreach (Control c in container.Controls)
{
c.MouseDown += (s, e) =>
{
var p = PointToThis((Control)s, e.Location);
OnMouseDown(new MouseEventArgs(e.Button, e.Clicks, p.X, p.Y, e.Delta));
};
WireMouseEvents(c);
};
}
Point PointToThis(Control c, Point p)
{
return PointToClient(c.PointToScreen(p));
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (Control.ModifierKeys == (Keys.Control | Keys.Alt | Keys.Shift))
MessageBox.Show("Handled!");
// Your custom logic
}
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
WireMouseEvents(this);
}
}
Answering issue 1
The same piece of code in a lot of places (maintainability)
It's not always applicable but if you find yourself using this behavior a lot, you could inherit from UserControl to create BitmapExportUserControl and put Image GetImage() and override void OnMouseDown(MouseEventArgs e) in this class instead, and make all your custom controls that need this behavior inherit from BitmapExportUserControl.
Another way could to perform the bitmap export from your Form itself, and have your Form subscribe to all the MouseDown events of all its children Control objects.
Answering issue 2
Works only when I click on the base control and not it's childs
As far as I know, there is no built-in "up" event propagation (or bubbling) in WinForms as there is in WPF for example. A solution could be to expose an event that can be raised by all UserControl in your application when there is a MouseDown event on them. Your code would become:
protected override void OnMouseDown(MouseEventArgs e)
{
GlobalMouseDown.RaiseGlobalMouseDownEvent(this, e);
}
and you would have your main Form subscribe to this GlobalMouseDown.GlobalMouseDownEvent and perform the checks and bitmap export.
This is functionally equivalent to having a public method HandleMouseDown in some GlobalMouseDown class that would be called by all your UserControl MouseDownEventHandlers. The code in each UserControl would become:
protected override void OnMouseDown(MouseEventArgs e)
{
GlobalMouseDown.HandleMouseDown(this, e);
}
and you would do your checks and bitmap export in this method.

Prevent selecting text in a TextBox

Similar questions have been already asked (e.g., here), however I've not found an answer for my specific case. I'm building a custom control based on a DevExpress control, which in turns is based on standard TextBox and I've a flickering problem that seems due to the base TextBox component, which tries to update selection.
Without explaining all the details of my custom control, to reproduce the problem you just need to place a TextBox inside a Form and then use this code:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
textBox1.MouseMove += TextBox1_MouseMove;
}
private void TextBox1_MouseMove(object sender, MouseEventArgs e) {
(sender as TextBox).Text = DateTime.Now.Ticks.ToString();
}
}
If you launch it, you click on the TextBox and then you move the cursor toward right you will notice the flickering problem (see video here). For my custom control I would need to avoid this flickering. I'm bound to use a TextBox (so no RichTextBox). Any idea?
The solution has been provided in the meantime by Reza Aghaei by overriding WndProc and intercepting WM_SETFOCUS messages. See here
Depending on what u want to do there are several solutions:
If you want to prevent the selection it would be:
private void TextBox1_MouseMove(object sender, MouseEventArgs e)
{
(sender as TextBox).Text = DateTime.Now.Ticks.ToString();
(sender as TextBox).SelectionLength = 0;
}
Or for selecting all:
private void TextBox1_MouseMove(object sender, MouseEventArgs e)
{
(sender as TextBox).Text = DateTime.Now.Ticks.ToString();
(sender as TextBox).SelectAll();
}
And besides that u also could specify the conditions for selecting, for example:
private void TextBox1_MouseMove(object sender, MouseEventArgs e)
{
(sender as TextBox).Text = DateTime.Now.Ticks.ToString();
if (MouseButtons == MouseButtons.Left) (sender as TextBox).SelectAll();
else (sender as TextBox).SelectionLength = 0;
}
But as long as you want to select the text you always will get some flickering, because a normal Textbox has not the possibility to use things like BeginEdit and EndEdit and so it will change the text first and then select it.
Looking at the video it looks like your textbox is calling WM_ERASEBKGND unnecessarily. In order to remedy this problem you can subclass the textbox class and intercept these messages. Below is sample code which should do the trick (untested) Disclaimer: I have used this technique for other WinForm controls that had the type of flicker shown in your video but not TextBox. If it does work for you, please let me know. Good luck!
// textbox no flicker
public partial class TexttBoxNF : TextBox
{
public TexttBoxNF()
{
}
public TexttBoxNF(IContainer container)
{
container.Add(this);
InitializeComponent();
//Activate double buffering
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
//Enable the OnNotifyMessage event so we get a chance to filter out
// Windows messages before they get to the form's WndProc
this.SetStyle(ControlStyles.EnableNotifyMessage, true);
}
//http://stackoverflow.com/questions/442817/c-sharp-flickering-listview-on-update
protected override void OnNotifyMessage(Message m)
{
//Filter out the WM_ERASEBKGND message
if (m.Msg != 0x14)
{
base.OnNotifyMessage(m);
}
}
}

Draw a Clickable Square?

I am new to C# but I have a lot of Java experience so I've been told that C# is fairly easy to comprehend based on that.
So far it is. At the moment though, I want to make a simple TicTacToe as part of an exercise. However what I want to do, is draw clickable squares that I can reference so I can check if the box is already clicked or not.
I am currently using Visual Studio Express 2012. I am making a Windows Form Application for Desktop usage.
I looked around for solutions but I can't seem to find something that does this.
How would I go about doing this?
internal sealed class Box : Control
{
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(Point.Empty, new Size(Width - 1, Height - 1)));
}
protected override void OnClick(EventArgs e)
{
MessageBox.Show("You Clicked The Box!");
}
}
Create a class that derives from Control. From here you can override a whole bunch of virtual members including the OnPaint method where you can perform all of your drawing logic. It's all very intuitive with the assistance of IntelliSense (DrawRectangle, Draw Line etc).
If you want you can override OnClick like I did here but otherwise you can subscribe to an the controls' Click event just as you would a standard control.
You can also derive from ContainerControl for your 'grid' which will behave similarly to a Panel or GroupBox control.
Here's a quick example I just put together to get you started. The border is a tiny bit bugged at some resolutions, I'll leave that down to my abysmal mathematics skills.
internal sealed class GameGrid : ContainerControl
{
protected override void OnCreateControl()
{
for (int y = 0; y < 3; y++)
{
for (int x = 0; x < 3; x++)
{
GameButton button = new GameButton
{
Width = Width/3,
Height = Height/3,
};
button.Location = new Point(x*button.Width++, y*button.Height++);
Controls.Add(button);
button.Click += button_Click;
}
}
}
static void button_Click(object sender, EventArgs e)
{
GameButton gameButton = (GameButton)sender;
gameButton.CircleCheck = true;
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(ClientRectangle.Location, new Size(Width - 1, Height - 1)));
}
}
internal sealed class GameButton : Control
{
private bool _cricleCheck;
public bool CircleCheck
{
get
{
return _cricleCheck;
}
set
{
_cricleCheck = value;
Invalidate();
}
}
private readonly Pen circlePen = new Pen(Brushes.Black, 2.0f);
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(ClientRectangle.Location, new Size(Width - 1, Height - 1)));
if (CircleCheck)
{
e.Graphics.DrawEllipse(circlePen, new Rectangle(ClientRectangle.Location.X + 10, ClientRectangle.Location.Y + 10, Width - 30, Height - 30));
}
}
}
The most common clickable square is a button.
If the button is clickable in the current gamestate and what icon should be presented there if it was clicked sounds like a job for your background logic.
Try this:
Create a new Form
Drop a PictureBox control inside the form in order to render the Tic Tac Toe board
Handle the Paint event to do the graphics rendering
Handle the MouseClick event to detect if the user has clicked inside the board and use the event arguments to determine on which of the 9 squares of the board the user has clicked
Hope this helps.
I did a similar exercise 2days ago,
Just check this tutorial, it will be enough I guess:
http://www.codeproject.com/Articles/2400/Tic-Tac-Toe-in-C
And for the Square you can either use buttons or you could use PictureBox like this tutorial (although they used vb.net not c# but it's easy to translate):
http://vbprogramming.8k.com/tutorials/tic-tac-toe.htm
I think the easiest solution is using simple buttons with background color or image.
Have a variable for currently playing user and change
button.Text
on click event.
Set all button texts for "" at the beginning- that way you can check if the button was clicked:
if (button.Text!="")
//it was already clicked

LinkLabel in C# copies its Text on double-click

When I double-click a LinkLabel in WindowsForms it copies its text; how can I prevent this?
BTW, it's a .Net 2.0 application, if that makes any difference.
Thanks
You can always clear the clipboard using :
Clipboard.Clear();
Update :
You can use this code in mouse double click event.
Try this :
private void linkLabel1_MouseDoubleClick(object sender, MouseEventArgs e)
{
Clipboard.Clear();
}
Update 2 :
Use these following codes it won't copy value of linklable and also it keeps your clipboard. you must use these codes with mouse enter event and mouse double click event.
Try this :
public string str;
private void linkLabel1_MouseEnter(object sender, EventArgs e)
{
str = Clipboard.GetText();
linkLabel1.MouseDoubleClick+=new MouseEventHandler(linkLabel1_MouseDoubleClick);
}
private void linkLabel1_MouseDoubleClick(object sender, MouseEventArgs e)
{
Clipboard.SetText(str);
}
It seems that this behaviour is built into the LinkLabel and that there's no way to override it.
Testing reveals the clipboard has already changed by the time the MouseDoubleClick event is triggered.
FWIW, I've never needed this control - a regular Label with some styling and use of the MouseEnter/MouseLeave events has served me well in many projects.
What you can do is to create your own label and derive it from Control as public class MyLabel : Control and then draw the text in it yourself as
protected override void OnPaint(PaintEventArgs e)
{
SolidBrush TextBrush = new SolidBrush(this.ForeColor);
TextRenderer.DrawText(e.Graphics, this.Text, this.Font, this.ClientRectangle, this.ForeColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter );
}

Windows.Forms.Control derived class with TabStop and key-events

I've got a control that derives from Forms.Control, it handles mouse events and paint events just fine, but I'm having a problem with key events. I need to handle Left arrow and Right arrow, but so far the Tab control that contains my class eats these.
How do I make this control selectable, focusable?
Here is a nice tutorial to make a focusable control. I just followed it to make sure it works. Also, added a keypress event to the control which works on condition that the control has focus.
http://en.csharp-online.net/Architecture_and_Design_of_Windows_Forms_Custom_Controls%E2%80%94Creating_a_Focusable_Control
Basically all I did was make an instance of my custom control, which inherits from Control. Then added KeyPress, Click, and Paint event. Key press was just a message:
void CustomControl1_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show(string.Format("You pressed {0}", e.KeyChar));
}
Click event just has:
this.Focus();
this.Invalidate();
The paint event I made like this, just so it was visible:
protected override void OnPaint(PaintEventArgs pe)
{
if (ContainsFocus)
pe.Graphics.FillRectangle(Brushes.Azure, this.ClientRectangle);
else
pe.Graphics.FillRectangle(Brushes.Red, this.ClientRectangle);
}
Then, in the main form, after making an instance called mycustomcontrol and adding the event handlers:
mycustomcontrol.Location = new Point(0, 0);
mycustomcontrol.Size = new Size(200, 200);
this.Controls.Add(mycustomcontrol);
The example is much neater than my five minute code, just wanted to be sure that it was possible to solve your problem in this way.
Hope that was helpful.
In order to be selectable, your control has to have the ControlStyles.Selectable style set. You can do this in the constructor by calling SetStyle.
Without seeing your code, I can only tell you that I created a 3 tab container, and created a very simple control that overrode the OnGotFocus:
public partial class CustomControl1 : Control
{
public CustomControl1()
{
InitializeComponent();
}
protected override void OnGotFocus(EventArgs e)
{
this.BackColor = Color.Black;
base.OnGotFocus(e);
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
}
I dropped the control on the form along with a couple of other buttons, set the tab stops appropriately, and the behavior was as expected. Some other default property has been changed in code that is causing the control to not be selectable.

Categories

Resources