PlaTyPuS8

Hi!

How can I do that I want to put these Times in a combobox....

Thx for help!!

PlaTyPuS



Re: Visual C# General Make an Enumeration of Times (00:00 to 23:30)

decyclone

Hi Platypus,

Try this :

public partial class TimeComboBox : ComboBox
{
public TimeComboBox()
: base()
{
InitializeComponent();
}

protected new ComboBox.ObjectCollection Items
{
get { return base.Items; }
}

protected override void OnCreateControl()
{
base.OnCreateControl();
PopulateItems();
}

private void PopulateItems()
{
DateTime dt = new DateTime(2000, 1, 1, 0, 0, 0);

for (int Index = 0; Index < 48; Index++)
{
Items.Add(new DateTimeObject(dt));
dt = dt.AddMinutes(30);
}
}

public DateTime SelectedTime
{
get {
if (SelectedIndex >= 0)
{
return ((DateTimeObject)SelectedItem).dt;
}
throw new IndexOutOfRangeException("Please select an Item first.");
}
}

public Double SelectedHour
{
get {
return SelectedTime.Hour;
}
}

public Double SelectedMinute
{
get {
return SelectedTime.Minute;
}
}
}

class DateTimeObject
{
public DateTime dt = DateTime.MinValue;

public DateTimeObject(DateTime dt)
{
this.dt = dt;
}

public override string ToString()
{
String Output = String.Format("{0:00}:{1:00}",dt.Hour,dt.Minute);
return Output;
}
}





Re: Visual C# General Make an Enumeration of Times (00:00 to 23:30)

PlaTyPuS8

hi!

thanks for your reply!

I tried it in my WPF project... but ComboBox.ObjectCollection isn't recognize...

I searched on MSDN and found the class http://msdn2.microsoft.com/en-us/library/system.windows.forms.combobox.objectcollection.aspx but it doesn't help me.

The error message is : Error 28 The type name 'ObjectCollection' does not exist in the type 'System.Windows.Controls.ComboBox'

Do you have an idea

thx Wink





Re: Visual C# General Make an Enumeration of Times (00:00 to 23:30)

PlaTyPuS8

Can I simply import System.Windows.Forms

will it work with the other WPF (System.Windows.Controls) controls





Re: Visual C# General Make an Enumeration of Times (00:00 to 23:30)

decyclone

Oh, WPF ComboBox !! you should have told me that before...

Anyways just check if System.Windows.ComboBox contains an Items Collection like System.Windows.Forms.ComboBox or not. If yes see it's data type and replace ObjectCollection with that one. If no, show me some code how you add new items to that ComboBox. I just want to disallow anyone add any new items besides the default ones.





Re: Visual C# General Make an Enumeration of Times (00:00 to 23:30)

PlaTyPuS8

yes, sorry...

I changed the 2 following methods :

Code Snippet

protected new ItemCollection Items

{

get { return base.Items; }

}

public override void BeginInit()

{

base.BeginInit();

PopulateItems();

}

I think the first one is ok, but replace OnCreateControl() with BeginInit() ...

But now I have the following error :

Error 28 The name 'InitializeComponent' does not exist in the current context.

InitializeComponent should be ok in WPF... no

Thx





Re: Visual C# General Make an Enumeration of Times (00:00 to 23:30)

decyclone

Sorry about that, just remove the call to InitializeComponent();





Re: Visual C# General Make an Enumeration of Times (00:00 to 23:30)

PlaTyPuS8

decyclone wrote:
Sorry about that, just remove the call to InitializeComponent();

it works!!!!

Here is the complete class :

FOR WPF :

Code Snippet

public partial class TimeComboBox : ComboBox

{

public TimeComboBox()

: base()

{

}

protected new ItemCollection Items

{

get { return base.Items; }

}

public override void BeginInit()

{

base.BeginInit();

PopulateItems();

}

private void PopulateItems()

{

DateTime dt = new DateTime(2000, 1, 1, 0, 0, 0);

for (int Index = 0; Index < 48; Index++)

{

Items.Add(new DateTimeObject(dt));

dt = dt.AddMinutes(30);

}

}

public DateTime SelectedTime

{

get

{

if (SelectedIndex >= 0)

{

return ((DateTimeObject)SelectedItem).dt;

}

throw new IndexOutOfRangeException("Please select an Item first.");

}

}

public Double SelectedHour

{

get

{

return SelectedTime.Hour;

}

}

public Double SelectedMinute

{

get

{

return SelectedTime.Minute;

}

}

}

class DateTimeObject

{

public DateTime dt = DateTime.MinValue;

public DateTimeObject(DateTime dt)

{

this.dt = dt;

}

public override string ToString()

{

String Output = String.Format("{0:00}:{1:00}", dt.Hour, dt.Minute);

return Output;

}

}

thx again decyclone!!!





Re: Visual C# General Make an Enumeration of Times (00:00 to 23:30)

decyclone

Hey, mark the post as right answer, so anyone in future having the same problem as you can find a solution.