 |
| C# 2005 For discussion of Visual C# 2005. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

March 20th, 2008, 04:32 AM
|
|
Authorized User
|
|
Join Date: Jul 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Add Items to a listbox on a modalresult form
Hi there
I have a problem. I want to select a font using a self created form witch loads on a button Click. The components on the form are created but the listbox items are not created. The easy solution is to use a font dialog but this form has to manny options. I only need te font name.
Here is my code:
private void button3_Click(object sender, EventArgs e)
{
// Create a new instance of the form.
Form form1 = new Form();
// Create The listbox.
ListBox listbox1 = new ListBox();
// Create two buttons to use as the accept and cancel buttons.
Button button1 = new Button ();
Button button2 = new Button ();
// Set the position of the Listbox on the form.
listbox1.Location = new Point(8, 8);
// Set the text of button1 to "OK".
button1.Text = "OK";
// Set the position of the button on the form.
button1.Location = new Point (201, 8);
// Set the text of button2 to "Cancel".
button2.Text = "Annuleren";
// Set the position of the button based on the location of button1.
button2.Location = new Point (button1.Left, button1.Height + button1.Top + 10);
// Make button1's dialog result OK.
button1.DialogResult = DialogResult.OK;
// Make button2's dialog result Cancel.
button2.DialogResult = DialogResult.Cancel;
// Set the caption bar text of the form.
form1.Text = "BTM - Textstyle";
// Define the border style of the form to a dialog box.
form1.FormBorderStyle = FormBorderStyle.FixedDialog;
// Set the accept button of the form to button1.
form1.AcceptButton = button1;
// Set the cancel button of the form to button2.
form1.CancelButton = button2;
// Set the start position of the form to the center of the screen.
form1.StartPosition = FormStartPosition.CenterParent;
// Add listbox1 to the form.
form1.Controls.Add(listbox1);
// Add button1 to the form.
form1.Controls.Add(button1);
// Add button2 to the form.
form1.Controls.Add(button2);
// Display the form as a modal dialog box.
form1.ShowDialog();
// Add fontitems to the listbox
FontFamily[] fontFam = System.Drawing.FontFamily.Families;
foreach (FontFamily ff in fontFam)
listbox1.Items.Add(ff.Name);
// Determine if the OK button was clicked on the dialog box.
if (form1.DialogResult == DialogResult.OK)
{
textBox1.Text = listbox1.SelectedItem.ToString();
form1.Dispose();
}
else
{
form1.Dispose();
}
}
Can any one help me to populate the listbox with the fonts.
Greetz Macdevv
|
|

March 20th, 2008, 05:55 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Hi MacDevv,
From the sounds of it, you want to keep the list contents out of the actual custom form yes? For re-usability?
If this is the case, then I would say: - Create an Abstract Class, say FormListItems.
- In this Abstract class, have a a method, say GetListItems()
- Create a Subclass of the Abstract Class called, say FontListItems
- In the implementation of GetListItems in the FontListItems class, implement the logic to create list of the Font names.
- Update your Forms constructor to receive a FormListItems object (e.g. "public Form(FormListItems itemsToList)")
- In the same constructor, call the GetListItems to get a list, then just enumerate the list, adding each to the ListBox.Items collection..
Does that help?
Rob
The Developing Developer
Currently Working Towards: MCAD C#
My Blog: http://www.robzyc.spaces.live.com
<center> "Nothing can stop the man with the right mental attitude from achieving his goal;
nothing on earth can help the man with the wrong mental attitude".
Thomas Jefferson</center>
|
|

March 20th, 2008, 06:07 AM
|
|
Authorized User
|
|
Join Date: Jul 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hy rob
Thx for your quick reply. I think your solution is the right one.
But i have no idea how to implement that. Im a newby.
I understant that i have to create an new class. But can you help me how to create the code???
|
|

