OnClick not working in listbox

  • Thread starter Thread starter Ken Beauchesne
  • Start date Start date
K

Ken Beauchesne

I have a problem trying to get the OnClick to fire in a listbox,
no matter what method I try to get access to it there is no response.
The doc's say it's supported has anyone had success or can see
what i'm doing wrong?


thanks
Ken

private DisplayListBox.DisplayList DisplayList = new
DisplayListBox.DisplayList();

this.Controls.Add(this.DisplayList);


--------------------------------
list gets filled with items, another event pops up the listbox
and everything is there and when I click on an item the OnClick
never gets called. I use this exact code else where for a non listbox
control and it works perfect.
---------------------------------

namespace DisplayListBox
{
public class DisplayList :System.Windows.Forms.ListBox
{
public delegate void DoubleClickEventHandler( EventArgs e);
public event DoubleClickEventHandler OnDoubleClick;
private int previousClick = SystemInformation.DoubleClickTime + 1;

public DisplayList()
{
this.OnDoubleClick +=new
DoubleClickEventHandler(DisplayList_OnDoubleClick);
this.Location = new Point(200,80);
this.Enabled = true;
this.Visible = false;
}


~DisplayList()
{
}

private void DisplayList_OnDoubleClick(EventArgs e)
{
//
int DisplayIndex;

DisplayIndex = 1; // just for debug never gets here
}



protected override void OnClick(EventArgs e)
{
this.Focus();
int now = System.Environment.TickCount;
if(now - previousClick <= SystemInformation.DoubleClickTime)
{
this.OnDoubleClick(e);
}

previousClick = now;

}
 
Ken said:
I have a problem trying to get the OnClick to fire in a listbox,

Take a look at docs for System.Windows.Forms.ListBox events [1]. There is no Click event. That would be your problem.

ListBox does have a DoubleClick event, and that's why you were able to register a delegate for the DoubleClick event, but you don't have a similar line of code to register a handler for the Click event.

Cheers,
Stuart Celarier, Fern Creek

[1] http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwindowsforms.asp
 
the OnClick (inherited from Control) is supported in .NET CF
the double click is not in CF that was what my control was attempting
to implement. Check out listbox members

Ken

Stuart Celarier said:
Ken said:
I have a problem trying to get the OnClick to fire in a listbox,

Take a look at docs for System.Windows.Forms.ListBox events [1]. There is
no Click event. That would be your problem.
ListBox does have a DoubleClick event, and that's why you were able to
register a delegate for the DoubleClick event, but you don't have a similar
line of code to register a handler for the Click event.
 
Ken said:
the OnClick (inherited from Control) is supported in .NET CF
the double click is not in CF that was what my control was attempting
to implement.

Sorry, I misunderstood your question. The DoubleClick in your sample is what threw me.

Yes, I have confirmed the problem. Below is a minimal sample that illustrates the symptom. You can compile it under both .NET Framework and .NET Compact Framework and confirm that for the latter OnClick is never called.

I will try to investigate further.

Cheers,
Stuart Celarier, Fern Creek

<SampleCode>

// On the .NET Framework a MessageBox is displayed
// when the ListBox is clicked.
//
// On the .NET Compact Framework a MessageBox is NOT displayed
// when the ListBox is clicked.

using System;
using System.Windows.Forms;

namespace OnClickNotCalled
{
public class CustomListBox : ListBox
{
protected override void OnClick(EventArgs e)
{
MessageBox.Show( "Click" );
base.OnClick (e);
}
}

public class Form1 : System.Windows.Forms.Form
{
private CustomListBox listBox1;

public Form1()
{
this.listBox1 = new CustomListBox();
this.listBox1.Location = new System.Drawing.Point(20, 20);
this.listBox1.Size = new System.Drawing.Size(100, 100);

this.Controls.Add(this.listBox1);
this.Text = "Form1";
}

static void Main() { Application.Run(new Form1()); }
}
}

</SampleCode>
 
Here's a little more information: the .NET Compact Framework's Class Library Comparison Tool [1] shows System.Windows.Forms.ListBox.OnClick is only supported on the .NET Framework. That contradicts the .NET Framework Class Library docs for System.Windows.Forms.ListBox members [2]. So maybe this is a documentation error?

Cheers,
Stuart Celarier, Fern Creek

[1] http://msdn.microsoft.com/library/en-us/dv_evtuv/html/etconnetcompactframeworkclasses.asp
[2] http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwindowsformslistboxmemberstopic.asp
 
Hello Stuart,

Yes, you are right. The Click event is not supported for ListBox in the
.NET Compact Framework.

Thank you,
Sergiy.

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
| From: Stuart Celarier <[email protected]>
| Subject: Re: OnClick not working in listbox
| References: <#[email protected]>
| Content-Type: text/plain; charset=iso-8859-1
| X-Newsreader: JetBrains Omea Reader 341.19
| Message-ID: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| Date: Thu, 02 Dec 2004 14:01:31 -0800
| NNTP-Posting-Host: c-67-169-206-96.client.comcast.net 67.169.206.96
| Lines: 1
| Path:
cpmsftngxa10.phx.gbl!TK2MSFTNGXA03.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14
.phx.gbl
| Xref: cpmsftngxa10.phx.gbl
microsoft.public.dotnet.framework.compactframework:66248
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| Here's a little more information: the .NET Compact Framework's Class
Library Comparison Tool [1] shows System.Windows.Forms.ListBox.OnClick is
only supported on the .NET Framework. That contradicts the .NET Framework
Class Library docs for System.Windows.Forms.ListBox members [2]. So maybe
this is a documentation error?
|
| Cheers,
| Stuart Celarier, Fern Creek
|
| [1]
http://msdn.microsoft.com/library/en-us/dv_evtuv/html/etconnetcompactframewo
rkclasses.asp
| [2]
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwindowsformsli
stboxmemberstopic.asp
|
 
Back
Top