How to find a comma inside a Textbox

Asked

Viewed 55 times

1

I have a Text Box in which can only contain a comma, so I thought to make the program detect, when the user uses the comma, if there is already one in that particular Text Box.

Here’s an example of the code I want to get:

if(*Verificar se já existe uma vírgula no textBox1) { //não fazer nada } else { this->textBox1->Text += ","; }

  • Voce is using c++ or winforms?

  • 1

    Post something you’ve already developed, some code, examples. Facilitates interpretation and possible answers

2 answers

1


I ended up using the marshal, here’s the resolution:

char* Valor = (char*)(void*)Marshal::StringToHGlobalAnsi(textBox1->Text);
ContadorDeVirgulas = strchr(Valor, ',');
if(ContadorDeVirgulas > 0) {
}
else {
    this->textBox1->Text += ",";
}

0

I don’t know if this->textBox1->Text is a Std::string but in case it’s just walking around looking for commas:

int contadorDeVirgulas = 0;
for(int i = 0; i < str.size(); ++i) 
{
    if (str[i] == ",")
        contadorDeVirgulas++;
}

Then just ask

if(contadorDeVirgulas > 0) 
{
    //não fazer nada
}
else 
{
    this->textBox1->Text += ",";
}

if you need to check in several places you can use this is within a function or lambda (depending on the case).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.