ich habe eine Klasse erstellt, die eine eigene Messagebox darstellen soll (ich weiß, dieses Thema wird 100.000mal diskutiert in Foren - brauche dennoch noch mal Hilfe).
Meine eigene Msgbox funktioniert, jedoch glaube ich habe ich von der C#-Struktur her ein ganz schönes Durcheinander erzeugt (bin neu in C#) - und bitte Euch um Hilfe und Tipps, Ordnung in meine Klassen zu bekommen.
Hier der Code: (Entschuldigung, jetzt kommt viel Code...)
-------------- Klasse MyMessagebox -----------------------------------
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332:
| public partial class MyMessagebox : Falcon.HForm { static MyMessagebox newMessageBox; static string Button_id; static string textYes = "Yes"; static string textNo = "No"; static string textOK = "OK"; static string textCancel = "Cancel"; public enum MSGBOX_KEYS { OK_ONLY = 0, OK_CANCEL = 1, YES_NO = 2, YES_NO_CANCEL = 3, NO_KEYS = 4 };
UInt16 MsgboxLength = 252; UInt16 MsgboxHeight = 146; UInt16 MsgboxPosX = 10; UInt16 MsgboxPosY = 166; UInt16 MsgboxPosXQuer = 128; UInt16 MsgboxPosYQuer = 2; UInt16 MsgboxTextLength = 202; UInt16 MsgboxTextHeitht = 75; string MsgboxBackgrountImg = "\\FlashDisk\\Falcon\\Resources\\Msgbox_Logo_gelb146.png"; UInt16 MsgBoxButtonOKPosX = 40; UInt16 MsgBoxButtonOKPosY = 98; UInt16 MsgBoxButtonCancelPosX = 131; UInt16 MsgBoxButtonCancelPosY = 98;
public delegate void CloseMyMessageboxDialogEvent(); public event CloseMyMessageboxDialogEvent CloseMyMessageboxDialog;
public MyMessagebox() { InitializeComponent();
this.PropertyChanged += new PropertyChangedEventHandler(MyMessagebox_PropertyChanged); }
public static string ShowBox(string txtMessage, string txtTitle, MyMessagebox.MSGBOX_KEYS MsgboxKeys) { newMessageBox = new MyMessagebox(); newMessageBox.UpdateControls(txtTitle, txtMessage, MsgboxKeys); newMessageBox.ShowDialog(); return Button_id; }
private void MsgBox_Close() { newMessageBox.Dispose(); }
private void MsgBoxButtonOK_Click(object sender, EventArgs e) { Button_id = "1"; MsgBox_Close(); }
private void MsgBoxButtonCancel_Click(object sender, EventArgs e) { Button_id = "2"; MsgBox_Close(); }
private void MyMessagebox_PropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == "Hochkant") { this.Invoke((System.Threading.ThreadStart)delegate { if (Hochkant) { newMessageBox.ClientSize = new System.Drawing.Size(newMessageBox.MsgboxLength, newMessageBox.MsgboxHeight); newMessageBox.Location = new System.Drawing.Point(newMessageBox.MsgboxPosX, newMessageBox.MsgboxPosY);
tMsgboxHeader.Location = new System.Drawing.Point(131, 3); tMsgboxText.Location = new System.Drawing.Point(24, 49); MsgBoxButtonOK.Location = new System.Drawing.Point(newMessageBox.MsgBoxButtonOKPosX, newMessageBox.MsgBoxButtonOKPosY); MsgBoxButtonCancel.Location = new System.Drawing.Point(newMessageBox.MsgBoxButtonCancelPosX, newMessageBox.MsgBoxButtonCancelPosY); } else { newMessageBox.ClientSize = new System.Drawing.Size(newMessageBox.MsgboxLength, newMessageBox.MsgboxHeight); newMessageBox.Location = new System.Drawing.Point(114, newMessageBox.MsgboxPosYQuer);
tMsgboxHeader.Location = new System.Drawing.Point(131, 3); tMsgboxText.Location = new System.Drawing.Point(24, 49); MsgBoxButtonOK.Location = new System.Drawing.Point(newMessageBox.MsgBoxButtonOKPosX, newMessageBox.MsgBoxButtonOKPosY); MsgBoxButtonCancel.Location = new System.Drawing.Point(newMessageBox.MsgBoxButtonCancelPosX, newMessageBox.MsgBoxButtonCancelPosY); }
}); }
if (e.PropertyName == "Language") { this.Invoke((System.Threading.ThreadStart)delegate { MsgBoxButtonOK.Text = rm.GetString("MyMessagebox_OK", ci); MsgBoxButtonCancel.Text = rm.GetString("MyMessagebox_Cancel", ci);
textYes = rm.GetString("MyMessagebox_Yes", ci); textNo = rm.GetString("MyMessagebox_No", ci); textOK = rm.GetString("MyMessagebox_OK", ci); textCancel = rm.GetString("MyMessagebox_Cancel", ci);
}); }
}
public void UpdateControls(string MsgboxHeader, string MsgboxText, MyMessagebox.MSGBOX_KEYS MsgboxKeys) {
UInt16 ButtonHeight = 40; System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MyMessagebox));
tMsgboxHeader.Text = MsgboxHeader;
tMsgboxText.Text = MsgboxText;
if (MsgboxKeys == MSGBOX_KEYS.NO_KEYS) ButtonHeight = 0; if (MsgboxText.Length < 28) { MsgboxHeight = 146; MsgboxPosX = 10; MsgboxPosXQuer = 128; MsgboxPosY = 166; MsgboxPosYQuer = 50; MsgboxBackgrountImg = "\\FlashDisk\\Falcon\\Resources\\Msgbox_Logo_gelb146.png"; MsgboxTextHeitht = (UInt16)(75 - ButtonHeight); MsgBoxButtonOKPosX = 40; MsgBoxButtonOKPosY = 98; MsgBoxButtonCancelPosX = 131; MsgBoxButtonCancelPosY = 98; } else if (MsgboxText.Length < 60) { MsgboxHeight = 175; MsgboxPosX = 10; MsgboxPosXQuer = 128; MsgboxPosY = 166; MsgboxPosYQuer = 50; MsgboxBackgrountImg = "\\FlashDisk\\Falcon\\Resources\\Msgbox_Logo_gelb286.png"; MsgboxTextHeitht = (UInt16)(104 - ButtonHeight); MsgBoxButtonOKPosX = 40; MsgBoxButtonOKPosY = 127; MsgBoxButtonCancelPosX = 131; MsgBoxButtonCancelPosY = 127;
} else { MsgboxHeight = 275; MsgboxPosX = 10; MsgboxPosXQuer = 128; MsgboxPosY = 115; MsgboxPosYQuer = 0; MsgboxBackgrountImg = "\\FlashDisk\\Falcon\\Resources\\Msgbox_Logo_gelb275.png"; MsgboxTextHeitht = (UInt16)(206 - ButtonHeight); MsgBoxButtonOKPosX = 40; MsgBoxButtonOKPosY = 229; MsgBoxButtonCancelPosX = 131; MsgBoxButtonCancelPosY = 229; }
if (Hochkant) { newMessageBox.Location = new System.Drawing.Point(MsgboxPosX, MsgboxPosY); } else { newMessageBox.Location = new System.Drawing.Point(MsgboxPosXQuer, MsgboxPosYQuer); } newMessageBox.Size = new System.Drawing.Size(MsgboxLength, MsgboxHeight); newMessageBox.BackgroundImage = new System.Drawing.Bitmap(MsgboxBackgrountImg); tMsgboxText.Size = new System.Drawing.Size(MsgboxTextLength, MsgboxTextHeitht); MsgBoxButtonOK.Location = new System.Drawing.Point(MsgBoxButtonOKPosX, MsgBoxButtonOKPosY); MsgBoxButtonCancel.Location = new System.Drawing.Point(MsgBoxButtonCancelPosX, MsgBoxButtonCancelPosY);
switch(MsgboxKeys) { case MSGBOX_KEYS.NO_KEYS: MsgBoxButtonOK.Visible = false; MsgBoxButtonCancel.Visible = false; MsgBoxTimer.Enabled = true; break; case MSGBOX_KEYS.OK_CANCEL: MsgBoxButtonOK.Visible = true; MsgBoxButtonCancel.Visible = true; break; case MSGBOX_KEYS.OK_ONLY: MsgBoxButtonOK.Visible = true; MsgBoxButtonOKPosX = 84; MsgBoxButtonOK.Location = new System.Drawing.Point(MsgBoxButtonOKPosX, MsgBoxButtonOKPosY); MsgBoxButtonCancel.Visible = false; break; case MSGBOX_KEYS.YES_NO: MsgBoxButtonOK.Text = textYes; MsgBoxButtonOK.Visible = true; MsgBoxButtonCancel.Text = textNo; MsgBoxButtonCancel.Visible = true; break; case MSGBOX_KEYS.YES_NO_CANCEL: break; default: break; }
MsgBoxButtonOK.Focus(); }
private void MyMessagebox_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Escape) MsgBox_Close();
if (e.KeyCode == Keys.Right) MsgBoxButtonCancel.Focus(); if (e.KeyCode == Keys.Left) MsgBoxButtonOK.Focus();
}
private void MsgBoxButtonOK_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Right) MsgBoxButtonCancel.Focus(); if (e.KeyCode == Keys.Up) MsgBoxButtonOK.Focus(); if (e.KeyCode == Keys.Down) MsgBoxButtonOK.Focus();
if (e.KeyCode == Keys.Enter) MsgBoxButtonOK_Click(sender, e); }
private void MsgBoxButtonCancel_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Left) MsgBoxButtonOK.Focus();
if (e.KeyCode == Keys.Up) MsgBoxButtonCancel.Focus();
if (e.KeyCode == Keys.Down) MsgBoxButtonCancel.Focus();
if (e.KeyCode == Keys.Enter) MsgBoxButtonCancel_Click(sender, e); }
private void MsgBoxTimer_Tick(object sender, EventArgs e) { MsgBoxTimer.Enabled = false;
MsgBox_Close(); }
} |
-------------- Ende Klasse MyMessagebox -----------------------------------
Aufgerufen wird die Messagebox so:
--------------- Form2 --------------------------------------
C#-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
| ... OpenMyMessageboxDialog(); string btnClicked = MyMessagebox.ShowBox(rm.GetString("EditUseCase1_ResetMessage", ci), rm.GetString("EditUseCase1_MessageTitle", ci), MyMessagebox.MSGBOX_KEYS.YES_NO); CloseMyMessageboxDialog();
if (btnClicked == "1") ... |
--------------- Ende Form2 --------------------------------------
-------------- Mainform ------------------------------------------
C#-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23:
| ... private void OpenMyMessageboxDialog() { MyMessageboxDialog.CloseMyMessageboxDialog += new MyMessagebox.CloseMyMessageboxDialogEvent(CloseMyMessageboxDialog);
OpenWindowStack.Push(Dialoge.MyMessageboxDialog);
MyMessageboxDialog.Hochkant = this.Hochkant; MyMessageboxDialog.Language = this.Language; }
private void CloseMyMessageboxDialog() { OpenWindowStack.Pop();
MyMessageboxDialog.CloseMyMessageboxDialog -= new MyMessagebox.CloseMyMessageboxDialogEvent(CloseMyMessageboxDialog); } ... |
--------------- Ende Mainform --------------------------------------------
Zur Erklärung:
Das Open- und Close-Event benötige ich nur, damit ich den Messagebox-Dialog auf den Stack legen kann. Wenn dann die Sprache geändert wird oder der Bildschirm gedreht wird (hochkant -> querformat oder quer->hoch), dann wird der PropertyChanged-Event aufgerufen. Außerdem sollen gewisse Tasten des PDAs deaktiviert sein, wenn die Msgbox offen ist.
Wahrscheinlich schlagt ihr eh schon die Hände über Euren Köpfen zusammen, was ich da für ein Durcheinander programmiert habe.
Ich glaube, dass das Erzeugen von der Instanz newMessagebox an der falschen Stelle steht. Erzeugt wird diese Instanz in der Funktion ShowBox, verwendet wird diese Instanz aber auch z.B. in der PropertyChanged-Methode. Es funktioniert, aber kann das richtig sein?
Die Messagebox hat 3 verschiedene Größen und unterschiedliche Button-Optionen und je nachdem, ob der Bildschirm im Hoch- oder Querformat ist unterschiedliche Positionen am Bildschirm. Wenn keine Buttons gewünscht sind, schließt sie automatisch.
Vielen Dank für Eure Hilfe schon mal.
Grüße
Claudia (flexray)
Moderiert von
Kha: csharp- durch cs-Tags ersetzt