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

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.

Related

I have a user control on my Form1 but when i add a mouse event of the control it dosen't do anything why?

private void lineGraph1_MouseEnter(object sender, EventArgs e)
{
MessageBox.Show("test");
}
I want that when I move the mouse over/on the control area it will show the message.
I downloaded the control dll from here :
http://www.codeproject.com/Articles/274318/Line-Graph-Component-in-Csharp
Downloaded the demo and used the dll file there.
So I have now the control on Form1 and then I did on the control in the Form1 designer right click mouse properties events and double click on Mouse Enter but nothing when im moving the mouse over the control nothing happen.
What did I do wrong ?
Adding the method does nothing by itself. You have to add it as an event handler, such as with
lineGraph1.MouseEnter += new EventHandler(lineGraph1_MouseEnter);
The Form Editor does this for you when you select the MouseEnter event from the events dropdown at top right.
I think it's due to the fact LineGraph has some controls over itself.
For example, if you put your onmouseenter event on the PlotArea control directly it will probably work fine.
public PlotArea()
{
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.MouseEnter += new EventHandler(PlotArea_MouseEnter);
}
void PlotArea_MouseEnter(object sender, EventArgs e)
{
MessageBox.Show("test");
}

KeyDown event not firing?

When coding a small game, I encountered a problem; my form's KeyDown and KeyUp events don't fire at all.
This is the form's code:
public class GameForm : Form
{
private ControllableBlock player;
public GameForm()
{
KeyDown += Game_KeyDown;
KeyUp += Game_KeyUp;
player = new ControllableBlock();
Controls.Add(player);
}
private void Game_KeyDown(object sender, KeyEventArgs e)
{
player.ReactToKey(e.KeyCode);
}
private void Game_KeyUp(object sender, KeyEventArgs e)
{
player.ReactToKey(e.KeyCode);
}
}
There's a lot more going on, but I only pasted the relevant code.
I've already tried setting this.KeyPreview = true; and calling this.Focus();, neither works.
The problem is not in ReactToKey() method, I've already set a breakpoint there and the event is never fired.
Edit: After some tests I've come to a conclusion that the problem is within my ControllableBlock.
Yet, I have no idea why, but I'm working on it.
If I comment out everything that's related to the player, the events start firing.
Edit 2: Seems like the problem is me inheriting my ControllableBlock from Control.
If I inherit it from Panel, it works fine.
Why is this?
Can't I fire an event if I inherit from control?
The ControllableBlock class is empty for now, so it doesn't even do anything other than inherits from Control.
Edit 3: Now that I've started a bounty, I'd like to clarify that I'm not looking for a solution on how to make the events fire, I'm looking for a reason on why they don't fire if I inherit from Control.
If your events should be application-wide try to set property KeyPreview to true - it will allow you to fire respective events regardless of focused control.
this.KeyPreview = true;
Otherwise you should attach these events directly to control that will process them.
Edit:
I removed InitializeComponent(); from my form and got behaviour identical to yours.
After implementing solution provided in this question all events started to qork perfectly.
Copy code snippet here:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == Keys.Left) {
// Do your staff for Left Key here
return true;
}
// you need to add if condition to every direction you want to handle
return base.ProcessCmdKey(ref msg, keyData);
}
I was able to reproduce a similar issue (which is hopefully related..)
Explanation:
Controls which return CanSelect==true are selectable for keyboard input
A blank descendent of Control() is selectable, one of Panel() is not
The first selectable control added to a form will get selected
A selected control will steal keyboard events from its parents by default
Certain keys used for navigation within a window require extra steps to be handleable
Check here for a good overview of how windows keyboard input works.
Code to reproduce it:
public class GameForm : Form
{
public GameForm()
{
this.KeyDown += Game_KeyDown;
var tests = new List<Control[]>() {
new[] { new Panel() },
new[] { new Panel(), new Panel() },
new[] { new Control() },
new[] { new Control(), new Panel() },
new[] { new Panel(), new Control() }
};
// When test index 2 to 4 used, keyboard input does not reach form level
Controls.AddRange(tests[0]);
// When uncommented, ensures all keyboard input reaches form level
/*this.KeyPreview = true;
// Additional plumbing required along with KeyPreview to allow arrow and other reserved keys
foreach (Control control in this.Controls)
{
control.PreviewKeyDown += control_PreviewKeyDown;
}*/
}
void control_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
e.IsInputKey = true;
}
private void Game_KeyDown(object sender, KeyEventArgs e)
{
// breakpoint here
Debug.WriteLine(e.KeyCode);
}
}
You need to make your control selectable before it can receive the focus.
Try adding the following to your constructor:
this.SetStyle(ControlStyles.Selectable, true);
And ensure that you give your form focus after it has been displayed. Or, override OnMouseDown() and call this.Focus() in it.
try moving the handler setup to the Form_Load event rather than the constructor. Should there not be a call to Initialize() in the constructor? I wouldn't particularly recommend removing it
If ControllableBlock inherits from Panel, it will have more event hookups and better UI interaction setup than a base Control object.

