views:

238

answers:

2

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?)

+3  A: 

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;
Konrad Rudolph
sorry meant >0 (typo...) I tried this, I also tried a small variation (double value = System::Double::Parse(this->CSumTextBox->Text); if (value < 0) {this->CSumTextBox->ForeColor = System::Drawing::Color::Red;}) but it gives me a FormatExeption
Ant
Well, the `FormatException` clearly says that your `TextBox` doesn't contain a valid floating-point number.
Konrad Rudolph
If that's the case what could I do?
Ant
A: 
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.

mos
You forgot a `%^val` as the second parameter to `TryParse`.
Konrad Rudolph
… what I meant was: a tracking reference … whatever the syntax for it (can't remember).
Konrad Rudolph
I'm sure you're right, but I never write managed C++ so I was just winging it. :)
mos