Club Builder - Foros de discusión Club Builder
Foro de C++Builder en español
 
 FAQFAQ   BuscarBuscar   MiembrosMiembros   Grupos de UsuariosGrupos de Usuarios   RegistrarseRegistrarse 
 PerfilPerfil   Entra para ver tus mensajes privadosEntra para ver tus mensajes privados   EntrarEntrar 

InputBox: solo números. Delphi a BCB

 
Publicar nuevo tema   Responder al tema    Foros de discusión -> Consultas
Ver tema anterior :: Ver tema siguiente  
Autor Mensaje
Mensaje    Asunto: InputBox: solo números. Delphi a BCB Responder citando

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
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

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
_________________
Cool
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email MSN Messenger
Mensaje    Asunto: Responder citando

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
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

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
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

Interesante solución, enhorabuena!!
_________________
Cool
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email MSN Messenger
Mostrar mensajes de anteriores:   
Cambiar a:  
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


Hosting WordPress Indedmedia desde 3,99 €
© 2002–2026 Club Builder · Funciona con phpBB.