0

テキストを定義し、後でそれを文字列などとしてではなく、関数の一部として使用するが、プログラムの途中で再定義できるオプションはありますか(定義はプリプロセッサでは行われませんが、ランタイム)?たとえば、C++ Windows フォームに次のコードがあります。

private: System::Void ps1_GotFocus(System::Object^  sender, System::EventArgs^  e)
{
    if(this->ps1->Text == L"/ Your text here /") this->ps1->Text = L"";
    this->ps1->ForeColor = System::Drawing::Color::FromName( "Black" );
}

private: System::Void ps2_GotFocus(System::Object^  sender, System::EventArgs^  e)
{
    if(this->ps1->Text == L"/ Your text here /") this->ps1->Text = L"";
    this->ps2->ForeColor = System::Drawing::Color::FromName( "Black" );
}

whereps1TextBoxesps2は、私はそれを使用して灰色の 'Your text here' 文字列を表示し、TextBox をクリックすると入力の準備ができて (TB の場合)、テキストをクリアして入力をblackにします。私はそのような 9 つの TextBoxes を持っていることを念頭に置いて、これをすべてより少ないコードで作成することは可能ですか? その ps を使用するすべての外部のグローバルメソッドを使用して同じコードを試しましたが、ご存知のように、s はプリプロセッサで実行され、プログラムが開始される前に最後の定義 ( ) が定義されます。GotFocus#define ps ps1ps_GetFocus()#defineps ps9

実行時にスコープ外のテキストを定義する方法はありますか?

4

1 に答える 1

1

ps_GotFocusすべてのテキスト ボックスに共通の関数を用意し、代わりに (sender最初に適切な型にキャストする必要があります。さまざまなオブジェクト。^dynamic_castps

次のようなもの:

private: System::Void ps_GotFocus(System::Object^  sender, System::EventArgs^  e)
{
    TypeForYourTextBox^ the_sender = dynamic_cast<TypeForYourTextBox^>(sender);
    // I'm unsure about the previous line but you get the idea
    // You may also want to check that the cast succeeded, ie. the_sender is not null
    if (the_sender->Text == L"/ Your text here /") the_sender->Text = L"";
    the_sender->ForeColor = System::Drawing::Color::FromName("Black");
}
于 2013-04-23T21:43:57.277 に答える