Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. datetimepicker format for entire application!! [modified]

datetimepicker format for entire application!! [modified]

Scheduled Pinned Locked Moved C#
regex
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hussam Fattahi
    wrote on last edited by
    #1

    after adding a key in app.config for format i tried this, in the Main method

    CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
    culture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy";
    culture.DateTimeFormat.LongTimePattern = "";
    Thread.CurrentThread.CurrentCulture = culture;

    i was able to change pattern for short date, but what i want is that the datetimepicker format's property defaults to what i set in config file. like if i set the datetime format in config file to Long the rule will applay to entire application. do i have to check and change manually for each control. thanks

    modified on Sunday, December 27, 2009 2:46 AM

    realJSOPR 1 Reply Last reply
    0
    • H Hussam Fattahi

      after adding a key in app.config for format i tried this, in the Main method

      CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
      culture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy";
      culture.DateTimeFormat.LongTimePattern = "";
      Thread.CurrentThread.CurrentCulture = culture;

      i was able to change pattern for short date, but what i want is that the datetimepicker format's property defaults to what i set in config file. like if i set the datetime format in config file to Long the rule will applay to entire application. do i have to check and change manually for each control. thanks

      modified on Sunday, December 27, 2009 2:46 AM

      realJSOPR Offline
      realJSOPR Offline
      realJSOP
      wrote on last edited by
      #2

      You could: 0) You could override the DateTime class with your own 1) You could setup a static method in your application that does it for you. 2) You could simply specify a string constant and use it when you call DateTime.ToString() 3) You could setup the culture object you've already got there as static, and the rest of the app could use it. (You would have to initialize it from the config file via a static method as well. I would probably go with #3 myself.

      .45 ACP - because shooting twice is just silly
      -----
      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
      -----
      "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

      L H L 3 Replies Last reply
      0
      • realJSOPR realJSOP

        You could: 0) You could override the DateTime class with your own 1) You could setup a static method in your application that does it for you. 2) You could simply specify a string constant and use it when you call DateTime.ToString() 3) You could setup the culture object you've already got there as static, and the rest of the app could use it. (You would have to initialize it from the config file via a static method as well. I would probably go with #3 myself.

        .45 ACP - because shooting twice is just silly
        -----
        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        #3 is fine #0 isn't, as DateTime is sealed. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        Merry Christmas and a Happy New Year to all.


        1 Reply Last reply
        0
        • realJSOPR realJSOP

          You could: 0) You could override the DateTime class with your own 1) You could setup a static method in your application that does it for you. 2) You could simply specify a string constant and use it when you call DateTime.ToString() 3) You could setup the culture object you've already got there as static, and the rest of the app could use it. (You would have to initialize it from the config file via a static method as well. I would probably go with #3 myself.

          .45 ACP - because shooting twice is just silly
          -----
          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

          H Offline
          H Offline
          Hussam Fattahi
          wrote on last edited by
          #4

          thanks for reply first: as i mentioned in my question, i was able to change the pattern of the format, not the format property for option 2: it does not work for me, because i am actually showing the date in the datetimepicker not into textbox control, so i'm not using DateTime.ToString() for option 3: do you mean that i have to access the static cultureinfo object every time i need to to display the date in datetimepicker or do i have to change the format manually in each datetimepicker contro on my form based on the cultureInfi properties excuse my bad english thanks for the reply again

          realJSOPR 1 Reply Last reply
          0
          • H Hussam Fattahi

            thanks for reply first: as i mentioned in my question, i was able to change the pattern of the format, not the format property for option 2: it does not work for me, because i am actually showing the date in the datetimepicker not into textbox control, so i'm not using DateTime.ToString() for option 3: do you mean that i have to access the static cultureinfo object every time i need to to display the date in datetimepicker or do i have to change the format manually in each datetimepicker contro on my form based on the cultureInfi properties excuse my bad english thanks for the reply again

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #5

            Hussam Fattahi wrote:

            for option 3: do you mean that i have to access the static cultureinfo object every time i need to to display the date in datetimepicker

            I don't know. I've never had to do what you're trying to do. Experiment with it. The benefit of creating a static culture object is that you don't have to recreate it every time you need it. It's always there, and it's always the same. I suspect that you can get away with setting the control when the form is constructed, and then forget about it.

            .45 ACP - because shooting twice is just silly
            -----
            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

            H 1 Reply Last reply
            0
            • realJSOPR realJSOP

              Hussam Fattahi wrote:

              for option 3: do you mean that i have to access the static cultureinfo object every time i need to to display the date in datetimepicker

              I don't know. I've never had to do what you're trying to do. Experiment with it. The benefit of creating a static culture object is that you don't have to recreate it every time you need it. It's always there, and it's always the same. I suspect that you can get away with setting the control when the form is constructed, and then forget about it.

              .45 ACP - because shooting twice is just silly
              -----
              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

              H Offline
              H Offline
              Hussam Fattahi
              wrote on last edited by
              #6

              John Simmons / outlaw programmer wrote:

              I suspect that you can get away with setting the control when the form is constructed, and then forget about it.

              yes, i can do that, but then i have to put it in each form in my application. i'm looking if there is a way to do it globaly do it in one place and this is it for the whole application thanks anyway

              realJSOPR 1 Reply Last reply
              0
              • H Hussam Fattahi

                John Simmons / outlaw programmer wrote:

                I suspect that you can get away with setting the control when the form is constructed, and then forget about it.

                yes, i can do that, but then i have to put it in each form in my application. i'm looking if there is a way to do it globaly do it in one place and this is it for the whole application thanks anyway

                realJSOPR Offline
                realJSOPR Offline
                realJSOP
                wrote on last edited by
                #7

                Well, you could derive your own datetime control from the .Net DateTimePicker class, and set it in the constructor, but then you'd have to remember to use YOUR datetimepicker control instead of .Net's version... If it were me, I would probably just take the easy way out and set the custom format on each control that needed it, and be done with it.

                .45 ACP - because shooting twice is just silly
                -----
                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                1 Reply Last reply
                0
                • realJSOPR realJSOP

                  You could: 0) You could override the DateTime class with your own 1) You could setup a static method in your application that does it for you. 2) You could simply specify a string constant and use it when you call DateTime.ToString() 3) You could setup the culture object you've already got there as static, and the rest of the app could use it. (You would have to initialize it from the config file via a static method as well. I would probably go with #3 myself.

                  .45 ACP - because shooting twice is just silly
                  -----
                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                  -----
                  "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Hmm, You could also create an Extension method if you are using .NET 3.5 or better.

                  H 1 Reply Last reply
                  0
                  • L Lost User

                    Hmm, You could also create an Extension method if you are using .NET 3.5 or better.

                    H Offline
                    H Offline
                    Hussam Fattahi
                    wrote on last edited by
                    #9

                    what do you mean by extension method?

                    L 1 Reply Last reply
                    0
                    • H Hussam Fattahi

                      what do you mean by extension method?

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      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

                      E 1 Reply Last reply
                      0
                      • L Lost User

                        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

                        E Offline
                        E Offline
                        elizas
                        wrote on last edited by
                        #11

                        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

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups