Windows/forms in a listbox

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# ?
 
D

Dmytro Lapshyn [MVP]

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.
 
G

GTi

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 ?
 
G

GTi

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 ?
 
M

Morten Wennevik

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 ?
 
G

GTi

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.
 
M

Morten Wennevik

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.
 
G

GTi

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.
 

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