WinForm - Don't allow radio button to be tabbed into

Since TabStop does not work on RadioButtons (see linked question), how can I prevent a (WinForm) RadioButton from being tabbed into, but also allow the user to click on the RadioButton, without the tab focus jumping somewhere else.
I've read this and so I thought the following would work:
rbFMV.Enter += (s, e) => focusFirstWorkflowButton();
rbFMV.MouseUp += (s, e) => rbFMV.Focus();
But it doesn't. When I click on the RB, the focus jumps away, and does not come back on Mouse Up.
Any dirty workarounds out there?
Try something like this:
Set TabStop property of the radiobuttons to "false" in the form's constructor. Then attach the following events handlers to the CheckedChanged events of the radiobuttons.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
radioButton1.TabStop = false;
radioButton2.TabStop = false;
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
radioButton1.TabStop = false;
radioButton2.TabStop = false;
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
radioButton1.TabStop = false;
radioButton2.TabStop = false;
}
}
You can attach these event handlers using lambda aswell, as you have shown in your question.
But the important point here is that whenever a radiobutton is checked/unchecked, it's tabstop property is also modified simultaneously. Hence you need to set it to false everytime that event occurs.
The underlying Win32 RadioButton does not automatically change the TabStop property. However, if you use .NET Reflector you can see that the .NET control runs code to update the TabStop property whenever OnEnter method is called because focus has entered the control or whenever the AutoCheck or Checked properties are modified.
Luckily there is a simple solution to your problem. Just derive a new class that overrides the OnTabStopChanged method and automatically set it back to false again. Here is the impl...
public class NonTabStopRadioButton : RadioButton
{
protected override void OnTabStopChanged(EventArgs e)
{
base.OnTabStopChanged(e);
if (TabStop)
TabStop = false;
}
}
Then always use the NonTabStopRadioButton in your application instead of the standard one.
only one control can have input focus at the time i think, so when they click the radio button it will get focus..
But what if you do something like this?
rbFMV.GotFocus += (s, e) => someothercontrol.Focus();
also, have you looked at the TabStop property?
-edit-
i see you have, sorry, missed that :/

Re-Inventing The Label Control

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);
}
}

Transparent Checkbox in Custom Control Using C#

I have created a custom control that gets highlighted when the mouse hovers over it. The custom control also has a checkbox. When the mouse goes over the checkbox, the highlighting of the custom control does not occur. I've tried using WS_EX_TRANSPARENT on the checkbox but it isn't working for me.
int cbStyle = GetWindowLong(CompletedCheckBox.Handle, GWL_EXSTYLE);
SetWindowLong(CompletedCheckBox.Handle, GWL_EXSTYLE, cbStyle | WS_EX_TRANSPARENT);
How can I do this?
Thanks
Transparent only affects drawing, not mouse events. The check box is getting the mouse events, this in turn means that when you mouse over the checkbox, your control receives a MouseLeave event. To ensure that the background color changes, even when a child control ( at any level) gets a MouseEnter event, you need to track that a control of interest -- or any child, grand-child ..etc-- has the mouse over it. To do this, recurse through all descendant controls and intercept the appropriate events for them. To do this, try something similar to the class below.
public partial class MyControl : UserControl
{
public MyControl()
{
InitializeComponent();
AttachMouseEnterToChildControls(this);
}
void AttachMouseEnterToChildControls(Control con)
{
foreach (Control c in con.Controls)
{
c.MouseEnter += new EventHandler(control_MouseEnter);
c.MouseLeave += new EventHandler(control_MouseLeave);
AttachMouseEnterToChildControls(c);
}
}
private void control_MouseEnter(object sender, EventArgs e)
{
this.BackColor = Color.AliceBlue;
}
private void control_MouseLeave(object sender, EventArgs e)
{
this.BackColor = SystemColors.Control;
}
}

Categories

Resources