Edit_Digit_Virg


O exemplo a seguir leva um Edit (que, via teclado, aceita apenas valores numéricos e backspace) e dois RadioButtons no Form. Quando o usuário dá um clique no RadioButton1, o programa providencia a colocação de uma vírgula a duas casas decimais do final da string numérica. O evento OnKeyPress do Edit possui um código para providenciar o acerto constante da vírgula a duas casas decimais, sempre que houver a inserção ou exclusão de dados.


nota: esse código é apenas exemplificativo, carecendo de alguns acertos para um uso mais profissional.






//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
      // vetor para receber a string
      char buffer[100];
 
      /*variável para trabalhar na alteração da posição da vírgula*/
      char buf; 

      /*booleana para desabilitar a inclusão de uma segunda vírgula*/
      bool virgula = true;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::RadioButton1Click(TObject *Sender)
{
if(virgula == true && Edit1->Text != "")
{
 char buffer[100];
 strcpy (buffer, Edit1->Text.c_str());  // converte Text de AnsiString para char*
 buffer[(Edit1->Text.Length()) + 1] = buffer[(Edit1->Text.Length())];
 buffer[(Edit1->Text.Length())]     = buffer[(Edit1->Text.Length())- 1];
 buffer[(Edit1->Text.Length()) - 1] = buffer[(Edit1->Text.Length())- 2];

/**************************************************************
Para colocar a vírgula a quatro casas decimais do final,
acrescente as linhas de código que estão na forma de comentários
e substitua os valores na forma de comentários no final de
algumas das linhas de código abaixo: os valores -4, -5 ou -6.
***************************************************************/

// buffer[((int)Edit1->Text.Length()) - 2] = buffer[((int)Edit1->Text.Length())- 3];
// buffer[((int)Edit1->Text.Length()) - 3] = buffer[((int)Edit1->Text.Length())- 4];

 buffer[(Edit1->Text.Length()) - 2] = ','; // - 4
 Edit1->Text = buffer;
 virgula = false;
}
 else
 return;

 }
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1MouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
// coloca o cursor no final do Edit
Edit1->SelStart = (Edit1->Text.Length());
}
//---------------------------------------------------------------------------



void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key)
{

/***  O código abaixo faz que o edit aceite
apenas valores numéricos através do teclado */

// (backspace == 8)
if(Key != 8)
{
// (valor 1 == 48) .. (valor 9 == 57)
if((Key < 48) || (Key > 57))
{
Key = 0;
}
}

/*O código abaixo mantém a vírgula a duas casas
decimais (...,00) do final da string *********/
if(RadioButton1->Checked == true)
{
        if (Key != VK_BACK) // Key != backspace
        {

                saida:
                // converte Text de AnsiString para char*
                strcpy (buffer, Edit1->Text.c_str());
                buf = buffer[(Edit1->Text.Length()) - 2]; //-4
                buffer[(Edit1->Text.Length()) - 3] = buf; //-5
                buffer[(Edit1->Text.Length()) - 2] = ','; //-4
                Edit1->Text = buffer;

                // coloca o cursor no final do Edit
                Edit1->SelStart = (Edit1->Text.Length());
        }


        if (Key == VK_BACK)
        {
           // converte Text de AnsiString para char*
           strcpy (buffer, Edit1->Text.c_str());  
           buf = buffer[(Edit1->Text.Length()) - 4]; // -6
           buffer[(Edit1->Text.Length()) - 3] = buf;      // -5
           buffer[(Edit1->Text.Length()) - 4] = ',';      // -6
           Edit1->Text = buffer;

           // coloca o cursor no final do Edit
           Edit1->SelStart = (Edit1->Text.Length()); 
        }

}

}
//---------------------------------------------------------------------------

HOME || MAPA DO SITE || CURSOS || TUTORIAIS || LINKS || FORUM || CONTATO