March 20th, 2008, 07:35 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Hi MacDevv,
No problem, we all start somewhere.
Please review this link on abstract classes and give me a shout if you get stuck. Not trying to be difficult, just trying to help you learn :)
http://www.codeproject.com/KB/cs/jmabstractclasses.aspx
Abstract classes are basically "concept classes". Its like saying "I know ' this' is ' one of these' - and I know ' one of these' can do ' this"
So what we are setting up in your application is a "carrier" object to pass information to your form. You form knows it needs to display a list in the ListBox. But it doesnt know (or want to know!) how to get the items to place in the list. So what we do here, is delegate it to a special type of class.
Now, we could just create a FontListGenerator class, and make it work with installed fonts. However, if we did that, we could only ever pass it a FontListGenerator, locking us in to pretty much the same situation.
So, what we do, we create an abstract class which says, "im something that can give you a list!" and then create subclasses that inherit from the abstract class. So anything that is a "ListGenerator" can then be passed, and the form can just say "I know you can produce lists, give me it".
Note: I know many of you are probably thinking "why not just allow the form to receive anything that implements IList or something, the reason I did not recommend this is because abstract classes can be extended to provide additional implementation, interfaces cannot!
Hope this helps, if any of it is unclear, then please ask!
Rob
The Developing Developer
Currently Working Towards: MCAD C#
My Blog: http://www.robzyc.spaces.live.com
<center> "Nothing can stop the man with the right mental attitude from achieving his goal;
nothing on earth can help the man with the wrong mental attitude".
Thomas Jefferson</center>
|
|

March 20th, 2008, 08:07 AM
|
|
Authorized User
|
|
Join Date: Jul 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I feel realy stupid ad this moment.
This is where im stuc:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace Bouwkundig_Teken_Menu
{
public abstract class FromListItems
{
public void GetListItems()
{
FontFamily[] fontFam = System.Drawing.FontFamily.Families;
}
public void FontlistItems()
{
}
}
}
Plz Help
|
|

March 20th, 2008, 09:09 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Don't feel stupid, your trying to get your head round something that I have even seen "seasoned" developers screw up :)
Heres some code to help you along the way :)
Code:
// This is an Abstract class, meaning its a "concept".
// See http://www.codeproject.com/KB/cs/jmabstractclasses.aspx for more info.
abstract class ListGenerator
{
// Here we are defining an abstract method.
// This means that we are saying anything that "IS A" ListGenerator can
// create an ArrayList by calling "GetList", however, we are not saying how its done.
public abstract ArrayList GetList();
}
// Here we are creating the ListGenerator specifically for listing Fonts.
class FontListGenerator : ListGenerator
{
// Here we are OVERRIDING (implementing) the abstract method,
// this is how we actually GET the List!
public override ArrayList GetList()
{
ArrayList rtn = new ArrayList();
// Add Fonts to the List.
FontFamily[] fonts = FontFamily.Families;
foreach (Font f in fonts)
{
rtn.Add(f.Name);
}
return rtn;
}
}
// and for the Form..
static class FormDemo
{
// This is the Constructor of the Form. Here we are saying, "I need a List Generator!"
public void Form(ListGenerator listGen)
{
// We know ListGenerators can give us a List, lets get it!
ArrayList list = listGen.GetList();
// Populate the List, we dont know these are font names, and we dont care, just fill it.
foreach (string s in list)
{
ListBox1.Add(s);
}
}
}
I hope this helps!
Rob
The Developing Developer
Currently Working Towards: MCAD C#
My Blog: http://www.robzyc.spaces.live.com
<center> "Nothing can stop the man with the right mental attitude from achieving his goal;
nothing on earth can help the man with the wrong mental attitude".
Thomas Jefferson</center>
|
|

