simple keyDown testcase no worky

B

Brannon

In the following code I have a Control in a UserControl in a Form. The
Control always gets the keypresses, even after I call Select on the
UserControl. What's the right way to do that? I actually want the
UserControl to get all keypresses and make the Control just pass its
presses to its parent. Thanks for your time.

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

namespace TestKeyPresses
{
public class Form1 : Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form 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.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Text = "Form1";
}

#endregion

public class ButtonClass : Control
{
public ButtonClass(Control parent)
: base(parent, "")
{
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
BackColor = Color.Transparent; // we'll rely on them setting the
button region to avoid flicker
DoubleBuffered = true; // yeah, this doesn't work
Dock = DockStyle.None;
MinimumSize = MaximumSize = Size = new Size(40, 40);
}
protected override void OnKeyDown(KeyEventArgs e)
{
MessageBox.Show("you'll see me");
base.OnKeyDown(e);
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.FillEllipse(Brushes.Black, ClientRectangle);
base.OnPaint(e);
}
}

public class ContainerClass : UserControl
{
public ContainerClass()
: base()
{
SetStyle(ControlStyles.Selectable, true);
UpdateStyles();
BackColor = Color.Red;
ButtonClass bc = new ButtonClass(this);
bc.Location = new Point(40, 40);
Size = new Size(100, 100);
Location = new Point(20, 20);
}
protected override void OnKeyDown(KeyEventArgs e)
{
MessageBox.Show("you won't see me. ever.");
base.OnKeyDown(e);
}
protected override void OnMouseEnter(EventArgs e)
{
if(CanSelect)
Select();
base.OnMouseEnter(e);
}
}


public Form1()
{
InitializeComponent();
Controls.Add(new ContainerClass());
KeyPreview = true;
}

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
 
B

Brannon

I was able to make it work by making the Control not selectable. I
still think there should be a way to support both key handlers, though.
 

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

Top