Kristian
Kristian

Reputation: 3461

Open .net windows form after event finishes

I'm having a Windows Mobile CE application written in C# with .Net CF.

Consider i have 2 forms in my application:

  1. List of objects (has a listview)
  2. Details page (should appear when something is selected in previous listview)

Currently i'm attaching callback to listview's SelectedIndexChanged event, and open a new form there.

New form opens okay (in the midde of the event callback), but when i close the form(this.Close()), then the list page isn't clickable first time, after the first click UI is interactable again.

Also the the ListViewItem clicked at first step doesn't get selected(blue background).

Here's a short (12s) video showing this problem: http://take.ms/urkme As you see from the video, after coming back from details screen, refresh button doesn't click on the first click..

I'm showing the details form like so:

private void listView_SelectedIndexChanged(object sender, EventArgs e)
{
    (new FormDetails()).ShowDialog();
}

Is there any way to show the details form after event finishes, or am i doing it completely wrong?

PS! Tried the same with a Button and it's click event, then all worked nicely..

Upvotes: 0

Views: 259

Answers (3)

josef
josef

Reputation: 5959

When you use ShowDialog() you open a form in modal mode. All further processing of following code will not take place until the modal form is closed or returns a DialogResult. Better use a modeless form using .Show().

private void listView_SelectedIndexChanged(object sender, EventArgs e)
{
    (new FormDetails()).Show(); //will not stop processing of events in mainForm
}

If the new FormDetails is finished, it can use a simple Hide or Close to bring up the main form to foreground. Remember that the main form is still there and will not wait for the FormDetails being closed (as it is a modeless dialog).

Upvotes: 0

Nam Bình
Nam Bình

Reputation: 422

As I know ListView's SelectedIndexChanged event fired twice on almost case not like Button's Click event which fired once, this maybe what cause that weird interaction. Maybe try changing to ItemSelectionChanged event as suggested in here.

Upvotes: 1

Oceans
Oceans

Reputation: 3509

Your problem is caused by using the SelectedIndexChanged event. When you select an item in your list you'll set the SelectedIndex, if you select the same item again the index won't be changed so you'll never call the event.

You could use the Click event to trigger the wanted response.

Here is an example:

private void listView1_Click(object sender, EventArgs e)
{
    var firstSelectedItem = listView1.SelectedItems[0]; //This is your selected item.
    //do stuff
}

Upvotes: 0

Related Questions