March 20th, 2008, 09:36 AM
|
|
Authorized User
|
|
Join Date: Jul 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi rob
Reading your code makes sense to so thats good. Bij how do implement this in my code. I dont have a form. Im creating a from on a buttons click. How do i implement this:
static class FormDemo
{
// This is the Constructor of the Form. Here we are saying, "I need a List Generator!"
public void Form(ListGenerator listGen)
{
// We know ListGenerators can give us a List, lets get it!
ArrayList list = listGen.GetList();
// Populate the List, we dont know these are font names, and we dont care, just fill it.
foreach (string s in list)
{
ListBox1.Add(s);
}
}
into this:
private void button3_Click(object sender, EventArgs e)
{
// Create a new instance of the form.
Form form1 = new Form();
// Create The listbox.
ListBox listbox1 = new ListBox();
// Create two buttons to use as the accept and cancel buttons.
Button button1 = new Button ();
Button button2 = new Button ();
// Set the position of the Listbox on the form.
listbox1.Location = new Point(8, 8);
// Set the text of button1 to "OK".
button1.Text = "OK";
// Set the position of the button on the form.
button1.Location = new Point (201, 8);
// Set the text of button2 to "Cancel".
button2.Text = "Annuleren";
// Set the position of the button based on the location of button1.
button2.Location = new Point (button1.Left, button1.Height + button1.Top + 10);
// Make button1's dialog result OK.
button1.DialogResult = DialogResult.OK;
// Make button2's dialog result Cancel.
button2.DialogResult = DialogResult.Cancel;
// Set the caption bar text of the form.
form1.Text = "BTM - Textstyle";
// Define the border style of the form to a dialog box.
form1.FormBorderStyle = FormBorderStyle.FixedDialog;
// Set the accept button of the form to button1.
form1.AcceptButton = button1;
// Set the cancel button of the form to button2.
form1.CancelButton = button2;
// Set the start position of the form to the center of the screen.
form1.StartPosition = FormStartPosition.CenterParent;
// Add listbox1 to the form.
form1.Controls.Add(listbox1);
// Add button1 to the form.
form1.Controls.Add(button1);
// Add button2 to the form.
form1.Controls.Add(button2);
// Display the form as a modal dialog box.
form1.ShowDialog();
// Add fontitems to the listbox
FontFamily[] fontFam = System.Drawing.FontFamily.Families;
foreach (FontFamily ff in fontFam)
listbox1.Items.Add(ff.Name);
// Determine if the OK button was clicked on the dialog box.
if (form1.DialogResult == DialogResult.OK)
{
textBox1.Text = listbox1.SelectedItem.ToString();
form1.Dispose();
}
else
{
form1.Dispose();
}
}
|
|

March 20th, 2008, 10:23 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Hi MacDevv,
OK, I misunderstood your original post. I was under the impression that you already HAD a Form and where trying to populate the ListBox on it. I apologise, I did not read your code and I should have.
Why are you creating the form on the fly like this? It would make much more sense actually create the form in the designer? This will then ensure all your code is tidy and you can make the form modal and return the value based on what the user selects in the list.
Were you aware you could do this? I just trying to get more of a handle of your problem before I run off and do more code :)
Rob
The Developing Developer
Currently Working Towards: MCAD C#
My Blog: http://www.robzyc.spaces.live.com
<center> "Nothing can stop the man with the right mental attitude from achieving his goal;
nothing on earth can help the man with the wrong mental attitude".
Thomas Jefferson</center>
|
|

March 20th, 2008, 10:52 AM
|
|
Authorized User
|
|
Join Date: Jul 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi rob
I did i this because i dont know how tot get the selected font name back to my main form. Creating it on the fly gave me the opportunity to write the selected font name from the listbox to a textbox on my main form. Thats why i created the from on the fly.
Here i posted pictures of the form this will give you the idea.
http://img171.imageshack.us/img171/9072/formsef4.th.jpg
On the click on the ok button on the BTM - Textstyle form the selected fontname needs to be put in the textbox in front of the fontbutton.
Is ik can do this in a better manner plz im all for.
|
|

March 20th, 2008, 11:09 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
|
|
MacDevv,
OK, I think I understand what you are doing, they click the little "A" button, and the smaller form opens and asks them for the Font?
Two Points:
⢠You have a ComboBox with "Arial" in, why not just populate that combo with the font names and let the user change it there? Seems odd to get the user to do several clicks for something that is a standard.
⢠If you do want a dialog to pop up, then I REALLY recommend using the standard Font dialog, what exactly are your reasons for not using it?
There are several approaches and I want to make sure I give you the "best" one for your particular scenario. :)
Rob
The Developing Developer
Currently Working Towards: MCAD C#
My Blog: http://www.robzyc.spaces.live.com
<center> "Nothing can stop the man with the right mental attitude from achieving his goal;
nothing on earth can help the man with the wrong mental attitude".
Thomas Jefferson</center>
|
|
 |