Custom properties

G

Guest

I have created a control and added a few properties. One of these is an
array of DateTime objects, looking like this:

[Browsable(true), EditorBrowsable(EditorBrowsableState.Always),
Description("whatever"), Category("look")]
public DateTime [] MyDates
{
get { return theDate;}
set {theDate = value;}
}

When I add my control to the form I can open the DateTime collection editor
from the properties window and set certain dates, but they do not seem to
show on my control, whereas creating an array of DateTimes in code works
fine. In fact, when I go back into the DateTime editor after running the
program, the list only contains one entry, despite having entered 6
previously.

Does anyone know a way round this?
 
D

Dave Sexton

Hi,

If you are referring to a web control, then none of the values you enter in the collection editor should be preserved since
DateTime[] cannot be automatically converted to and from a string representation. For that you'd have to create a custom
TypeConverter.

I tested your claim in a WinForms 2.0 app. All of the dates that I added in the collection editor to the DateTime[] property were
preserved at runtime. I cannot reproduce the behavior you have specified.

Here's the bulk of my control class:

public DateTime[] MyDates
{
get { return dates; }
set
{
dates = value;
Invalidate();
}
}

private DateTime[] dates;

protected override void OnPaint(PaintEventArgs pe)
{
if (dates == null)
pe.Graphics.DrawString("Null", Font, Brushes.Black, 0, 0);
else
{
int fontHeight = Font.Height;
int y = 0;

foreach (DateTime dt in dates)
{
pe.Graphics.DrawString(dt.ToString(), Font, Brushes.Black, 0, y);

y += fontHeight + 2;
}
}

// Calling the base class OnPaint
base.OnPaint(pe);
}

Is there anything in particular that I'm missing?
 
G

Guest

Hi Dave,

many thanks for replying.

I am creating a Windows application using Visual Studio .NET 2003, so I
don't know how valid this information will be. I don't see how the paint
handler will affect the properties window at design time. I have since
entered a few more dates, closed the dialog, then opened it again. None of
the dates I entered were still visible.

I think there must be a simple way to create a collection property in a
Windows application and make it available in the properties window.

Thanks again, Dave,

Mac

Dave Sexton said:
Hi,

If you are referring to a web control, then none of the values you enter in the collection editor should be preserved since
DateTime[] cannot be automatically converted to and from a string representation. For that you'd have to create a custom
TypeConverter.

I tested your claim in a WinForms 2.0 app. All of the dates that I added in the collection editor to the DateTime[] property were
preserved at runtime. I cannot reproduce the behavior you have specified.

Here's the bulk of my control class:

public DateTime[] MyDates
{
get { return dates; }
set
{
dates = value;
Invalidate();
}
}

private DateTime[] dates;

protected override void OnPaint(PaintEventArgs pe)
{
if (dates == null)
pe.Graphics.DrawString("Null", Font, Brushes.Black, 0, 0);
else
{
int fontHeight = Font.Height;
int y = 0;

foreach (DateTime dt in dates)
{
pe.Graphics.DrawString(dt.ToString(), Font, Brushes.Black, 0, y);

y += fontHeight + 2;
}
}

// Calling the base class OnPaint
base.OnPaint(pe);
}

Is there anything in particular that I'm missing?

--
Dave Sexton

cashdeskmac said:
I have created a control and added a few properties. One of these is an
array of DateTime objects, looking like this:

[Browsable(true), EditorBrowsable(EditorBrowsableState.Always),
Description("whatever"), Category("look")]
public DateTime [] MyDates
{
get { return theDate;}
set {theDate = value;}
}

When I add my control to the form I can open the DateTime collection editor
from the properties window and set certain dates, but they do not seem to
show on my control, whereas creating an array of DateTimes in code works
fine. In fact, when I go back into the DateTime editor after running the
program, the list only contains one entry, despite having entered 6
previously.

Does anyone know a way round this?
 
D

Dave Sexton

Hi Mac,
I am creating a Windows application using Visual Studio .NET 2003, so I
don't know how valid this information will be.

I tested the code in VS.NET 2003 as well and I was able to repro the described behavior. By simply initializing the 'dates' field
to a non-null value I was able to get the desired behavior. Apparently they fixed this behavior in VS.NET 2005 so that a collection
is still handled properly by the designer when it's initialized to null in the class.

public DateTime[] MyDates
{
get { return dates; }
set
{
dates = value;
Invalidate();
}
}

// field must be initialized to a non-null value
private DateTime[] dates = new DateTime[0];
I don't see how the paint
handler will affect the properties window at design time.

It won't. The purpose of painting was to display the dates that are part of the collection at design-time so I wouldn't have to
keep building and running the app.
I have since
entered a few more dates, closed the dialog, then opened it again. None of
the dates I entered were still visible.
I think there must be a simple way to create a collection property in a
Windows application and make it available in the properties window.

This is the simplest way that I'm aware of, and it's pretty simple, IMO. You might want to post your code if my suggestion doesn't
work for you.

--
Dave Sexton

cashdeskmac said:
Hi Dave,

many thanks for replying.

I am creating a Windows application using Visual Studio .NET 2003, so I
don't know how valid this information will be. I don't see how the paint
handler will affect the properties window at design time. I have since
entered a few more dates, closed the dialog, then opened it again. None of
the dates I entered were still visible.

