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?
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 ?
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.
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.
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.
@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.
@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
@magicalclick: this was the best reply I guess. I will try that. thank you.
@rezaElc87: Have you tried just the Windows calculator? Granted, it's not a roll-your-own, but it'll give immediate results.
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.