Locked Why Doesnt ComboBox have CharacterCasing?

  • Wednesday, April 15, 2009 5:31 AM
     
     

    I see textbox in C# having CharacterCasing that forces the entered Characters to be of upperCase. But why this doesnt be in ComboBox. Whats the difficulty by they left this property for comboboxes?

All Replies

  • Wednesday, April 15, 2009 3:08 PM
     
     
    Textbox is design for input or typing text, which means it allow you to format your text but combobox is design to to hold list of items. You can change your list to upper or lower before add it to combobox
    Dim st As String
    st = st.ToLower
    ComboBox1.Items.Add(st)
    'to lower
    st = st.ToUpper
    ComboBox1.Items.Add(st)
    'to upper


    kaymaf


    I hope this helps, if that is what you want, just mark it as answer so that we can move on
  • Wednesday, April 15, 2009 4:24 PM
     
     
    Thats pretty only if you see in that perspective.

    But you can also see a basic textbox in combobox which allows you to input values. Right?

    Whats the case with this?


    Guna
    Regards, Guna
  • Wednesday, April 15, 2009 5:38 PM
     
     
    may be you should try to change the text when you use keyPress event
    kaymaf
    I hope this helps, if that is what you want, just mark it as answer so that we can move on
  • Wednesday, April 15, 2009 7:04 PM
    Moderator
     
     Answered
    Hello.

    The reason why a a ComboBox---or a TextBox for that matter---does not have that property is because of pretty clever and good object design.  The TextBox is designed to display plain simple text strings just like MS Notepad.  A RichTextBox can be used to display formatted text much like MS Write or MS Word might do it.  The emphasis is on display.  That's all it does.

    One of the basic driving principles behind Object Oriented design is abstraction, abstraction, abstraction.  That means not letting one object become dependent upon changes in another object this it uses or is used in tandem with.  So the designers designed the string formatting methods into the string object itself, and designed the TextBox to display one of these "smart string" objects. 

    To do put the string formatting methods into the Form Control would mean that each and every Form Control would need to have these same---hopefully the same, typos permitting---formatting methods written out for them.  That's not good.  That's re-inventing the wheel everytime you build a car.  So they cleverly tucked the code into the String object itself, and defined a standard interface of methods and properties to work with strings.

    You will see this abstraction philosophy repeated throughout the .NET Framework.

    Hope this helps.

    Rudedog   =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
  • Thursday, April 16, 2009 5:16 AM
     
     

    Hello,
        It does help, but i have few more questions from this?

     

    1) But the textbox has a property which converts all entered characters to upper case which the combobox doesnt have. Can i assume this as an answer for this?

    Textbox is limited to text only but the Combobox is able to show other objects (images...) that plain text itself... right?

     

    2) You mean to say the textbox while characterCasing set to convert to upper or lower calls the String's methods and not convert the cases while KeyPress Events?

    Doesnt that affects performance?

     

    Guna


    Regards, Guna
  • Thursday, April 16, 2009 2:12 PM
    Moderator
     
     
    1)  As noted, the TextBox displays plain text, just like MS Notepad.  The ComboBox displays text, too.  But, it follows a slightyly different route to do it by using the ToString() method.  All .NET types have that method because they all unltimately inherit from System.Object.  The CombobBox displays a list of "Items".  It even has a property collection called "Items".  The type stored in this collection is System.Object.  When it comes time to display a "SelectedItem", the ComboBox uses the ToString() property to display a textual representation of the object.  It is a common practice for classes defined by the developer to "override" the ToString() method to fulfill this behavior.  Not overriding it, ToString() will simply return the TypeName.  The ComboBox takes advantage of an object behavior known as Polymorphism to do this.

    2)  YES!  That is exactly what the textbox does.  This is known in Object Oriented Programming as delegation.  Delegating the responsibility of some tasks to custom classes or the object itself.  Do not confuse my use of the term "delegate" with the keyword or the .NET Framework type Delegate.  However, my use of the word "delegate" in this context is an accurate description of how the .NET type got its' name.  The concept of Abstraction is driven by this principle of delegation.

    Design Guidelines for Class Libraries

    3)  Performance?  Not really.  A method by another name is just a method.  But, it helps do delegate tasks to other classes or the objects themselves when it comes time to write your source code.  You do not want a class or a code file filled with stuff that is so intertwined that it becomes to edit one part without modifying half of the rest.  That happens to a lot of folks, and they fail to see or understand why it occurs. 

    Rudedog   =8^D

    Mark the best replies as answers. "Fooling computers since 1971."
  • Thursday, April 16, 2009 2:18 PM
     
     
    For your question , In keyPress event, you can modified the each character enter to upper or lower but it doesnt work perfect. The only thing i believe you can use to modified your combobox is to use OWNERDRAWVARIABLE. what you have to is to set the DrawMode property to OWNERDRAWVARIABLE from normal. With this method , you should be able to implement your combobox with image, characterchasing and others. Just try to search for how to use OWNERDRAWVARIABLE in .net. I never used it with combobox but with Menu and Listbox in VS2003.
    kaymaf


    I hope this helps, if that is what you want, just mark it as answer so that we can move on
  • Thursday, April 16, 2009 3:49 PM
     
     

    OK.. Here's my next Q.

    1)If ComboBox's textbox shows all object's ToString() returnee as text,
    (i'm not aware of it before, i thought if an image is added, it shows the image itself)
    then they should have the CharacterCasing for the textbox (in combobox) right...

    It seems appropriate.

    2)Performance.
    Even though it's just a method, what is in this case

    Manipulating a whole string to convert all characters to upper case each time

    Converting the lastentered character alone to upper case.

    Which one is efficient?


    Hope It Works, Guna
  • Thursday, April 16, 2009 4:24 PM
    Moderator
     
     
    Forum rules and protocols suggest that you should start a new thread if you have a new question.  As a general, one topic per thread.  You could also give the Moderators a break by marking a reply as the answer.  I like Kay,af's reply about keypress.  The Moderators have the task of reviewing each and every thread that is not marked as having an answer.

    Delegation.  The ComboBox and many other Forms Controls display objects of type String.  String class has numerous static methods, too.  The only built-in stuff operates across the entire string, ToUpper and ToLower. 

    Do not get hung up on writing "perfect" code.  Make it work.  Make it all work together.  Then, make it better.
    Mark the best replies as answers. "Fooling computers since 1971."
  • Thursday, April 16, 2009 4:50 PM
     
     
    But i Dint get my answer yet, i Feel.

    I have worked around for that issue in my code. What all i need to know is WHY? WHY? WHY? they havent implemented CharacterCasing for a control whose intention basically is to show Text.?

    Am I clear?

    Guna
    Hope It Works, Guna
  • Thursday, April 16, 2009 5:05 PM
    Moderator
     
     
    "To do put the string formatting methods into the Form Control would mean that each and every Form Control would need to have these same---hopefully the same, typos permitting---formatting methods written out for them.  That's not good.  That's re-inventing the wheel everytime you build a car.  So they cleverly tucked the code into the String object itself, and defined a standard interface of methods and properties to work with strings."

    This is the direct answer.  They wanted to write the code once and have it one place, inside of the String object itself, instead of a hundred places.  If you have a bug or want to add a new feature you go to one spot, not hundreds of spots.
    Mark the best replies as answers. "Fooling computers since 1971."
  • Thursday, April 16, 2009 5:12 PM
    Moderator
     
     
    They also do it that way---delegate the formatting to another class or the class itself--- because it is consistent with good OOP Design Practices.  Here's some food for thought that I frequently post.


    Treat class names as nouns, and method names as verbs. When you need to write code, you start of with a description of what it needs to do. Sometimes it is formally written out in plain English by you or your customer. That description is an excellent source for what classes you need and what methods you should deploy where. Use this acid test on your class and method names.

    The ClassName, MethodName itself. <---- Apply this to classes that you use to create object instances.

    You should be able to create a sensible sentence out of it. You may have to add stuff like "for", "to" or even the letter s. The result may even sound like broken Englilsh, but the meaning should still be clear. You can even apply this rule to properties....

    The ClassName, GetPropertyName itself.

    The ClassName, SetPropertyName itself.

    ...because properties get inlined into class member methods by the compiler. You can turn it around to determine how to name and design static methods and classes.

    The MethodName, ClassName instance. <---- Apply this to classes that you use to define static methods.

    Apply these tests to your code. Post back with what it shows to you. Hope this helps.


    Mark the best replies as answers. "Fooling computers since 1971."
  • Thursday, April 16, 2009 5:16 PM
    Moderator
     
     
    As I already noted, a ComboBox does not display text, nor is intended to display text, like a Textbox. 
    A ComboBox is designed to display a dropdown list of objects, not a dropdown list of lines of text.  
    A Textbox is designed to display unformatted string data.
    Mark the best replies as answers. "Fooling computers since 1971."
  • Thursday, April 16, 2009 5:37 PM
     
     
    Rudedog, it seems to me that you are not gasping the OP question completely, hence giving a different answer to what he expects. Mainly I think it is because you didn’t realize that TextBox actually does have a CharacterCasing Property, slightly contradicting your whole explanation. He’s question is why TextBox has it and Combox doesn’t.

    Now, the question still remains, but unfortunately I don’t think you’ll get a concrete answer unless he who designed the TextBox answers you. I for one can’t tell you the real reason behind this, but if I have to guess, I’d say it is because TextBox is a ReadWrite control while the ComboBox is more of a static control. Yes, I know that ComboBox still allows user to type in text, and that actually makes it more of a hybrid. Still, I see it as static since most of the time the user will select an already existing item. The textbox on the other hand, is thought precisely for text input/display. Those are it’s only purposes. That’s why on this subject it allows more functionalities, not only CharacterCasing, also PasswordChar, Multiline, AcceptsTab and a bunch more.
    /* No comments */
  • Thursday, April 16, 2009 5:42 PM
     
     
    Your question is getting beyond programming language, is now about VS design environment, so you should close this thread by mark any post that satisfied you as answer. Then, you can open another thread in Visual Studio Team System forum for suggestion if is possible to add characterchasing property to combobox in the version of visual studio 11 because  version 10 will soon be out. Try to post new thread at  http://social.msdn.microsoft.com/Forums/en-US/vstsarch/threads 

    kaymaf
    I hope this helps, if that is what you want, just mark it as answer so that we can move on
  • Thursday, April 16, 2009 6:22 PM
    Moderator
     
     
    Thanks, Fernando.
    Mark the best replies as answers. "Fooling computers since 1971."
  • Friday, April 17, 2009 6:04 AM
     
     
    Sorry, Came back right now...

    Fernando's answer gives me a thought... But i still want to know more on this...

    Should i have to repost it as kaymaf said?
    Hope It Works, Guna
  • Wednesday, August 08, 2012 4:16 PM
     
      Has Code

    I know this is old, but I was searching in regards to this as well. I found my own solution so I'm sharing it here for any others who're also searching for this. Just replace .ToUpper() with whatever formatting you'd prefer. The .Select and .SelectionStart makes sure the user's cursor (caret) position stays the same after the string formatting.

    comboBox1.TextUpdate += new EventHandler(comboBox1_TextUpdate);
    
    private void comboBox1_TextUpdate(object sender, EventArgs e)
    {
        int i = comboBox1.SelectionStart;
        comboBox1.Text = comboBox1.Text.ToUpper();
        comboBox1.Select(i, 0);
    }

    I hope that helps. :)