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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
/// <summary>
/// Klasse: ValueBox ; abgeleitet von TextBox, Nur Zahlen zugelassen
/// -------
///
/// Diese ValueBox ist eine angepasste TextBox in der man nur Zahlen eingeben kann (incl. MIUNUS,PLUS- und KOMMA-Zeichen).
///
///
/// Zwei statische Members:
/// -----------------------
/// 1. ValueBox.CreateValueBox(myWindowsForm-Objekt, Location_X, Location_y , myFont , myColor , deltaLänge, deltaHöhe, AnzahlZeichen);
/// Diese Funktion erstellt ein TextBox bzw. ValuBox-Objekt und legt es in der WindowsForm an.
/// Die Eigenschaft gibt zusätzlich ein ValueBox-Objekt zurück. (wird ggf. für die 2. Funktion Set_Layout_ValueBox benötigt)
///
/// 2. ValueBox.Set_Layout_ValueBox( ValueBox-Objekt , Location_X, Location_y , myFont , myColor ,deltaLänge ,deltaBreite, AnzahlZeichen);
/// Mit dieser Funktion kann man die Eigenschaften eines bestehenden ValueBox-Objekt nachträglich ändern:
/// -Position(Location), die Länge, die Höhe, die Schriftart, die Schriftfarbe, die AnzahlDerZeichen
///
///
/// 3. In der Eigenschaft: myValueBox.ZAHL steht die Zahl(double) zur weitern Verwendung zu Verfügung.
/// -------------------------------------------------------------------------------------------------
///
/// z.B:
/// private ValueBox myValueBox; //bitte Global !!!
/// ....
/// z.B in Form1_Load
/// myValueBox=ValueBox.CreateValueBox(this , 10 , 100 , textBox1.Font , Color.Black , 0 , 0 , "9999"); //ValueBox anlegen
/// ....
/// z.B. in einen button1_Click oder sonst wo... (Zusatzfunktion)
/// ValueBox.Set_Layout_ValueBox(myValueBox , 10 , 200 , (new Font(new FontFamily("Arial"),8,
/// FontStyle.Regular, GraphicsUnit.Pixel)) , Color.Red , 0 , 0 , "99999999999");
///
/// ....
/// doubel wert=myValueBox.ZAHL; // <----- der Inhalt der ValueBox steht hier drin
///
/// 4. weiter Eigenschaften sind (myValueBox.)
/// -MIN und MAX -> Eingabe der Zahl eingrenzen (double)
/// -KOMMA -> ZAHL mit oder ohne Komma (bool)
/// -AUTO_SIZE -> die Boxlänge automatisch anhand der MaxLength anpassen (bool)
/// -MIT_MINUS -> Minuszeichen zugelassen oder nicht (bool)
///
///
/// benoetigte USINGS
/// using System;
/// using System.Collections.Generic;
/// using System.Linq;
/// using System.Text;
/// using System.Windows.Forms;
/// using System.Drawing;
///
/// viel Spass
/// Andy
/// </summary>
public class ValueBox : TextBox
{
#region div. Eigenschaften
private bool MINUS=false;
private double dZahl=0;
private double dmin=0;
private double dmax=0;
private int org_width=0;
private bool mit_komma=true;
private bool mit_minus=true;
private bool mit_autosize=false;
private ValueBoxValues valueBox_Values;
// hier wird der Wert der ValueBox abgelegt
// Aufruf: myValueBox.ZAHL als double
[Description("Double-Value of the textbox")]
public double ZAHL
{
get { return dZahl; }
private set { dZahl=value; }
}
[Description("Minimum Value of the textbox")]
public double MIN
{
get { return dmin; }
set { dmin=value; }
}
[Description("Maximum Value of the textbox")]
public double MAX
{
get { return dmax; }
set { dmax=value; }
}
[Description("Value with or without comma")]
public bool KOMMA
{
get { return mit_komma; }
set { mit_komma=value; }
}
[Description("Textbox Autosize TRUE or FALSE")]
public bool AUTO_SIZE
{
get { return mit_autosize; }
set { mit_autosize=value; }
}
[Description("With or without minus sign")]
public bool MIT_MINUS
{
get { return mit_minus; }
set { mit_minus=value; }
}
#endregion
// Konstructor...
public ValueBox()
{
//this.MaxLength="123456789012".Length;
this.ZAHL=0;
this.Text="";
this.Refresh();
}
// -----------------------------------------------------------------------------------------------------------------------------
#region zwei statische Members - ValueBox.CreateValueBox(....) und ValueBox.Set_Layout_ValueBox(...)
/// <summary>
/// ValueBox erzeugen und in einer Windows-Form anlegen
///
/// </summary>
/// <param name="f"> Form Formobjekt</param>
/// <param name="x"> int Location_X</param>
/// <param name="y"> int Location_y</param>
/// <param name="myf"> Font (z.B. from anyTexbox.Font)</param>
/// <param name="myC"> Color (z.B. Color.Black)</param>
/// <param name="withdx"> int zusatzLaenge zu der ValueBoxLaenge (um die Box zusätzlich zu verlängern)</param>
/// <param name="heightdx"> int zusatzHoehe zu der ValueBoxHoehe (um die Box zusätzlich zu erhöhen)</param>
/// <param name="anzZeichen"> int AnzahlZeichen der ValueBox (hiermit wird die Länge der ValueBox festgelegt)</param>
/// <returns>ein erzeugtes Valueobjekt </returns>
public static ValueBox CreateValueBox( Form f , int x , int y , Font myf , Color myC , int withdx , int heightdx , string anzZeichen )
{
ValueBox myvBox=null;
try
{
myvBox=new ValueBox();
bool ok=Set_Layout_ValueBox(myvBox , x , y , myf , myC , withdx , heightdx , anzZeichen);
if(ok==true)
{
f.Controls.Add(myvBox);
}
else
{
myvBox=null;
}
}
catch(Exception ex)
{
//fehler
MessageBox.Show("Fehler in Member: CreateValueBox\n\n"+ex.Message);
}
return myvBox;
}
/// <summary>
/// Das Layout der ValueBox festlegen - (Location,Länge,Höhe,maxAnzahlZeichen,Font,Color)
/// </summary>
///
/// <param name="vb"> ValueBox-object</param>
/// <param name="x"> int Location_X</param>
/// <param name="y"> int Location_y</param>
/// <param name="myf"> Font (z.B. from anyTexbox.Font)</param>
/// <param name="myC"> Color (z.B. Color.Black)</param>
/// <param name="withdx"> int zusatzLaenge zu der ValueBoxLaenge</param>
/// <param name="heightdx"> int zusatzHoehe zu der ValueBoxHoehe</param>
/// <param name="anzZeichen"> int AnzahlZeichen der ValueBox (hiermit wird die Länge der ValueBox festgelegt)</param>
/// <returns>bool true/false </returns>
public static bool Set_Layout_ValueBox( ValueBox vb , int x , int y , Font myf , Color myC , int withdx , int heightdx , string anzZeichen )
{
bool ok=true;
try
{
Graphics g = vb.CreateGraphics();
SizeF mySsingle=(g.MeasureString(anzZeichen , myf)).ToSize();
mySsingle.Width+=withdx+6;
mySsingle.Height+=heightdx;
int xa=0 , ya=1;
//textbox innerhalb des panels
vb.Location=new Point(xa , ya);
vb.Font=myf;
vb.ForeColor=myC;
vb.Size=mySsingle.ToSize();
vb.MaxLength=anzZeichen.Length;
vb.Location=new Point(x , y);
vb.ZAHL=0;
vb.Text="";
vb.Refresh();
}
catch(Exception ex)
{
//fehler
MessageBox.Show("Fehler in Funktion: Set_Layout_ValueBox\n"+ex.Message);
ok=false;
}
return ok;
}
#endregion
// -----------------------------------------------------------------------------------------------------------------------------
// privates
#region div. private Members...
// TextBoxMemeber OnKeyPress überschreiben, um die nur Zahlen,Minuszeichen,Kommar und Conrtollsteuerzeichen auszufiltern !!!
protected override void OnKeyPress( KeyPressEventArgs e )
{
try
{
// wenn dezimalpunkt dann in komma !!!
if(e.KeyChar=='.')
e.KeyChar=',';
if(e.KeyChar=='-' && MIT_MINUS==true) // minus eingegeben
{
if(Anzahl_EinesZeichens_ImText(this.Text , '-') == 0)
{
MINUS=false;
}
if(MINUS==false)
{
this.Text=this.Text.Insert(0 , "-");
MINUS=true;
this.Select(this.Text.Length , 0);
}
if(Anzahl_EinesZeichens_ImText(this.Text , '-') >1)
{
e.Handled = true;
}
}
if(e.KeyChar=='+') // bei eingabe des pluszeichen evt. das minuszeichen wieder rausnehmen
{
if(MINUS==true && this.Text[0]=='-')
{
this.Text=this.Text.Remove(0 , 1); // minuszeichen wieder rausnehmen
this.Select(this.Text.Length , 0);
MINUS=false; // und MINUS-Flag zuruecksetzen, um das MINUS-Zeichen erneut setzen zu koennen
}
e.Handled = true;
return;
}
//pruefen, ob mehr als ein komma eingegeben wurde
if(e.KeyChar==',' && KOMMA==true) // komma eingegeben
{
if(Anzahl_EinesZeichens_ImText(this.Text+e.KeyChar , ',') >1)
{
e.Handled = true;
}
}
// pruefen auf -> zahl , control-steuerzeichen oder ein komma
if(KOMMA==true)
{
if((!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar)) && !(e.KeyChar==','))
e.Handled = true; // wenn nein, dann eingabe ignorieren
}
else
{
if((!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar)) )
e.Handled = true; // wenn nein, dann eingabe ignorieren
}
}
catch(Exception ex)
{
MessageBox.Show("Fehler in Member ValueBox.OnKeyPress\n\n"+ex.Message);
}
}
// TextBoxMemeber OnLeave überschreiben, um die Eigenschaft "ZAHL" zu füllen
// in der Eigenschaft "ZAHL" steht der innhalt der ValueBox !!!!!!!!!!!!!!!!
// Die Eigenschaften MIN und MAX werden nur abgefragt, wenn eine von beiden nicht NULL ist.
// (sprich: wenn beide auf NULL gestzt sind wird nicht abgefragt)
protected override void OnLeave( EventArgs e )
{
try
{
if(!(this.Text=="" || this.Text.Length<=0))
{
ZAHL=Convert.ToDouble(this.Text);
if(!(dmin==0 && dmax==0))
{
//MessageBox.Show(MIN.ToString()+" "+MAX.ToString());
if(ZAHL<MIN)
{
MessageBox.Show(ZAHL.ToString()+" < "+MIN.ToString()+"(MIN)");
this.Focus();
}
if(ZAHL>MAX)
{
MessageBox.Show(ZAHL.ToString()+" > "+MAX.ToString()+"(MAX)");
this.Focus();
}
}
}
else
{
ZAHL=0;
this.Text="";
}
}
catch(Exception ex)
{
MessageBox.Show("Fehler in Member ValueBox.OnLeave\n\nZAHL auf Null gesetzt\n"+ex.Message);
ZAHL=0;
}
base.OnLeave(e);
}
// TextBoxMemeber OnENTER überschreiben, um bei AUTOSIZE=TRUE die Textbox-Grössen automatisch anzupassen !!!
protected override void OnEnter( EventArgs e )
{
if(AUTO_SIZE==true)
{
if(this.MaxLength<=50)
{
StringBuilder s=new StringBuilder();
for(int i=0 ; i<this.MaxLength ; i++)
s.Append("0");
Set_Layout_ValueBox(this , this.Location.X , this.Location.Y , this.Font , this.ForeColor , 0 , 0 , s.ToString());
AUTO_SIZE=false;
}
//else
// MessageBox.Show("NO AUTOZICE avalible! maxLenght("+this.MaxLength+") > 50");
}
base.OnEnter(e);
}
// TextBoxMemeber OnKeyDown überschreiben, bei Enter - eingabe beenden - simuliert TAB-Taste
protected override void OnKeyDown( KeyEventArgs e )
{
if(e.KeyCode==Keys.Enter)
{
SendKeys.Send("{TAB}");
e.Handled = true;
}
base.OnKeyDown(e);
}
// Anzahl eines Zeichens im Text ermitteln
private int Anzahl_EinesZeichens_ImText( string text , char zeichen )
{
int z=0;
foreach(char s in text)
{
if(s==zeichen)
z++;
}
return z;
}
#endregion
}
|