从textBox获取文本并将其用作dword值 [英] Get text from a textBox and use it as a dword value

查看:89
本文介绍了从textBox获取文本并将其用作dword值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我的项目是Windows窗体应用程序(使用Microsoft Visual C ++ Studio 2010).
我创建了一个textBox.该文本框下面有3个单选按钮(分别名为Hex,Dec,Bin).我需要的是在用户选择通过单击单选按钮给出的选项之一之后,他输入一些数字(假设他选择Bin并输入11100).然后,他单击Dec,文本框中的数字将更改为28(十进制值11100).如果他单击十六进制",则数字将更改为1C(11100的十六进制).

现在是要点:假设用户选择十六进制并输入1C.在将文本作为字符串检索之后,我必须声明一个DWORD类型的变量,例如DWORD d;保留文本的十六进制值.我该怎么办,因为我不知道用户要输入什么号码.如果我知道他要输入1C,则可以这样声明DWORD d = 0x1C.

请帮助我!

任何帮助将不胜感激!

Hello everybody!

My project is windows form application (using Microsoft Visual C++ Studio 2010).
I''ve created a textBox. There are 3 radio buttons below that textBox (named Hex, Dec, Bin). What I need is after the user chooses one of the option given by clicking on a radio button, he enters some number (let''s say he chooses Bin and enters 11100). Then he clicks on Dec the number in textBox will be changed to 28 (the decimal value of 11100). If he clicks on Hex then the number will be changed to 1C (the hexcimal of 11100).

Now is the point: let''s assume that the user chooses Hex and enters 1C. After I retrieve that text as a string, I have to declare a variable of type DWORD like DWORD d; that holds the hexcimal value of the text. How could I do that because I don''t know what number the user is going to enter. If I knew that he is going to enter 1C, then I could declare like this DWORD d = 0x1C.

Please help me with this!

Any help would be appreciated!

Thank in advance!

推荐答案

您遇到了问题,因为该概念从根本上讲是错误的.声明为 DWORD的变量不能为十进制或十六进制;它是二进制的.只有代表数字的字符串可以.另外,变量的声明是编译时的事情,所以您不能声明或初始化一个或另一个来响应某些动作.

您尝试使之复杂是因为您不了解简单的事物.您只需要将一些字符串解析为仅一种将要使用的类型.使用System::UInt32::ParseSystem::UInt32::TryParse.允许用户输入任何内容(您可以通过处理System.Windows.Forms::Control::KeyPress来过滤掉不需要的字符,并仅在需要使用该值时将文本解析为uint.

—SA
You have a problem because the concept is wrong in the very basics. The variable declared as DWORD cannot be decimal, or hexadecimal; it''s binary. Only the string representing numbers can be. Also, declaration of variable is the compile-time thing, so you cannot declare or even initialize one or another in response to the some action.

You are trying to make is complex because you don''t understand simple things. You simply need to parse some string into only one type you are going to use. Use System::UInt32::Parse or System::UInt32::TryParse. Allow the user to enter anything (you can filter out unwanted characters though handling System.Windows.Forms::Control::KeyPress and parse the text to uint only when you need to use the value.

—SA


您必须编写解析器,该解析器根据实际的数字基数选择将文本转换为数字,并编写函数以根据单击的数字基按钮.

要将文本转换为数字,请查看C函数strol(),您可以在其中指定数字的基数.

要使用十进制和十六进制数字创建文本,请使用C样式printf()函数的格式规范.创建具有二进制表示形式的文本字符串必须由您自己完成(但这非常简单).
You must write parsers that convert the text to a number according to the actual number base selection and functions that create a text string representing the number according to the clicked number base button.

To convert text to numbers, have a look at the C function strol() where you can specify the number''s base.

To create text from decimal and hex numbers, use the format specifications of C style printf() functions. Creating a text string with binary representation must be done by yourself (but it''s very simple).


这篇关于从textBox获取文本并将其用作dword值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