reagan123 Posted August 30, 2005 Posted August 30, 2005 i'm using the following piece of code int currentYear = DateTime.Now.Year; right now it gives me '2005'... how can get it to be '05' this is an asp.net/c# web project thanks Quote
Joe Mamma Posted August 30, 2005 Posted August 30, 2005 i'm using the following piece of code int currentYear = DateTime.Now.Year; right now it gives me '2005'... how can get it to be '05' thanks well. . . '05' is a string. . . do you want the integer 5 or the string '05' to get the the string '05' string currentYear = DateTime.Now.ToString('yy'); from there you can figure out how to get integer 5. Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
reagan123 Posted August 30, 2005 Author Posted August 30, 2005 thanks.. basically all i'm wanting is to populate a dropdown box with the following values 05 06 07 08 09 10 Here is the definition from the api i'm using cardexpyear -- integer from 00 to 99 -- The two-digit expiration year of the credit card Quote
Joe Mamma Posted August 30, 2005 Posted August 30, 2005 dont mix format with value. . . Do something like the following: public class YearValue { int _value; int _min; int _max; public string Display { get { return _value.ToString().PadLeft(4,'0').Substring(2,2); } } public int Value { get { return _value; } set { if ((value >= _min) && (value <= _max)) _value = value; else throw new ArgumentException("Value out of range"); } } public YearValue() { _min = 2000; _max = 2010; _value = 2000; } public YearValue(int min, int max) { _min = min; _max = max; _value = min; } public YearValue(int min, int max, int value) { _min = min; _max = max; Value = value; } } usage System.Collections.ArrayList al = new ArrayList(); al.Add(new YearValue(2005, 2010, 2005)); al.Add(new YearValue(2005, 2010, 2006)); al.Add(new YearValue(2005, 2010, 2007)); al.Add(new YearValue(2005, 2010, 2008)); al.Add(new YearValue(2005, 2010, 2009)); al.Add(new YearValue(2005, 2010, 2010)); comboBox1.DataSource = al; comboBox1.DisplayMember = "Display"; comboBox1.ValueMember = "Value"; Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
reagan123 Posted August 31, 2005 Author Posted August 31, 2005 thanks! that looks like a perfect solution... i'll try and implement it later today! Quote
Joe Mamma Posted August 31, 2005 Posted August 31, 2005 there are many ways to filet this feline. . . and maybe this is simpler. . . though the value of the combobox will be a DateValue object where the first one the value was an int. . . public class YearValue { int _value; public override string ToString() { string s = _value.ToString().PadLeft(4,'0'); return s.Substring(s.Length-2,2); } public YearValue(int value) { _value = value; } public int Value { get { return _value; } set { _value = value; } } } and to load: comboBox1.DataSource = new ArrayList( new object[] { new YearValue(2005), new YearValue(2006), new YearValue(2007), new YearValue(2008), new YearValue(2009), new YearValue(2010) } ); set the DropDownStyle of the combo to DropDownList. And to get the actual year value: MessageBox.Show((comboBox1.SelectedValue as YearValue).Value.ToString()); do you need to allow a null selection in the combo??? comboBox1.DataSource = new ArrayList( new object[] { string.Empty; new YearValue(2005), new YearValue(2006), new YearValue(2007), new YearValue(2008), new YearValue(2009), new YearValue(2010) } ); then you can add a property to your form: protected YearValue SelectedYear { get { //returns null if empty string is selected. . . return comboBox1.SelectedValue as YearValue; } } then to get the value: if (this.SelectedYear == null) MessageBox.Show("Please Select a Year") else MessageBox.Show(this.SelectedYear.Value.ToString()); Again. . .the concept here is that the control only manages the GUI events and presentation, That is, it presents the selected object from the bound collection to the GUI. It has a reference to the collection of objects but it dose not explicitly contain the objects. I hope this sparks some ideas. Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
Joe Mamma Posted August 31, 2005 Posted August 31, 2005 if (this.SelectedYear == null) MessageBox.Show("Please Select a Year") else MessageBox.Show(this.SelectedYear.Value.ToString()); or - if (this.SelectedYear == null) MessageBox.Show("Please Select a Year") else MessageBox.Show(string.format("{0} = {1}", this.SelectedYear.ToString(), this.SelectedYear.Value.ToString())); Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
Joe Mamma Posted August 31, 2005 Posted August 31, 2005 now. . . all that being said - Do you need more than one of these selected year combo's in your app??? It may be worth your while to inherit from ComboBox and publish out a max, min, AllowNull and SelectedYear properties. Implement ISupoportInitialize and Create and Populate the bound collection in it's EndInit method (only if not designmode!!!) - public class YearCombo : System.Windows.Forms.ComboBox, ISupportInitialize { int _min; int _max; bool _allowNull; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public YearCombo(System.ComponentModel.IContainer container) { /// /// Required for Windows.Forms Class Composition Designer support /// container.Add(this); InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } public YearCombo() { /// /// Required for Windows.Forms Class Composition Designer support /// InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Component Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { components = new System.ComponentModel.Container(); } #endregion #region ISupportInitialize Members public void BeginInit() { // TODO: Add YearCombo.BeginInit implementation } public void EndInit() { if (DesignMode) return; ArrayList al = new ArrayList(); if (_allowNull) al.Add(string.Empty); for (int i = _min; i <= _max; i++) al.Add(new YearValue(i)); this.DataSource = al; } #endregion public int MinYear { get { return _min; } set { if (_max < value) throw new ArgumentException( string.Format("Minimum Year must be less than or equal to {0}", _max.ToString())); _min = value; this.EndInit(); } } public int MaxYear { get { return _max; } set { if (_min > value) throw new ArgumentException( string.Format("Maximum Year must be greater than or equal to {0}", _min.ToString())); _max = value; this.EndInit(); } } public bool AllowNull { get { return _allowNull; } set { if (_allowNull = value) return; _allowNull = value; EndInit(); } } public YearValue SelectedYear { get { //returns null if empty string is selected. . . return this.SelectedValue as YearValue; } } } public class YearValue { int _value; public override string ToString() { string s = _value.ToString().PadLeft(4,'0'); return s.Substring(s.Length-2,2); } public YearValue(int value) { _value = value; } public int Value { get { return _value; } set { _value = value; } } } Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
Joe Mamma Posted August 31, 2005 Posted August 31, 2005 oops. . .just saw that this is ASP.NET!!! the same ideas should apply! Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.