Windows/forms in a listbox

  • Thread starter Thread starter GTi
  • Start date Start date
G

GTi

In Win32 I have a function that opens several Dialogs and put the
window handle in a listbox (together with a string):

HWND hWnd = CreateDialogParam(...)
LB_SETITEMDATA, index, (LPARAM)hWnd);

In this way when users select a different item in the listbox I just hides
the old window handle,
and show the new window with
ShowWindow(LB_GETITEMDATA(...), SW_SHOW);

But what is the best way of doing this in C# ?
 
Hi,

You can store references to Form-derived class instances in the listbox
items and then hide/show the respective forms by using the Show and Hide
methods.
 
Dmytro Lapshyn said:
Hi,

You can store references to Form-derived class instances in the listbox
items and then hide/show the respective forms by using the Show and Hide
methods.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]


GTi said:
In Win32 I have a function that opens several Dialogs and put the
window handle in a listbox (together with a string):

HWND hWnd = CreateDialogParam(...)
LB_SETITEMDATA, index, (LPARAM)hWnd);

In this way when users select a different item in the listbox I just
hides the old window handle,
and show the new window with
ShowWindow(LB_GETITEMDATA(...), SW_SHOW);

But what is the best way of doing this in C# ?

I have tried that in several ways, but failed.
Do U have any sample for me ?
 
Something like this?
(do not work)

this.listBox1.Items.Add(subForm1);

....

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs
e)
{
Form a = (Form)this.listBox1.SelectedItem; // <-- Runtime error: ILLEGAL
CAST
a.Show();
}



Other idea?
(C# newbie)



GTi said:
Dmytro Lapshyn said:
Hi,

You can store references to Form-derived class instances in the listbox
items and then hide/show the respective forms by using the Show and Hide
methods.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]


GTi said:
In Win32 I have a function that opens several Dialogs and put the
window handle in a listbox (together with a string):

HWND hWnd = CreateDialogParam(...)
LB_SETITEMDATA, index, (LPARAM)hWnd);

In this way when users select a different item in the listbox I just
hides the old window handle,
and show the new window with
ShowWindow(LB_GETITEMDATA(...), SW_SHOW);

But what is the best way of doing this in C# ?

I have tried that in several ways, but failed.
Do U have any sample for me ?
 
Your code should work provided that the SelectedItem is indeed a Form reference.
Check your code to make sure you don't chance the reference or add something other than a Form reference.

PS! The listbox will show System.Windows.Forms.Form, Text unless you set the DisplayMember before adding the references.


protected override void OnLoad(EventArgs e)
{
listBox1.DisplayMember = "Text";
Form f = new Form();
f.Text = "Dialog 1";
listBox1.Items.Add(f);
f = new Form();
f.Text = "Dialog 2";
listBox1.Items.Add(f);
}

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
Form f = (Form)listBox1.SelectedItem;
f.Show();
}


Something like this?
(do not work)

this.listBox1.Items.Add(subForm1);

...

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs
e)
{
Form a = (Form)this.listBox1.SelectedItem; // <-- Runtime error: ILLEGAL
CAST
a.Show();
}



Other idea?
(C# newbie)



GTi said:
Dmytro Lapshyn said:
Hi,

You can store references to Form-derived class instances in the listbox
items and then hide/show the respective forms by using the Show and Hide
methods.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]


In Win32 I have a function that opens several Dialogs and put the
window handle in a listbox (together with a string):

HWND hWnd = CreateDialogParam(...)
LB_SETITEMDATA, index, (LPARAM)hWnd);

In this way when users select a different item in the listbox I just
hides the old window handle,
and show the new window with
ShowWindow(LB_GETITEMDATA(...), SW_SHOW);

But what is the best way of doing this in C# ?

I have tried that in several ways, but failed.
Do U have any sample for me ?
 
Morten Wennevik said:
Your code should work provided that the SelectedItem is indeed a Form
reference.
Check your code to make sure you don't chance the reference or add
something other than a Form reference.

PS! The listbox will show System.Windows.Forms.Form, Text unless you set
the DisplayMember before adding the references.


protected override void OnLoad(EventArgs e)
{
listBox1.DisplayMember = "Text";
Form f = new Form();
f.Text = "Dialog 1";
listBox1.Items.Add(f);
f = new Form();
f.Text = "Dialog 2";
listBox1.Items.Add(f);
}

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs
e)
{
Form f = (Form)listBox1.SelectedItem;
f.Show();
}


Something like this?
(do not work)

this.listBox1.Items.Add(subForm1);

...

private void listBox1_SelectedIndexChanged(object sender,
System.EventArgs
e)
{
Form a = (Form)this.listBox1.SelectedItem; // <-- Runtime error: ILLEGAL
CAST
a.Show();
}



Other idea?
(C# newbie)



GTi said:
message
Hi,

You can store references to Form-derived class instances in the listbox
items and then hide/show the respective forms by using the Show and
Hide
methods.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]


In Win32 I have a function that opens several Dialogs and put the
window handle in a listbox (together with a string):

HWND hWnd = CreateDialogParam(...)
LB_SETITEMDATA, index, (LPARAM)hWnd);

In this way when users select a different item in the listbox I just
hides the old window handle,
and show the new window with
ShowWindow(LB_GETITEMDATA(...), SW_SHOW);

But what is the best way of doing this in C# ?


I have tried that in several ways, but failed.
Do U have any sample for me ?


Nice - it was almost the same ting I have done (except a small bug....)

