简体   繁体   中英

Changing the color of a text box depending on its content

I have in a VS2005 C++ Form application a table adapter and a Text box that displays data from a specific column. What I want to do is to have its color changed on whether the content is >0 or <0. I tried adding this:

if(this->CSumTextBox->TabIndex<0) 
{
    this->CSumTextBox->ForeColor = System::Drawing::Color::Red;
}

But it doesn't work... (I didn't really believe TabIndex was the correct function, but it seemed the only one close) Help please

Edit: CSum is a double. Here is the whole code for CsumTextBox:

// 
// CSumTextBox
// 
this->CSumTextBox->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
this->CSumTextBox->BackColor = System::Drawing::SystemColors::Window;
this->CSumTextBox->DataBindings->Add((gcnew System::Windows::Forms::Binding(L"Text", this->sumclosedpnlBindingSource, L"CSum", true)));
this->CSumTextBox->Location = System::Drawing::Point(214, 632);
this->CSumTextBox->Name = L"CSumTextBox";
this->CSumTextBox->Size = System::Drawing::Size(86, 20);
this->CSumTextBox->TabIndex = 7;

It is in the Form Header (the whole program is a GUI, so almost everything is in there...)

Edit: Maybe if I check the data binding source's Value it would work, but how can I do that? (Does a this->sumclosedpnlBindingSource->returnvalue(CSum) or something like that exist?)

What do you mean by this:

… have its color changed on whether the content is >) …

To get access to the content of the text box, use its Text property. To test for a numeric value, you need to convert it to an integer (or other number type) first:

int value = System::Int32::Parse(CSumTextBox->Text);

if (value < 0)
    CSumTextBox->ForeColor = System::Drawing::Color::Red;
int val = -1;
if(!Int32::TryParse(CSumTextBox->Text) || val != 0)
{
   CSumTextBox->ForeColor = System::Drawing::Color::Red;
}

This will check that the value in the textbox actually converts to an integer, and that the value is not 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM