Custome control from DateTime format string

  • Thread starter Thread starter gregcm
  • Start date Start date
G

gregcm

I've got a custom control that is trying to allow the user to enter a
DateTime in a definable format and I was wondering if there was an
easy way to alter the look of said control based on the contents of
the format string. For example I would like a year TextBox that is
small if input formatting string contains "yy" and larger if it
contains "yyyy".
I guess I could write a parser with string.Contains et. al. but that
seems fairly kludgy, any ideas?
 
(e-mail address removed) skrev:
I've got a custom control that is trying to allow the user to enter a
DateTime in a definable format and I was wondering if there was an
easy way to alter the look of said control based on the contents of
the format string. For example I would like a year TextBox that is
small if input formatting string contains "yy" and larger if it
contains "yyyy".
I guess I could write a parser with string.Contains et. al. but that
seems fairly kludgy, any ideas?

Use DateTimePicker dtp; with dtp.Format=DateTimePickerFormat.Custom, and
dtp.CustomFormat="yy.MM.dd HH:mm" for example.

Believe me, you will never be able to trap all variants a user might try
to use in a TextBox.

Use dtp.Value.ToString("yy.MM.dd HH:mm") to get out whatever format you
want.
 
Bjørn Brox skrev:
(e-mail address removed) skrev:

Use DateTimePicker dtp; with dtp.Format=DateTimePickerFormat.Custom, and
dtp.CustomFormat="yy.MM.dd HH:mm" for example.

Believe me, you will never be able to trap all variants a user might try
to use in a TextBox.

Use dtp.Value.ToString("yy.MM.dd HH:mm") to get out whatever format you
want.
BTW, below is a code that I had to implement to solve the "problem" I
had to tell the user that he had to enter a date and that this was done.
A DateTimePicker use DateTime.Now as initial value.

The below code replaces the DateTimePicker with a button which is
replaced by the original DateTimePicker pointed to in the Tag element
when hit.

I my code I just test on DateTimePicker.Visible to find out if the user
has entered a date or not (which also could be detected by adding a
ValueChanged eventhandler).

There is just one unsolved problem with the code below: When the user
hits the button the Calender should open as well.

public static void DateTimePicker2Button(DateTimePicker dtp)
{
dtp.Visible = false;
Button dtp_button = new Button();

dtp_button.Font = new System.Drawing.Font("Tahoma", 6F,
System.Drawing.FontStyle.Bold);

dtp_button.Location = dtp.Location;
dtp_button.Name = dtp.Name + "Button";
dtp_button.Size = dtp.Size;
dtp_button.Text = "<Assign Date>";
dtp_button.Click += new EventHandler(dtp_button_Click);
dtp_button.Visible = true;
dtp_button.Tag = dtp;

dtp.Parent.Controls.Add(dtp_button);
}

internal static void dtp_button_Click(object sender, EventArgs e)
{
Button dtp_button = (Button)sender;
DateTimePicker dtp = (DateTimePicker)dtp_button.Tag;

dtp_button.Visible = false;
dtp.Parent.Controls.Remove(dtp_button);
dtp_button.Dispose();

dtp.Value = DateTime.Now;
dtp.Visible = true;
}
 
Hi,

You need to give more details, is this a web or a win app?
Where you got your control from?
Do you have the source code (you are talking about modifying it after all)
 
Back
Top