CSharp/C# >> PictureBox_KeyDown or PictureBox_KeyPress not firing

by Christian Blackburn » Mon, 08 Sep 2008 05:28:25 GMT

Hi Gang,

Man is C# a heck of a lot harder to write events for than Visual
Basic. In any event I can't get the following code to fire when I
press a key on a picture box:
// Handle the KeyDown event to determine the type of character
entered into the control.
private void picLogo_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{


MessageBox.Show(this, "you pressed a key", "keyboard",
MessageBoxButtons.OK, MessageBoxIcon.Information);

}

private void InitializeComponent()
{
this.picLogo.KeyDown += new
System.Windows.Forms.KeyEventHandler(this.picLogo_KeyDown);

}

I don't get any errors, but it just never fires :(.

Thanks for any and all help,
Christian Blackburn

CSharp/C# >> PictureBox_KeyDown or PictureBox_KeyPress not firing

by Peter Duniho » Mon, 08 Sep 2008 06:18:50 GMT


On Sun, 07 Sep 2008 14:28:25 -0700, Christian Blackburn



Do you have VB.NET code that does the same thing and which works?

Just off the top of my head: are you sure that the PictureBox control has
focus when you are pressing the keys? My recollection is that it's one of
the "non-focusable" controls. You may be able to change that by
sub-classing PictureBox and setting the Selectable control style in the
constructor. But you may find it simpler to handle the key-press in the
container class (e.g. your form).

Keep in mind that you still have some UI issues:

-- Even if you allow PictureBox to accept focus by sub-classing, it
doesn't show focus by default. So either you'd have to add that, or you'd
have lack of good user presentation.

-- Even worse is the prospect of having a control react to key input
without having focus. In some scenarios this might work okay, but any
time you head down that path you should really reconsider whether that's
the right control/UI paradigm for the job.

Pete

CSharp/C# >> PictureBox_KeyDown or PictureBox_KeyPress not firing

by raylopez99 » Mon, 08 Sep 2008 06:32:30 GMT

On Sep 7, 2:28爌m, Christian Blackburn < XXXX@XXXXX.COM >


Besides the stuff Peter D mentioned, which is generally good, keep in
mind the following stuff about keydown events in C#, which took me
half a day to figure out the hard way:

this.KeyPreview = true; that must be set to 憈rue?from Wizard for key
press event KeyUp or KeyDown to work, (in InitializeComponent(); it抯:
this.KeyPreview = true;)


RL

Similar Threads

1. visual c# advancedsearch does not return results or will not fire

2. Activex control events not firing or ActiveX composite control not listening

I'm new to ATL so please bear with me.

I'm using VS2005. I generated a composite ActiveX control to host
ActiveX controls. It will host two or more ActiveX controls. I put the
ActiveX controls on the dialog at design time. I've also put a few
standard buttons.

Here's a clip of the generated class def:
class ATL_NO_VTABLE CTest5Ctrl :
    ....
    public CComCompositeControl<CTest5Ctrl>,
    public IDispEventImpl<IDC_MMHITIMERX1,CTest5Ctrl>,
    public IDispEventImpl<IDC_MMWAVERECORDERX1,CTest5Ctrl>
{...}

I used GetDlgItem() and AtlAxGetControl() to get smart pointers around
the ActiveX controls. I used CAxWindow.Attach() and GetDlgItem() to get
wrappers around the buttons. I did all of this in OnInitDialog -- (side
question: should I be doing this in OnInPlaceActivate?). This, I'm
guessing, doesn't really pertain to my problem, but I put it out there.

I added event handlers for the buttons which then call some methods on
the ActiveX controls. For ex:

LRESULT CTest5Ctrl::OnBnClickedBtnRecord(WORD /*wNotifyCode*/, WORD
/*wID*/, HWND /*hWndCtl*/, BOOL& bHandled)
{
    HRESULT r = m_spWaveRecorder->Recording();  // ActiveX control
    bHandled = TRUE;
    return 0;
}

Calling Recording() should fire a OnStart event. I added an OnStart
handler. Here's the generated sink map entry and my handler.

BEGIN_SINK_MAP(CTest5Ctrl)
    SINK_ENTRY(IDC_MMWAVERECORDERX1, 1, OnStartMmwaverecorderx1)
END_SINK_MAP()

void __stdcall CTest5Ctrl::OnStartMmwaverecorderx1()
{
    // I should get here
}

Now I get the button events. I can step into them and call methods on
the ActiveX controls. I put breakpoints in all of my ActiveX control
event handlers and none of them are entered. I put breakpoints in
BEGIN_SINK_MAP and they are entered upon startup.

I tried Spy++ and I don't see events being fired from the controls but
being new to ATL and Spy++, I don't know if what I'm seeing is normal
or not. I see other events such as mouse movements and clicks, but
nothing unique to the controls.

I have no idea why I'm not getting events from the ActiveX controls.
Any ideas?

Gary F.

3. DocumentComplete Does Not Fire When WebBrowser Is Not Visible

4. Simple composite control fires event fires first time but not second

Hello,

I have a simple composite control (asp.net 1.1). The on click event
fires the first time the button is clicked but not on subsequest
clicks. Any ideas why please?


Imports System.ComponentModel
Imports System.Collections.Specialized
Imports System.Web.UI
Imports System.Web
Imports System.Web.UI.WebControls
Imports Microsoft.ContentManagement.Common
Imports Microsoft.ContentManagement.Publishing
Imports System.Web.UI.WebControls.WebControl
Imports Microsoft.ContentManagement.WebControls.Design
Imports Microsoft.ContentManagement.WebControls

<ToolboxData("<{0}:TopNavMenuComp runat=server></{0}:TopNavMenuComp>")
> _
    Public Class TopNavMenuComp
    Inherits Control
    Implements INamingContainer

    Private _SearchTerm As TextBox
    Private _GoSearch As Button

    Protected Overrides Sub createchildcontrols()

        Controls.Clear()

        _GoSearch = New Button
        _SearchTerm = New TextBox
        _SearchTerm.ID = "searchtermID"
        _GoSearch.Text = "Go Search"
        _GoSearch.ID = "mysearchID"

        With Me.Controls
            .Add(_SearchTerm)
            .Add(_GoSearch)
        End With

        AddHandler _GoSearch.Click, New EventHandler(AddressOf
Me.OnSearchClick)

        ensurechildcontrols()


    End Sub

    Private Sub OnSearchClick(ByVal sender As Object, ByVal e As
EventArgs)

        Dim buttonclicked As String

        buttonclicked = _SearchTerm.Text

    End Sub



There is then a render

  Protected Overrides Sub Render(ByVal output As
System.Web.UI.HtmlTextWriter)

  that includes the lines:

  _SearchTerm.RenderControl(output)
        _GoSearch.RenderControl(output)


Regards,
Paul

5. Server-side events do not fire the first time after client-side validators have fired - Asp.Net

6. LoadViewState not firing / fired in ASP.NET 2.0

7. events do not fire from exe but fire from vb6 ide - VB.Net

8. TextBox_ChangedText event fires but button_click does not fire