But the behave of this code stumble me.

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs
e)
{
Form f = (Form)listBox1.SelectedItem;
if(oldMenuForm!=null) oldMenuForm.Hide();
oldMenuForm=f;
if(oldMenuForm!=null)
{
oldMenuForm.Size = panel1.Size;
oldMenuForm.Location = panel1.Location;
oldMenuForm.Show();
}
}

When the form is displayed the location is all wrong ("random location"),
however if I change it to:
oldMenuForm.Show();
oldMenuForm.Size = panel1.Size;
oldMenuForm.Location = panel1.Location;

Then it works fine, BUT then the window flicker a little - annoying.
 
Try setting Form.StartPosition = FormStartPosition.Manual or something other than the WindowsDefault ones.


Morten Wennevik said:
Your code should work provided that the SelectedItem is indeed a Form
reference.
Check your code to make sure you don't chance the reference or add
something other than a Form reference.

PS! The listbox will show System.Windows.Forms.Form, Text unless you set
the DisplayMember before adding the references.


protected override void OnLoad(EventArgs e)
{
listBox1.DisplayMember = "Text";
Form f = new Form();
f.Text = "Dialog 1";
listBox1.Items.Add(f);
f = new Form();
f.Text = "Dialog 2";
listBox1.Items.Add(f);
}

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs
e)
{
Form f = (Form)listBox1.SelectedItem;
f.Show();
}


Something like this?
(do not work)

this.listBox1.Items.Add(subForm1);

...

private void listBox1_SelectedIndexChanged(object sender,
System.EventArgs
e)
{
Form a = (Form)this.listBox1.SelectedItem; // <-- Runtime error: ILLEGAL
CAST
a.Show();
}



Other idea?
(C# newbie)



message
Hi,

You can store references to Form-derived class instances in the listbox
items and then hide/show the respective forms by using the Show and
Hide
methods.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]


In Win32 I have a function that opens several Dialogs and put the
window handle in a listbox (together with a string):

HWND hWnd = CreateDialogParam(...)
LB_SETITEMDATA, index, (LPARAM)hWnd);

In this way when users select a different item in the listbox I just
hides the old window handle,
and show the new window with
ShowWindow(LB_GETITEMDATA(...), SW_SHOW);

But what is the best way of doing this in C# ?


I have tried that in several ways, but failed.
Do U have any sample for me ?


Nice - it was almost the same ting I have done (except a small bug....)

But the behave of this code stumble me.

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs
e)
{
Form f = (Form)listBox1.SelectedItem;
if(oldMenuForm!=null) oldMenuForm.Hide();
oldMenuForm=f;
if(oldMenuForm!=null)
{
oldMenuForm.Size = panel1.Size;
oldMenuForm.Location = panel1.Location;
oldMenuForm.Show();
}
}

When the form is displayed the location is all wrong ("random location"),
however if I change it to:
oldMenuForm.Show();
oldMenuForm.Size = panel1.Size;
oldMenuForm.Location = panel1.Location;

Then it works fine, BUT then the window flicker a little - annoying.
 
Morten,
Outstanding m8 !!!!!!!!!!!!!!!!!!!



Morten Wennevik said:
Try setting Form.StartPosition = FormStartPosition.Manual or something
other than the WindowsDefault ones.


Morten Wennevik said:
Your code should work provided that the SelectedItem is indeed a Form
reference.
Check your code to make sure you don't chance the reference or add
something other than a Form reference.

PS! The listbox will show System.Windows.Forms.Form, Text unless you set
the DisplayMember before adding the references.


protected override void OnLoad(EventArgs e)
{
listBox1.DisplayMember = "Text";
Form f = new Form();
f.Text = "Dialog 1";
listBox1.Items.Add(f);
f = new Form();
f.Text = "Dialog 2";
listBox1.Items.Add(f);
}

private void listBox1_SelectedIndexChanged(object sender,
System.EventArgs
e)
{
Form f = (Form)listBox1.SelectedItem;
f.Show();
}



Something like this?
(do not work)

this.listBox1.Items.Add(subForm1);

...

private void listBox1_SelectedIndexChanged(object sender,
System.EventArgs
e)
{
Form a = (Form)this.listBox1.SelectedItem; // <-- Runtime error:
ILLEGAL
CAST
a.Show();
}



Other idea?
(C# newbie)



message
Hi,

You can store references to Form-derived class instances in the
listbox
items and then hide/show the respective forms by using the Show and
Hide
methods.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]


In Win32 I have a function that opens several Dialogs and put the
window handle in a listbox (together with a string):

HWND hWnd = CreateDialogParam(...)
LB_SETITEMDATA, index, (LPARAM)hWnd);

In this way when users select a different item in the listbox I just
hides the old window handle,
and show the new window with
ShowWindow(LB_GETITEMDATA(...), SW_SHOW);

But what is the best way of doing this in C# ?


I have tried that in several ways, but failed.
Do U have any sample for me ?


Nice - it was almost the same ting I have done (except a small bug....)

But the behave of this code stumble me.

private void listBox1_SelectedIndexChanged(object sender,
System.EventArgs
e)
{
Form f = (Form)listBox1.SelectedItem;
if(oldMenuForm!=null) oldMenuForm.Hide();
oldMenuForm=f;
if(oldMenuForm!=null)
{
oldMenuForm.Size = panel1.Size;
oldMenuForm.Location = panel1.Location;
oldMenuForm.Show();
}
}

When the form is displayed the location is all wrong ("random location"),
however if I change it to:
oldMenuForm.Show();
oldMenuForm.Size = panel1.Size;
oldMenuForm.Location = panel1.Location;

Then it works fine, BUT then the window flicker a little - annoying.
 
Back
Top