C#/.NET/GUI プログラミングは比較的新しいですが、ここに行きます。現在、WinForms を使用して職場でビジネス アプリケーションを作成しています。約 5 つのテキスト ボックスと 1 つのコンボ ボックスが空であるかどうかを確認し、空である場合はユーザーに伝え、そのコントロールにフォーカスを設定します。これを行うにはどうすればよいですか?
各コントロールをチェックする if ステートメントを使用することもできます。
if (usernameField IsNullOrEmpty) then:
setFocus(UsernameField);
return;
if (addressField IsNullOrEmpty) then:
setFocus(addressField);
return;
continue with rest of application as normal...
または、例外を除いてこれを行うこともできます。
try {
if (usernameField IsNullOrEmpty) then:
throw new Exception(usernameField);
if (addressField IsNullOrEmpty) then:
throw new Exception(addressField);
} catch (Exception e) {
setFocus(ControlField) // encapsulate in exception some way?
}
または、コードの重複を防ぐために、関数を書くだけです:
try {
checkField(usernameField);
checkField(addressField);
} catch (Exception e) {
setFocus(ControlField) // encapsulate in exception some way?
}
void checkField(control ctrl) {
if (ctrl IsEmptyOrNull)
throw new Exception(ctrl);
}
GUIプログラミングに比較的慣れていないため、空のテキストフィールドは例外に値しますか、それとも通常のプログラムフローと見なされますか?