Coffeehouse Thread

12 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

number of characters in textbox (C#)

Back to Forum: Coffeehouse
  • User profile image
    rezaElc87

    hi- every one.

    I'm having a problem with a program I'm trying to write in windows forms. I want to write a program to convert binary numbers (1Byte) to hexadecimal.

    the program works like this:

    user inserts a binary number in a textbox and the result will be shown in another textbox.

    I know the algorithm and everything. there is just one problem:

    since the number must be binary I don't want the keyboard type in any other numbers ( other than 0 and 1) or other characters. I want to disable them in my application. how can I do that?

    best regard

  • User profile image
    figuerres

    well you can do the binary w/o a text box, like 8 check boxes... an option to think about.

    with textbox you need to look at the methods for the textbox controll.

    like KeyPress , KeyDown , KeyUp and others ....

    you can filter the input with them and count the length as they type.

    have you never done windows forms ?

     

  • User profile image
    kettch

    This smells like a homework assignment. I would recommend a simple search on MSDN. For me the top result was a great example of how to do what you are after.

  • User profile image
    magicalclick

    1) something like .length , I don't remember the exact method, but, just go through each items in the dropdown menu using Visual Studio. The dropdown menu should show up when you type <textbox followed by space.

    2) You can add a new onChanged event handler and remove the un wanted characters. Or onKeyPress if onChanged doesn't trigger as early as you like.

    Leaving WM on 5/2018 if no apps, no dedicated billboards where I drive, no Store name.
    Last modified
  • User profile image
    PeterF

    Indeed your question reeks like homework Wink

    Have a look at http://www.codeproject.com/Articles/15239/Validation-in-Windows-Presentation-Foundation

    And next time please use a search engine, you might start giving the impression that students nowadays are even too lazy to look for examples Tongue Out

  • User profile image
    JohnAskew

    @PeterF: +1

    The 'Tech Off' forum is the correct place for this type of post.

    Thanks.

  • User profile image
    MasterPi
  • User profile image
    MasterPi

    , kettch wrote

    This smells like a homework assignment.

    How is this question a homework assignment? It's most likely part of a homework assignment where the solution involves writing an easy to use interface for doing the conversion, and that the conversion is the main part of the assignment (which isn't what he's asking).

    If the input validation were the main part of the assignment, then it's a really lame assignment.

  • User profile image
    JohnAskew

    @MasterPie: I think the point is that the answers are easy to find if one were to start with a search engine instead of a forum.

    MaskedTextBox is a nightmare waiting for you, imho...

    I prefer utilizing the event model around a textbox. WPF element binding is a good idea here too.

  • User profile image
    rezaElc87

    @rezaElc87: hi- thank you for answering. all of you!

    this is not a homework assignment. it's not even a part of that too.

    it is a very small part of a big robotic project that is under development. in order to program a micro-controller I need to convert all these numbers every time and I thought it would be nice if I have the application on my computer. The one I create myself. I have worked with C and java. I'm just not so familiar with C#. but I think I like this language.

    and two minutes after I posted this question, I found the answer for my first question. I can change it in the property window of TextBox (MaxLength property). And some of you mentioned this.

    anyway, thanks for your time and answers

  • User profile image
    rezaElc87

    @magicalclick: this was the best reply I guess. I will try that. thank you. 

  • User profile image
    kettch

    @rezaElc87: Have you tried just the Windows calculator? Granted, it's not a roll-your-own, but it'll give immediate results.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.