 |
| C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# 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 11th, 2004, 08:14 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 119
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Question Re: DropDown box in Windows Form
In creating a dropdown box in a WINDOWS FORM, not a web form, that contains each month of the year I did the following:
//
// lstMonthText
//
this.lstMonthText.Items.AddRange(new object[] {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"});
this.lstMonthText.Location = new System.Drawing.Point(560, 40);
this.lstMonthText.Name = "lstMonthText";
this.lstMonthText.Size = new System.Drawing.Size(112, 17);
this.lstMonthText.TabIndex = 1;
this.lstMonthText.SelectedIndexChanged += new System.EventHandler(this.lstMonthText_SelectedInde xChanged);
However I am needing now to only list the CURRENT MONTH and the PREVIOUS MONTH in this dropdown list per the customer. In C#, does anyone have any code examples or direct me to a code example that could help me with this? Any help or direction would be appreciated.
Thanks.
|
|

March 11th, 2004, 05:24 PM
|
|
Authorized User
|
|
Join Date: Nov 2003
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
To load the list how about ...
Code:
DateTime dtMonth;
dtMonth = DateTime.Now;
this.lstMonthText.Items.Add(dtMonth.ToString("MMMM"));
this.lstMonthText.Items.Add(dtMonth.AddMonths(-1).ToString("MMMM"));
// Select 'this' month as the default
this.lstMonthText.SelectedIndex = 0;
Then the list will always be current with this month and last month.
|
|

March 11th, 2004, 05:30 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 119
Thanks: 0
Thanked 1 Time in 1 Post
|
|
KABay,
Thanks a lot. THAT GOT IT!!
Your help is appreciated!!
|
|

March 12th, 2004, 11:45 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 119
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Ran into a problem. I am needing to pass on the INT value of the month into my stored procedure from these two items listed in KABay's example below.
Is there not a way to assign the INT value associated with the month listed in my LISTBOX like you would in HTML?
Here is the code example:
//
// lstMonthText
//
DateTime dtMonth;
dtMonth = DateTime.Now;
// 3/10/2004 = Per customer, define only the current and previous months in the dropdown.
this.lstMonthText.Items.Add(dtMonth.AddMonths(-1).ToString("MMMM").);
this.lstMonthText.Items.Add(dtMonth.ToString("MMMM "));
//Select 'this' month as the default
this.lstMonthText.SelectedIndex = 0;
this.lstMonthText.Location = new System.Drawing.Point(560, 40);
this.lstMonthText.Name = "lstMonthText";
this.lstMonthText.Size = new System.Drawing.Size(112, 17);
this.lstMonthText.TabIndex = 1;
this.lstMonthText.SelectedIndexChanged += new System.EventHandler(this.lstMonthText_SelectedInde xChanged);
Thank you.
|
|

March 12th, 2004, 01:59 PM
|
|
Authorized User
|
|
Join Date: Nov 2003
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sorry this is a little long but it IS fully functional. Just cut and paste (replace) all the code in an empty/new C# Solution.
The key concept here is that you are defining a data source from which to extract the 'value' you are looking for for the month selected.
(This code was 'ripped' from the MSDN Library)
Code:
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
namespace MyListControlSample
{
public class MonthItem
{
private string myMonthNumber;
private string myMonthName;
public MonthItem(string strMonthName, string strMonthNumber)
{
this.myMonthNumber = strMonthNumber;
this.myMonthName = strMonthName;
}
public string MonthNumber
{
get
{
return myMonthNumber;
}
}
public string MonthName
{
get
{
return myMonthName;
}
}
public override string ToString()
{
return this.MonthName + " - " + this.MonthNumber;
}
}
public class ListBoxWithValue:Form
{
private ListBox lstboxMonths = new ListBox();
private TextBox txtMonthValue = new TextBox();
[STAThread]
static void Main()
{
Application.Run(new ListBoxWithValue());
}
public ListBoxWithValue()
{
this.AutoScaleBaseSize = new Size(5, 13);
this.ClientSize = new Size(292, 181);
this.Text = "ListBox Sample3";
lstboxMonths.Location = new Point(24, 16);
lstboxMonths.Name = "lstboxMonths";
lstboxMonths.Size = new Size(232, 130);
txtMonthValue.Location = new Point(24, 160);
txtMonthValue.Name = "txtMonthValue";
txtMonthValue.Size = new Size(240, 24);
this.Controls.AddRange(new Control[] {lstboxMonths, txtMonthValue});
// ***************************************
// Start the really significant stuff
// ***************************************
// Populates the list box using DataSource.
ArrayList MonthItems = new ArrayList();
DateTime dtMonth;
string sMonthName;
string sMonthValue;
dtMonth = DateTime.Now;
sMonthName = dtMonth.ToString("MMMM");
sMonthValue = dtMonth.ToString("MM");
MonthItems.Add(new MonthItem(sMonthName, sMonthValue));
dtMonth = dtMonth.AddMonths(-1);
sMonthName = dtMonth.ToString("MMMM");
sMonthValue = dtMonth.ToString("MM");
MonthItems.Add(new MonthItem(sMonthName, sMonthValue));
// ***************************************
// End the really significant stuff
// ***************************************
lstboxMonths.SelectedValueChanged += new EventHandler(lstboxMonths_SelectedValueChanged);
lstboxMonths.DataSource = MonthItems;
lstboxMonths.DisplayMember = "MonthName";
lstboxMonths.ValueMember = "MonthNumber";
}
private void InitializeComponent()
{
}
private void lstboxMonths_SelectedValueChanged(object sender, EventArgs e)
{
if (lstboxMonths.SelectedIndex != -1)
txtMonthValue.Text = lstboxMonths.SelectedValue.ToString();
}
}
}
|
|

March 15th, 2004, 09:10 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 119
Thanks: 0
Thanked 1 Time in 1 Post
|
|
KABay,
thanks for taking the time to come up with this code. I will try it out this morning.
Again, your help and time are very much appreciated.
|
|
 |