Child Form Gets Parent Form's Click

G

Guest

I have an issue where click events on a parent form are being passed along to
a child form. The parent form has a ListView that invokes the child form when
a list item is clicked. The ListView lies on about the same bit of real
estate on the parent form as an OpenNETCF LinkLabel does on the child form.
Upon clicking a listitem, the child form is initialized and has a linklabel
click event already queued up and waiting for it as if the event had been
passed along to the child form. In fact I've placed a 5 second sleep between
the ListView click on the parent form and creation of the child form just to
guarantee that any pressure latency from the screen tap has dissipated and is
not the cause of the child form's click event. I originally assumed that this
was an OpenNETCF problem so I programmed a ClickLabel ala "Microsoft .NET
Compact Framework" core reference page 595. It experiences precisely the
same behavior.

1) If this is a bug is there a workaround?
2) If this is not a bug is there a workaround?

Below is example code that will demo the issue. The ParentForm has a
ListView with 3 list items. The ChildForm has a LinkLabel that falls under
ParentForm's ListView item 1. The LinkLabel contains text that is displayed
in a MessageBox when the LinkLabel is clicked. Compile the code, then run it
on either the 2002 or 2003 emulator, or on your PPC. Click ListView item 1.
After a 5 second delay, the child form will appear and the LinkLabel's
MessageBox will already be deployed.


Bill


/********************************************/
/**************** Parent Form **************/
/******************************************/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;

namespace ClickIssue
{
/// <summary>
/// Summary description for ParentForm.
/// </summary>
public class ParentForm : System.Windows.Forms.Form
{
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader columnHeader1;

public ParentForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

this.MinimizeBox = false;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}

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

#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()
{
System.Windows.Forms.ListViewItem listViewItem1 = new
System.Windows.Forms.ListViewItem();
System.Windows.Forms.ListViewItem listViewItem2 = new
System.Windows.Forms.ListViewItem();
System.Windows.Forms.ListViewItem listViewItem3 = new
System.Windows.Forms.ListViewItem();
this.listView1 = new System.Windows.Forms.ListView();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
//
// listView1
//
this.listView1.Columns.Add(this.columnHeader1);
this.listView1.FullRowSelect = true;
this.listView1.HeaderStyle =
System.Windows.Forms.ColumnHeaderStyle.None;
listViewItem1.Text = "ListView item 1";
listViewItem2.Text = "ListView item 2";
listViewItem3.Text = "ListView item 3";
this.listView1.Items.Add(listViewItem1);
this.listView1.Items.Add(listViewItem2);
this.listView1.Items.Add(listViewItem3);
this.listView1.Size = new System.Drawing.Size(240, 272);
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.SelectedIndexChanged += new
System.EventHandler(this.listView1_SelectedIndexChanged);
//
// columnHeader1
//
this.columnHeader1.Text = "Listview Column";
this.columnHeader1.Width = 240;
//
// ParentForm
//
this.Controls.Add(this.listView1);
this.Text = "ParentForm";

}
#endregion

private void listView1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
Thread.Sleep( 5000 );
Form fChildForm = new ChildForm();
fChildForm.ShowDialog();
}
}
}

/********************************************/
/**************** Child Form **************/
/******************************************/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace ClickIssue
{
/// <summary>
/// Summary description for ChildForm.
/// </summary>
public class ChildForm : System.Windows.Forms.Form
{
private OpenNETCF.Windows.Forms.LinkLabel linkLabel1;

public ChildForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
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.linkLabel1 = new OpenNETCF.Windows.Forms.LinkLabel();
//
// linkLabel1
//
this.linkLabel1.LinkData = "This is help for linklabel1.";
this.linkLabel1.Size = new System.Drawing.Size(176, 20);
this.linkLabel1.Text = "Click here for linklabel help.";
this.linkLabel1.LinkClicked += new
OpenNETCF.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
//
// ChildForm
//
this.Controls.Add(this.linkLabel1);
this.Text = "ChildForm";

}
#endregion

private void linkLabel1_LinkClicked(object sender,
OpenNETCF.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
string temp = (string)e.LinkData;
MessageBox.Show( temp );
}
}
}
 
D

Daniel Moth

I haven't run your code, but you do realise ListView.SelectedIndedxChanged
is typically run twice, right? Once with a selection and once without. Maybe
that is a red herring but I would cater for that in the design (i.e. show
the form after the *second* and final selIndChanged event).

Furthermore, ItemActivate is a better listview event to use for opening
other forms (if you use it look at the Activation property as well).

Cheers
Daniel
 
G

Guest

Thanks, item activate works just fine!
Bill

Daniel Moth said:
I haven't run your code, but you do realise ListView.SelectedIndedxChanged
is typically run twice, right? Once with a selection and once without. Maybe
that is a red herring but I would cater for that in the design (i.e. show
the form after the *second* and final selIndChanged event).

Furthermore, ItemActivate is a better listview event to use for opening
other forms (if you use it look at the Activation property as well).

Cheers
Daniel
 

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