I think there must be a simple way to create a collection property in a
Windows application and make it available in the properties window.

Thanks again, Dave,

Mac

Dave Sexton said:
Hi,

If you are referring to a web control, then none of the values you enter in the collection editor should be preserved since
DateTime[] cannot be automatically converted to and from a string representation. For that you'd have to create a custom
TypeConverter.

I tested your claim in a WinForms 2.0 app. All of the dates that I added in the collection editor to the DateTime[] property
were
preserved at runtime. I cannot reproduce the behavior you have specified.

Here's the bulk of my control class:

public DateTime[] MyDates
{
get { return dates; }
set
{
dates = value;
Invalidate();
}
}

private DateTime[] dates;

protected override void OnPaint(PaintEventArgs pe)
{
if (dates == null)
pe.Graphics.DrawString("Null", Font, Brushes.Black, 0, 0);
else
{
int fontHeight = Font.Height;
int y = 0;

foreach (DateTime dt in dates)
{
pe.Graphics.DrawString(dt.ToString(), Font, Brushes.Black, 0, y);

y += fontHeight + 2;
}
}

// Calling the base class OnPaint
base.OnPaint(pe);
}

Is there anything in particular that I'm missing?

--
Dave Sexton

cashdeskmac said:
I have created a control and added a few properties. One of these is an
array of DateTime objects, looking like this:

[Browsable(true), EditorBrowsable(EditorBrowsableState.Always),
Description("whatever"), Category("look")]
public DateTime [] MyDates
{
get { return theDate;}
set {theDate = value;}
}

When I add my control to the form I can open the DateTime collection editor
from the properties window and set certain dates, but they do not seem to
show on my control, whereas creating an array of DateTimes in code works
fine. In fact, when I go back into the DateTime editor after running the
program, the list only contains one entry, despite having entered 6
previously.

Does anyone know a way round this?
 
G

Guest

Thanks again, Dave.

I will have a play with that.

Dave Sexton said:
Hi Mac,
I am creating a Windows application using Visual Studio .NET 2003, so I
don't know how valid this information will be.

I tested the code in VS.NET 2003 as well and I was able to repro the described behavior. By simply initializing the 'dates' field
to a non-null value I was able to get the desired behavior. Apparently they fixed this behavior in VS.NET 2005 so that a collection
is still handled properly by the designer when it's initialized to null in the class.

public DateTime[] MyDates
{
get { return dates; }
set
{
dates = value;
Invalidate();
}
}

// field must be initialized to a non-null value
private DateTime[] dates = new DateTime[0];
I don't see how the paint
handler will affect the properties window at design time.

It won't. The purpose of painting was to display the dates that are part of the collection at design-time so I wouldn't have to
keep building and running the app.
I have since
entered a few more dates, closed the dialog, then opened it again. None of
the dates I entered were still visible.
I think there must be a simple way to create a collection property in a
Windows application and make it available in the properties window.

This is the simplest way that I'm aware of, and it's pretty simple, IMO. You might want to post your code if my suggestion doesn't
work for you.

--
Dave Sexton

cashdeskmac said:
Hi Dave,

many thanks for replying.

I am creating a Windows application using Visual Studio .NET 2003, so I
don't know how valid this information will be. I don't see how the paint
handler will affect the properties window at design time. I have since
entered a few more dates, closed the dialog, then opened it again. None of
the dates I entered were still visible.

I think there must be a simple way to create a collection property in a
Windows application and make it available in the properties window.

Thanks again, Dave,

Mac

Dave Sexton said:
Hi,

If you are referring to a web control, then none of the values you enter in the collection editor should be preserved since
DateTime[] cannot be automatically converted to and from a string representation. For that you'd have to create a custom
TypeConverter.

I tested your claim in a WinForms 2.0 app. All of the dates that I added in the collection editor to the DateTime[] property
were
preserved at runtime. I cannot reproduce the behavior you have specified.

Here's the bulk of my control class:

public DateTime[] MyDates
{
get { return dates; }
set
{
dates = value;
Invalidate();
}
}

private DateTime[] dates;

protected override void OnPaint(PaintEventArgs pe)
{
if (dates == null)
pe.Graphics.DrawString("Null", Font, Brushes.Black, 0, 0);
else
{
int fontHeight = Font.Height;
int y = 0;

foreach (DateTime dt in dates)
{
pe.Graphics.DrawString(dt.ToString(), Font, Brushes.Black, 0, y);

y += fontHeight + 2;
}
}

// Calling the base class OnPaint
base.OnPaint(pe);
}

Is there anything in particular that I'm missing?

--
Dave Sexton

I have created a control and added a few properties. One of these is an
array of DateTime objects, looking like this:

[Browsable(true), EditorBrowsable(EditorBrowsableState.Always),
Description("whatever"), Category("look")]
public DateTime [] MyDates
{
get { return theDate;}
set {theDate = value;}
}

When I add my control to the form I can open the DateTime collection editor
from the properties window and set certain dates, but they do not seem to
show on my control, whereas creating an array of DateTimes in code works
fine. In fact, when I go back into the DateTime editor after running the
program, the list only contains one entry, despite having entered 6
previously.

Does anyone know a way round this?
 

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