|
Well, an extension method is a new method declaration in 3.5 and above. Think of it as sort of a helper method and overload at the same time.
From MSDN:
'Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.'
Example:
namespace ExtensionMethods
{
public static class MyExtensions
{
public static int WordCount(this String str)
{
return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
}
}
}
In this example the extension method is WordCount which returns an int , and can be called using the construct:
String myString = "Hello World";
int count = myString.WordCount();
If you look at the string class you won't find a 'WordCount ' method, it has been made a 'custom extension' of the string class.
So for your date picker you could create extension methods and customize the class how ever you would like with out inheritance etc.
Some good Extension Method Resources:
Extension Methods (C# Programming Guide)[^]
C# .NET Extension Methods 3.0[^]
I have run into this problem with the DateTimePicker and used this type of solution to solve the problem.
additionally if you do use an extension method for the DateTimePicker you can name it so the code reflects the fact that you have made a specialization to the date time returned. Ie. DateTimePicker.MyDateTime() ;
I think Luc has an article on this geared for the DateTime class.
~TheArch
|
|
|
|
|
ASP.net Ajax Control Toolkit Color Picker Extender have a SampleControlID property, that shows the selected color's hexadecimal color code value for further use. But some time for better ui visibility need, if you don't want that hexadecimal color code to appear in the same control, then here is the way to do that.
In this sample code below:
I used a TextBox (txtColor) as ColorPickerExtender's SampleControlID and TargetControlID.
I used a ImageButton(ibtnCollorPicker) with ColorPickerExtender's PopupButtonID.
Now, when I click on the ibtnCollorPicker, this will show the colorpicker. Any color selection done, will show the hexa value in the txtColor.
Method 1: (Mostly Used By Developers)
On color selection, the selected color of color picker becomes the both the fore color and background color of the textbox. So since the hexa value does not appear directly.
<asp:TextBox ID="txtColor" runat="server"></asp:TextBox>
<asp:ImageButton ID="ibtnCollorPicker" runat="server" ImageUrl="~/Images/color_button.png"
ToolTip="Pick Color"></asp:ImageButton>
<cc:ColorPickerExtender ID="txtColor_ColorPickerExtender" runat="server"
http://www.mindfiresolutions.com/Hiding-hexadecimal-color-code-in-ColorPickerExtenders-Sample-Control-679.php[^]
TargetControlID="txtColor" SampleControlID="txtColor"
OnClientColorSelectionChanged="changeColor"
PopupButtonID="ibtnCollorPicker">
</cc:ColorPickerExtender>
function changeColor(sender)
{
sender.get_element().style.color = sender.get_selectedColor();
}
It works fine but it has a small issue. After selection, if you highlight textbox text with the help of mouse, then the hexa value will appear again.
Method 2 contains solution of this.
Cheers,
Eliza
|
|
|
|
|
Hi All
Most applications need the ability to print reports of one sort or another.
Printing classes are tedious and difficult to program.
I am wondering: what do people on here do when you need printing capabilites (for reports etc)?
|
|
|
|
|
Ryan Minor wrote: Printing classes are tedious and difficult to program.
Initially one might feel that way but not once you get hold of them.
Ryan Minor wrote: what do people on here do when you need printing capabilites (for reports etc)?
Depends on the need. Use crystal reports or SSRS if possible or else go with the Printing classes available.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
I go with SSRS, it has all the functionality built-in that we need for all the projects I've worked on. It's also nice to have a separate Report project and be able to develop the Reports in VS2005.
Hold on a second here... Don't you think you might be putting the horse ahead of the cart?
|
|
|
|
|
Like d@nish says, it could be a daunting task at the begining but once you have written it once you can re-use it over and over again all the time. I wroyte my own some time ago when I needed one and I just add the class into my new projects now. Took a while to get it written but once I got it working it is great.
Excellence is doing ordinary things extraordinarily well.
|
|
|
|
|
I cheat by spitting out a beautifully formatted temporary HTML file and invoking the print verb on it, as shown in this[^] freebie I wrote.
/ravi
|
|
|
|
|
I have date time pickers in a windows form application. I want to set their date time format as 'dd/MM/yy' without changing the Regional settings from control panel.
|
|
|
|
|
DateTimePicker does have a CustomFormat property. That should help you out.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
Thanx d@nish 4 ur reply. I tried it b4 but i get problem inserting or updating data from the date pickers if i apply custom date format. Is there any way of doing it by regional settings? How can i set the application's regional settings so that the date pickers will show the date in "dd/MM/yy" format.
|
|
|
|
|
What is the problem you get during insert/update?
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
An exception arises during converting string to datetime. But, if i use other formats, there is no need of convertion and the date is inserted successfully.
|
|
|
|
|
You should try and avoid such conversions. There are overloads of conversion methods which take IFormatProvider as a parameter. You can make use of them.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
|
Hello
I have a datagridview binded to a datatable : Wine
This datatable contains
CepageID
Cepage
percent
DGV shows Cepage and percent
I have another datatable Cepages
When the user wants to edit a cepage in the DGV I want to switch the textboxcell to a comboboxcell showing the different available cepages
Is it possible with a binded datagridview ?
Thanks for any help
|
|
|
|
|
Why not use DataGridViewComboboxColumn right away?
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
Thanks for your answer !
I do not use Combobox column because I want to keep the normal textbox display when the cell is not editing !
Combobox display takes to much space in the grid to display the item properly !
|
|
|
|
|
Set these properties for your DataGridView as below:
comboboxColumn.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
yourDataGridView.EditMode = DataGridViewEditMode.EditOnEnter;
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
woaw that sounds great
and so simple
thank you I'll try it
|
|
|
|
|
Hello d@nish
Your suggestion was fine !
I've done it that way
cbxCepage.DataSource = sCodes.dtCepages;
cbxCepage.DisplayMember = "Value";
cbxCepage.ValueMember = "ID";
cbxCepage.DataPropertyName = "ID";
dgvCepages.DataSource = null;
dgvCepages.AutoGenerateColumns = false;
dgvCepages.DataSource = Reg.Prod.dtCepages;
dgvCepages.EditMode = DataGridViewEditMode.EditOnEnter;
cbxCepage.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
|
|
|
|
|
Hi,
i'm confused. If String is a reference type, why b does not change if a is modified, since b is just referring to a?
E.g.
String a = "Hello";
String b = a;
a = a.ToUpper();
Console.WriteLine(a);
Console.WriteLine(b);
> HELLO
> hello
Thanks
|
|
|
|
|
uus831 wrote: If String is a reference type, why b does not change if a is modified, since b is just referring to a?
You're not passing a reference to a string, but invoking a method on such a reference, and putting the result back in a . The documentation[^] says that this method returns a copy of the string;
' Returns a copy of this String converted to uppercase. The method ToUpper is executed, and it returns a reference to a new string. This pointer is then stored in your A-variable, not touching B.
I are Troll
|
|
|
|
|
Yes, Microsoft purposely made the String type act like a value type, even though it is really a reference type; it is immutable. Any operations on a String produce a copy, or a partial copy, of the String; the underlying string data cannot be directly changed. This is why you have to do a = a.ToUpper(); in the first place, rather than the ToUpper() method directly changing the String . This is also the reason that the StringBuilder class exists, because it allows string manipulation without the extra copying, and thus has better performance in situations where lots of string manipulation will be happening.
|
|
|
|
|
Because it's not a.ToUpper() but a = a.ToUpper() - ToUpper doesn't change anything, it returns a new string.
If you change the actual string (yes you can, unsafe code), b should probably change
|
|
|
|
|
Ok, i understand. Thanks.
|
|
|
|