|
What is the best way to validate dates when entered from a text box? formats can be mmddyy, mdyy mdyyyy etc.. Any code would be helpful..
|
|
|
|
|
I'd recommend using a DateTimePicker control.
For what you want to do, imagine the user types: "122004"
Is it:
* mdyyyy - Jan. 2, 2004
* mmddyy - Dec. 20, 2004
How do you know?
The DateTimePicker control has a masked edit already for entering dates, so use it with its Format property set to short date.
Also, it won't let the user type an invalid date.
-- LusiR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
you da man! Works great.. Our shop is new to all of this and we never thought about this.. Boss is happy.. I did a custom format MM/dd/yyyy.. we wanted to show the month and day as 01/01/2004 instead of 1/ 1/2004..
|
|
|
|
|
house wrote:
you da man!
I'm glad it helped. That control is very powerful (for example, see its Checked property, that although buggy, makes it display a check box) so it's always good to know it.
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
house wrote:
What is the best way to validate dates when entered from a text box? formats can be mmddyy, mdyy mdyyyy etc.. Any code would be helpful..
Check out the static method DateTime.Parse . It has a series of overloaded methods that allow you to pass in a IFormatProvider as well as specify any DateTimeStyles flags you prefer.
- Nick Parker My Blog | My Articles
|
|
|
|
|
Hello,
I have an MDI child form with a tab control on it, and many controls on each page.
When I open the form, I want to set the focus to a control on the first page. I've tried calling the control's Focus() method at the end of the constructor and in the OnLoad handler and it doesn't work. The focus is always initially on the tab control.
Any ideas?
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
I think I made it work. I'm doing it when a VisibleChanged event arrives, only the first time (after the Load event.)
Any better ideas?
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
I'm getting some unpredictable results when I divide two doubles. Here is an example.
If
double[] a = new double[6]{30.0, 3.0, 0.3, 0.03, 0.003, 0.0003};
double[] b = new double[6]{20.0, 2.0, 0.2, 0.02, 0.002, 0.0002};
then according to C# (.NET?)
a[0] / b[0] = 1.5
a[1] / b[1] = 1.5
a[2] / b[2] = 1.4999999999999998
a[3] / b[3] = 1.5
a[4] / b[4] = 1.5
a[5] / b[5] = 1.4999999999999998
Not a big difference in magnitude but my code also involves rounding the results which really throws things off.
Any help with an explanation and/or solution is greatly appreciated.
|
|
|
|
|
ldself wrote:
Any help with an explanation and/or solution is greatly appreciated.
Check out the Double.Epsilon Field[^], it is explained there. Here is a quick example:
double diff = (double)1/3 - (double).33333;
if(Math.Abs(diff) < Double.Epsilon)
Console.WriteLine("They are equal.");
else
Console.WriteLine("They are not equal.");
- Nick Parker My Blog | My Articles
|
|
|
|
|
Hi there,
The reason why 0.3/0.2 does not give an exact result is the following :
C# (and every other compiler) represents your variables in binary values.
For example:
2 is represented by 10 in binary which stands for (1*2^1 + 0*2^0)
4 100 (1*2^2 + 0*2^1 + 0*2^0)
30 11110 (1*2^4 + 1*2^3 + 1*2^2 + 1*2^1 + 0*2^0)
and so on
for values like 0.3, the binary value is not exact, because it is impossible to represent it exactly with this notation...
0.3 is 0,01001100110011... but the number never ends.
Since a double value is represented with 64 0 and 1, you loose a little precision in your operations. There is no real way to avoid this unfortunatly.
I hope this helps
|
|
|
|
|
h_migneron wrote:
There is no real way to avoid this unfortunatly
Actually there is. Double.Epsilon is a field that is defined to be the smallest postive double greater than zero. The idea is that if you take the absolute value of the difference between the two values you wish to compare and their result is less than Double.Epsilon , they are considered equal.
- Nick Parker My Blog | My Articles
|
|
|
|
|
A question for C# language experts:
If I have a struct that implements IDisposable:
struct C : IDisposable
{
public int clan;
public void Dispose()
{
Console.WriteLine("Disposing");
}
}
And later in the code something like:
using (C s = new C())
{
s.clan = 1;
}
A compiler error is reported:
error CS0131: The left-hand side of an assignment must be a variable, property or indexer
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
The problem lies with the fact that structs cant be safely casted, in this case IDisposable. The compiler will typical generate the following for the 'using' block:
IDisposable o = new C();
try
{
C s = o as C;
}
finally
{
o.Dispose();
}
Anyways, my best guess :p
top secret Download xacc-ide 0.0.3 now! See some screenshots
|
|
|
|
|
Hmmmm, but using as with structs causes
error CS0077: The as operator must be used with a reference type ('sharptest.C' is a value type)
And my code complains about assigning a value to a member of s.
Also, this compiles (and works) fine:
using (C s = new C())
{
Console.WriteLine(s.clan.ToString());
}
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
If U have the struct in the same namespace as ur code
the code below works.
C c = new C();
c.clan = 1;
Console.WriteLine(c.clan.ToString());
and if in different namespace -- in that case using can follow the initial using block. I mean immediately after
using System;
using System.Data;
....
using NameSpaceName.C;
C c = new C();
c.clan = 1;
Console.WriteLine(c.clan.ToString());
I hope this help and that i have not missunderstood ur question.
Ketty
|
|
|
|
|
|
According to the C# language spec[^]:
Local variables declared in a resource-acquisition are read-only, and must include an initializer. A compile-time error occurs if the embedded statement attempts to modify these local variables (by assignment or the ++ and -- operators) or pass them as ref or out parameters.
Wow!
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
Structures are allocated on the stack and not on the heap as classes.
Write the following code instead, it does the same trick:
C s = new C();
using (s)
{
s.clan = 1;
}
Ami
|
|
|
|
|
Ami Bar wrote:
Structures are allocated on the stack and not on the heap as classes.
That is correct in this context - I just didn't know that variables declared in using nlock need to be read-only.
BTW, what exactly "read-only" really means in C#? It does not have const methods like C++.
Whatever... Thanks for your answer
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
|
Good day all.
While attempting to use the ListView column Textalign property to 'Center' text with in the first column of my Listbox, it always uses the Left text align variable. I have a three column ListView. The sub items of this List accepts and aligns the text within the other columns 'Centered'.
Thank you in advance for your assistance.
Have a great coding day.
Regards,
Tony D. Abel
|
|
|
|
|
Please check all your columns and there TextAlign property in your listview control. And make sure the value of TextAlign property is 'Center'.
Sreejith Nair
[ My Articles ]
|
|
|
|
|
hi, i've added user control onto my main form. but when i run my program, i realise it eats up a lot of resources. my com becames very lag. what's the problem?
Chris
|
|
|
|
|
What sort of user control that you are using ? i mean what it does ?
Sreejith Nair
[ My Articles ]
|
|
|
|
|
i've this main window form where it contains 4 user control. each of this user control connects to the database. after i added these user control onto my main form and run my program, my com became very lag.
Chris
|
|
|
|