 |
Club Builder Foro de C++Builder en español
|
| Ver tema anterior :: Ver tema siguiente |
| Autor |
Mensaje |
Felip Master

Registrado: 09 Jun 2004Mensajes: 200
|
Publicado: Jue 28 Oct, 2021 5:10 am Asunto: InputBox: solo números. Delphi a BCB |
|
|
Encontré este código para que un inputbox acepte solo números, pero no pude hacerlo funcionar en C ++ Builder:
| Código: |
const
InputBoxNumberMessage = WM_USER + 666;// a custom message
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure InputBoxSetOnlyNumbers(var Msg: TMessage); message InputBoxNumberMessage;
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
InputString: string;
begin
PostMessage(Handle, InputBoxNumberMessage, 0, 0);
InputString := InputBox('Input', 'Enter a number', '');
ShowMessage(InputString);
end;
procedure TForm1.InputBoxSetOnlyNumbers(var Msg: TMessage);
var
hActiveForm : HWND;
hEdit : HWND;
dwLong : Longint;
begin
hActiveForm := Screen.ActiveForm.Handle;
if (hActiveForm <> 0) then
begin
hEdit := FindWindowEx(hActiveForm, 0, 'TEdit', nil);//determine the handle of the TEdit
dwLong := GetWindowLong(hEdit, GWL_STYLE);//get the current style of the control
SetWindowLong(hEdit, GWL_STYLE, dwLong or ES_NUMBER)//set the new style
end;
end;
|
En la traducción que hice, el inputbox sigue aceptando otros valores.
fuente:
https://stackoverflow.com/questions/5678690/delphi-7-force-inputbox-to-integer-only
Gracias por cualquier ayuda |
|
| Volver arriba |
|
 |
Cesar C. Salazar Gonzalez Master


Registrado: 01 Oct 2002Mensajes: 2155Ubicación: San Cristóbal de Las Casas, Chiapas
|
Publicado: Jue 28 Oct, 2021 8:05 pm Asunto: |
|
|
En dado caso puedes usar un InputQuery() (a partir de C++Builder XE2) , tal y como lo dice la ayuda de Embarcadero.
-->Enlace
Permite definir lo que deseas que se ingrese, string, double o integer _________________
 |
|
| Volver arriba |
|
 |
Felip Master

Registrado: 09 Jun 2004Mensajes: 200
|
Publicado: Jue 28 Oct, 2021 11:37 pm Asunto: |
|
|
Muchas gracias Cesar. Busqué en Google pero no encontré ningún ejemplo. Ni siquiera sabía por dónde empezar. De todos modos, logré traducir el código satisfactoriamente. Transcribo para quien lo necesite:
| Código: |
// unit.h
...
const UINT InputBoxNumberMessage = WM_USER + 123;
class TForm1 : public TForm
{
...
private: // User declarations
void __fastcall InputBoxSetOnlyNumbers( TMessage & Msg );
BEGIN_MESSAGE_MAP
VCL_MESSAGE_HANDLER(InputBoxNumberMessage, TMessage, InputBoxSetOnlyNumbers)
END_MESSAGE_MAP(TForm)
...
|
...
| Código: |
// unit.cpp
...
void __fastcall TForm1::InputBoxSetOnlyNumbers( TMessage &Msg )
{
HWND hActiveForm;
HWND hEdit;
//signed int dwLong;
hActiveForm = Screen->ActiveForm->Handle;
if ( (hActiveForm != 0) )
{
hEdit = FindWindowEx(hActiveForm, 0, L"TEdit", NULL);//determine the handle of the TEdit
//dwLong = GetWindowLong(hEdit, GWL_STYLE);//get the current style of the control
SetWindowLong(hEdit, GWL_STYLE,
(GetWindowLong(hEdit, GWL_STYLE)) | ES_NUMBER); //set the new style
}
}
//==============
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString InputString ;
PostMessage(Handle, InputBoxNumberMessage, 0, 0);
InputString = InputBox("Input", "Enter a number", "");
ShowMessage(InputString);
}
...
|
|
|
| Volver arriba |
|
 |
Felip Master

Registrado: 09 Jun 2004Mensajes: 200
|
Publicado: Vie 29 Oct, 2021 1:02 am Asunto: |
|
|
Noté que también es posible limitar la cantidad de dígitos en el InputBox:
| Código: |
...
if ( (hActiveForm != 0) )
{
hEdit = FindWindowEx(hActiveForm, 0, L"TEdit", NULL);
SendMessage(hEdit, EM_LIMITTEXT, 2, 0); // limita la cantidad de dígitos: 2
...
|
|
|
| Volver arriba |
|
 |
Cesar C. Salazar Gonzalez Master


Registrado: 01 Oct 2002Mensajes: 2155Ubicación: San Cristóbal de Las Casas, Chiapas
|
Publicado: Vie 29 Oct, 2021 2:47 am Asunto: |
|
|
Interesante solución, enhorabuena!! _________________
 |
|
| Volver arriba |
|
 |
|
|
No puedes publicar temas nuevos en este foro No puedes responder a temas en este foro No puedes editar tus mensajes en este foro No puedes borrar tus mensajes en este foro No puedes votar en las encuestas de este foro
|
© 2002–2026 Club Builder · Funciona con phpBB.
|