IPAddressControl.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. // Copyright (c) 2007-2012 Michael Chapman
  2. // http://ipaddresscontrollib.googlecode.com
  3. // Permission is hereby granted, free of charge, to any person obtaining
  4. // a copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to
  8. // permit persons to whom the Software is furnished to do so, subject to
  9. // the following conditions:
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  15. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  16. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  17. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  18. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. using System;
  20. using System.ComponentModel;
  21. using System.Diagnostics;
  22. using System.Drawing;
  23. using System.Globalization;
  24. using System.Net;
  25. using System.Net.Sockets;
  26. using System.Text;
  27. using System.Windows.Forms;
  28. using System.Windows.Forms.VisualStyles;
  29. namespace IPAddressControlLib
  30. {
  31. [DesignerAttribute( typeof( IPAddressControlDesigner ) )]
  32. public class IPAddressControl : ContainerControl
  33. {
  34. #region Public Constants
  35. public const int FieldCount = 4;
  36. #endregion // Public Constants
  37. #region Public Events
  38. public event EventHandler<FieldChangedEventArgs> FieldChangedEvent;
  39. #endregion //Public Events
  40. #region Public Properties
  41. [Browsable( true )]
  42. public bool AllowInternalTab
  43. {
  44. get
  45. {
  46. foreach ( FieldControl fc in _fieldControls )
  47. {
  48. return fc.TabStop;
  49. }
  50. return false;
  51. }
  52. set
  53. {
  54. foreach ( FieldControl fc in _fieldControls )
  55. {
  56. fc.TabStop = value;
  57. }
  58. }
  59. }
  60. [Browsable( true )]
  61. public bool AnyBlank
  62. {
  63. get
  64. {
  65. foreach ( FieldControl fc in _fieldControls )
  66. {
  67. if ( fc.Blank )
  68. {
  69. return true;
  70. }
  71. }
  72. return false;
  73. }
  74. }
  75. [Browsable( true )]
  76. public bool AutoHeight
  77. {
  78. get { return _autoHeight; }
  79. set
  80. {
  81. _autoHeight= value;
  82. if ( _autoHeight )
  83. {
  84. AdjustSize();
  85. }
  86. }
  87. }
  88. [Browsable( false )]
  89. public int Baseline
  90. {
  91. get
  92. {
  93. NativeMethods.TEXTMETRIC textMetric = GetTextMetrics( Handle, Font );
  94. int offset = textMetric.tmAscent + 1;
  95. switch ( BorderStyle )
  96. {
  97. case BorderStyle.Fixed3D:
  98. offset += Fixed3DOffset.Height;
  99. break;
  100. case BorderStyle.FixedSingle:
  101. offset += FixedSingleOffset.Height;
  102. break;
  103. }
  104. return offset;
  105. }
  106. }
  107. [Browsable( true )]
  108. public bool Blank
  109. {
  110. get
  111. {
  112. foreach ( FieldControl fc in _fieldControls )
  113. {
  114. if ( !fc.Blank )
  115. {
  116. return false;
  117. }
  118. }
  119. return true;
  120. }
  121. }
  122. [Browsable( true )]
  123. public BorderStyle BorderStyle
  124. {
  125. get { return _borderStyle; }
  126. set
  127. {
  128. _borderStyle = value;
  129. AdjustSize();
  130. Invalidate();
  131. }
  132. }
  133. [Browsable( false )]
  134. public override bool Focused
  135. {
  136. get
  137. {
  138. foreach ( FieldControl fc in _fieldControls )
  139. {
  140. if ( fc.Focused )
  141. {
  142. return true;
  143. }
  144. }
  145. return false;
  146. }
  147. }
  148. [Browsable( false ), DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden )]
  149. public IPAddress IPAddress
  150. {
  151. get { return new IPAddress( GetAddressBytes() ); }
  152. set
  153. {
  154. Clear();
  155. if ( null == value ) { return; }
  156. if ( value.AddressFamily == AddressFamily.InterNetwork )
  157. {
  158. SetAddressBytes( value.GetAddressBytes() );
  159. }
  160. }
  161. }
  162. [Browsable( true )]
  163. public override Size MinimumSize
  164. {
  165. get { return CalculateMinimumSize(); }
  166. }
  167. [Browsable( true )]
  168. public bool ReadOnly
  169. {
  170. get { return _readOnly; }
  171. set
  172. {
  173. _readOnly = value;
  174. foreach ( FieldControl fc in _fieldControls )
  175. {
  176. fc.ReadOnly = _readOnly;
  177. }
  178. foreach ( DotControl dc in _dotControls )
  179. {
  180. dc.ReadOnly = _readOnly;
  181. }
  182. Invalidate();
  183. }
  184. }
  185. [Bindable(true)]
  186. [Browsable(true)]
  187. [DesignerSerializationVisibility( DesignerSerializationVisibility.Visible )]
  188. public override string Text
  189. {
  190. get
  191. {
  192. StringBuilder sb = new StringBuilder(); ;
  193. for ( int index = 0; index < _fieldControls.Length; ++index )
  194. {
  195. sb.Append( _fieldControls[ index ].Text );
  196. if ( index < _dotControls.Length )
  197. {
  198. sb.Append( _dotControls[ index ].Text );
  199. }
  200. }
  201. return sb.ToString();
  202. }
  203. set
  204. {
  205. Parse( value );
  206. }
  207. }
  208. #endregion // Public Properties
  209. #region Public Methods
  210. public void Clear()
  211. {
  212. foreach ( FieldControl fc in _fieldControls )
  213. {
  214. fc.Clear();
  215. }
  216. }
  217. public byte[] GetAddressBytes()
  218. {
  219. byte[] bytes = new byte[FieldCount];
  220. for ( int index = 0; index < FieldCount; ++index )
  221. {
  222. bytes[index] = _fieldControls[index].Value;
  223. }
  224. return bytes;
  225. }
  226. [System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", Justification = "Using Bytes seems more informative than SetAddressValues." )]
  227. public void SetAddressBytes( byte[] bytes )
  228. {
  229. Clear();
  230. if ( bytes == null )
  231. {
  232. return;
  233. }
  234. int length = Math.Min( FieldCount, bytes.Length );
  235. for ( int i = 0; i < length; ++i )
  236. {
  237. _fieldControls[i].Text = bytes[i].ToString( CultureInfo.InvariantCulture );
  238. }
  239. }
  240. public void SetFieldFocus( int fieldIndex )
  241. {
  242. if ( ( fieldIndex >= 0 ) && ( fieldIndex < FieldCount ) )
  243. {
  244. _fieldControls[fieldIndex].TakeFocus( Direction.Forward, Selection.All );
  245. }
  246. }
  247. public void SetFieldRange( int fieldIndex, byte rangeLower, byte rangeUpper )
  248. {
  249. if ( ( fieldIndex >= 0 ) && ( fieldIndex < FieldCount ) )
  250. {
  251. _fieldControls[fieldIndex].RangeLower = rangeLower;
  252. _fieldControls[fieldIndex].RangeUpper = rangeUpper;
  253. }
  254. }
  255. public override string ToString()
  256. {
  257. StringBuilder sb = new StringBuilder();
  258. for ( int index = 0; index < FieldCount; ++index )
  259. {
  260. sb.Append( _fieldControls[index].ToString() );
  261. if ( index < _dotControls.Length )
  262. {
  263. sb.Append( _dotControls[index].ToString() );
  264. }
  265. }
  266. return sb.ToString();
  267. }
  268. #endregion Public Methods
  269. #region Constructors
  270. public IPAddressControl()
  271. {
  272. BackColor = SystemColors.Window;
  273. ResetBackColorChanged();
  274. for ( int index = 0; index < _fieldControls.Length; ++index )
  275. {
  276. _fieldControls[index] = new FieldControl();
  277. _fieldControls[index].CreateControl();
  278. _fieldControls[index].FieldIndex = index;
  279. _fieldControls[index].Name = "FieldControl" + index.ToString( CultureInfo.InvariantCulture );
  280. _fieldControls[index].Parent = this;
  281. _fieldControls[index].CedeFocusEvent += new EventHandler<CedeFocusEventArgs>( OnCedeFocus );
  282. _fieldControls[index].Click += new EventHandler( OnSubControlClicked );
  283. _fieldControls[index].DoubleClick += new EventHandler( OnSubControlDoubleClicked );
  284. _fieldControls[index].GotFocus += new EventHandler( OnFieldGotFocus );
  285. _fieldControls[index].KeyDown += new KeyEventHandler( OnFieldKeyDown );
  286. _fieldControls[index].KeyPress += new KeyPressEventHandler( OnFieldKeyPressed );
  287. _fieldControls[index].KeyUp += new KeyEventHandler( OnFieldKeyUp );
  288. _fieldControls[index].LostFocus += new EventHandler( OnFieldLostFocus );
  289. _fieldControls[index].MouseClick += new MouseEventHandler( OnSubControlMouseClicked );
  290. _fieldControls[index].MouseDoubleClick += new MouseEventHandler( OnSubControlMouseDoubleClicked );
  291. _fieldControls[index].MouseEnter += new EventHandler( OnSubControlMouseEntered );
  292. _fieldControls[index].MouseHover += new EventHandler( OnSubControlMouseHovered );
  293. _fieldControls[index].MouseLeave += new EventHandler( OnSubControlMouseLeft );
  294. _fieldControls[index].MouseMove += new MouseEventHandler( OnSubControlMouseMoved );
  295. _fieldControls[index].PreviewKeyDown += new PreviewKeyDownEventHandler( OnFieldPreviewKeyDown );
  296. _fieldControls[index].TextChangedEvent += new EventHandler<TextChangedEventArgs>( OnFieldTextChanged );
  297. Controls.Add( _fieldControls[index] );
  298. if ( index < ( FieldCount - 1 ) )
  299. {
  300. _dotControls[index] = new DotControl();
  301. _dotControls[index].CreateControl();
  302. _dotControls[index].Name = "DotControl" + index.ToString( CultureInfo.InvariantCulture );
  303. _dotControls[index].Parent = this;
  304. _dotControls[index].Click += new EventHandler( OnSubControlClicked );
  305. _dotControls[index].DoubleClick += new EventHandler( OnSubControlDoubleClicked );
  306. _dotControls[index].MouseClick += new MouseEventHandler( OnSubControlMouseClicked );
  307. _dotControls[index].MouseDoubleClick += new MouseEventHandler( OnSubControlMouseDoubleClicked );
  308. _dotControls[index].MouseEnter += new EventHandler( OnSubControlMouseEntered );
  309. _dotControls[index].MouseHover += new EventHandler( OnSubControlMouseHovered );
  310. _dotControls[index].MouseLeave += new EventHandler( OnSubControlMouseLeft );
  311. _dotControls[index].MouseMove += new MouseEventHandler( OnSubControlMouseMoved );
  312. Controls.Add( _dotControls[index] );
  313. }
  314. }
  315. SetStyle( ControlStyles.AllPaintingInWmPaint, true );
  316. SetStyle( ControlStyles.ContainerControl, true );
  317. SetStyle( ControlStyles.OptimizedDoubleBuffer, true );
  318. SetStyle( ControlStyles.ResizeRedraw, true );
  319. SetStyle( ControlStyles.UserPaint, true );
  320. SetStyle( ControlStyles.FixedWidth, true );
  321. SetStyle( ControlStyles.FixedHeight, true );
  322. _referenceTextBox.AutoSize = true;
  323. Cursor = Cursors.IBeam;
  324. AutoScaleDimensions = new SizeF( 96F, 96F );
  325. AutoScaleMode = AutoScaleMode.Dpi;
  326. Size = MinimumSize;
  327. DragEnter += new DragEventHandler( IPAddressControl_DragEnter );
  328. DragDrop += new DragEventHandler( IPAddressControl_DragDrop );
  329. }
  330. #endregion // Constructors
  331. #region Protected Methods
  332. protected override void Dispose( bool disposing )
  333. {
  334. if ( disposing ) { Cleanup(); }
  335. base.Dispose( disposing );
  336. }
  337. protected override void OnBackColorChanged( EventArgs e )
  338. {
  339. base.OnBackColorChanged( e );
  340. _backColorChanged = true;
  341. }
  342. protected override void OnFontChanged( EventArgs e )
  343. {
  344. base.OnFontChanged( e );
  345. AdjustSize();
  346. }
  347. protected override void OnGotFocus( EventArgs e )
  348. {
  349. base.OnGotFocus( e );
  350. _focused = true;
  351. _fieldControls[0].TakeFocus( Direction.Forward, Selection.All );
  352. }
  353. protected override void OnLostFocus( EventArgs e )
  354. {
  355. if ( !Focused )
  356. {
  357. _focused = false;
  358. base.OnLostFocus( e );
  359. }
  360. }
  361. protected override void OnMouseEnter( EventArgs e )
  362. {
  363. if ( !_hasMouse )
  364. {
  365. _hasMouse = true;
  366. base.OnMouseEnter( e );
  367. }
  368. }
  369. protected override void OnMouseLeave( EventArgs e )
  370. {
  371. if ( !HasMouse )
  372. {
  373. base.OnMouseLeave( e );
  374. _hasMouse = false;
  375. }
  376. }
  377. protected override void OnPaint( PaintEventArgs e )
  378. {
  379. if ( null == e ) { throw new ArgumentNullException( "e" ); }
  380. base.OnPaint( e );
  381. Color backColor = BackColor;
  382. if ( !_backColorChanged )
  383. {
  384. if ( !Enabled || ReadOnly )
  385. {
  386. backColor = SystemColors.Control;
  387. }
  388. }
  389. using ( SolidBrush backgroundBrush = new SolidBrush( backColor ) )
  390. {
  391. e.Graphics.FillRectangle( backgroundBrush, ClientRectangle );
  392. }
  393. Rectangle rectBorder = new Rectangle( ClientRectangle.Left, ClientRectangle.Top,
  394. ClientRectangle.Width - 1, ClientRectangle.Height - 1 );
  395. switch ( BorderStyle )
  396. {
  397. case BorderStyle.Fixed3D:
  398. if ( Application.RenderWithVisualStyles )
  399. {
  400. using ( Pen pen = new Pen( VisualStyleInformation.TextControlBorder ) )
  401. {
  402. e.Graphics.DrawRectangle( pen, rectBorder );
  403. }
  404. rectBorder.Inflate( -1, -1 );
  405. e.Graphics.DrawRectangle( SystemPens.Window, rectBorder );
  406. }
  407. else
  408. {
  409. ControlPaint.DrawBorder3D( e.Graphics, ClientRectangle, Border3DStyle.Sunken );
  410. }
  411. break;
  412. case BorderStyle.FixedSingle:
  413. ControlPaint.DrawBorder( e.Graphics, ClientRectangle,
  414. SystemColors.WindowFrame, ButtonBorderStyle.Solid );
  415. break;
  416. }
  417. }
  418. protected override void OnSizeChanged( EventArgs e )
  419. {
  420. base.OnSizeChanged( e );
  421. AdjustSize();
  422. }
  423. #endregion // Protected Methods
  424. #region Private Properties
  425. private bool HasMouse
  426. {
  427. get
  428. {
  429. return DisplayRectangle.Contains( PointToClient( MousePosition ) );
  430. }
  431. }
  432. #endregion // Private Properties
  433. #region Private Methods
  434. private void AdjustSize()
  435. {
  436. Size newSize = MinimumSize;
  437. if ( Width > newSize.Width )
  438. {
  439. newSize.Width = Width;
  440. }
  441. if ( Height > newSize.Height )
  442. {
  443. newSize.Height = Height;
  444. }
  445. if ( AutoHeight )
  446. {
  447. Size = new Size( newSize.Width, MinimumSize.Height );
  448. }
  449. else
  450. {
  451. Size = newSize;
  452. }
  453. LayoutControls();
  454. }
  455. private Size CalculateMinimumSize()
  456. {
  457. Size minimumSize = new Size( 0, 0 );
  458. foreach ( FieldControl fc in _fieldControls )
  459. {
  460. minimumSize.Width += fc.Width;
  461. minimumSize.Height = Math.Max( minimumSize.Height, fc.Height );
  462. }
  463. foreach ( DotControl dc in _dotControls )
  464. {
  465. minimumSize.Width += dc.Width;
  466. minimumSize.Height = Math.Max( minimumSize.Height, dc.Height );
  467. }
  468. switch ( BorderStyle )
  469. {
  470. case BorderStyle.Fixed3D:
  471. minimumSize.Width += 6;
  472. minimumSize.Height += ( GetSuggestedHeight() - minimumSize.Height );
  473. break;
  474. case BorderStyle.FixedSingle:
  475. minimumSize.Width += 4;
  476. minimumSize.Height += ( GetSuggestedHeight() - minimumSize.Height );
  477. break;
  478. }
  479. return minimumSize;
  480. }
  481. private void Cleanup()
  482. {
  483. foreach ( DotControl dc in _dotControls )
  484. {
  485. Controls.Remove( dc );
  486. dc.Dispose();
  487. }
  488. foreach ( FieldControl fc in _fieldControls )
  489. {
  490. Controls.Remove( fc );
  491. fc.Dispose();
  492. }
  493. _dotControls = null;
  494. _fieldControls = null;
  495. }
  496. private int GetSuggestedHeight()
  497. {
  498. _referenceTextBox.BorderStyle = BorderStyle;
  499. _referenceTextBox.Font = Font;
  500. return _referenceTextBox.Height;
  501. }
  502. [System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Usage", "CA1806", Justification = "What should be done if ReleaseDC() doesn't work?" )]
  503. private static NativeMethods.TEXTMETRIC GetTextMetrics( IntPtr hwnd, Font font )
  504. {
  505. IntPtr hdc = NativeMethods.GetWindowDC( hwnd );
  506. NativeMethods.TEXTMETRIC textMetric;
  507. IntPtr hFont = font.ToHfont();
  508. try
  509. {
  510. IntPtr hFontPrevious = NativeMethods.SelectObject( hdc, hFont );
  511. NativeMethods.GetTextMetrics( hdc, out textMetric );
  512. NativeMethods.SelectObject( hdc, hFontPrevious );
  513. }
  514. finally
  515. {
  516. NativeMethods.ReleaseDC( hwnd, hdc );
  517. NativeMethods.DeleteObject( hFont );
  518. }
  519. return textMetric;
  520. }
  521. private void IPAddressControl_DragDrop( object sender, System.Windows.Forms.DragEventArgs e )
  522. {
  523. Text = e.Data.GetData( DataFormats.Text ).ToString();
  524. }
  525. private void IPAddressControl_DragEnter( object sender, System.Windows.Forms.DragEventArgs e )
  526. {
  527. if ( e.Data.GetDataPresent( DataFormats.Text ) )
  528. {
  529. e.Effect = DragDropEffects.Copy;
  530. }
  531. else
  532. {
  533. e.Effect = DragDropEffects.None;
  534. }
  535. }
  536. private void LayoutControls()
  537. {
  538. SuspendLayout();
  539. int difference = Width - MinimumSize.Width;
  540. Debug.Assert( difference >= 0 );
  541. int numOffsets = _fieldControls.Length + _dotControls.Length + 1;
  542. int div = difference / ( numOffsets );
  543. int mod = difference % ( numOffsets );
  544. int[] offsets = new int[numOffsets];
  545. for ( int index = 0; index < numOffsets; ++index )
  546. {
  547. offsets[index] = div;
  548. if ( index < mod )
  549. {
  550. ++offsets[index];
  551. }
  552. }
  553. int x = 0;
  554. int y = 0;
  555. switch ( BorderStyle )
  556. {
  557. case BorderStyle.Fixed3D:
  558. x = Fixed3DOffset.Width;
  559. y = Fixed3DOffset.Height;
  560. break;
  561. case BorderStyle.FixedSingle:
  562. x = FixedSingleOffset.Width;
  563. y = FixedSingleOffset.Height;
  564. break;
  565. }
  566. int offsetIndex = 0;
  567. x += offsets[offsetIndex++];
  568. for ( int i = 0; i < _fieldControls.Length; ++i )
  569. {
  570. _fieldControls[i].Location = new Point( x, y );
  571. x += _fieldControls[i].Width;
  572. if ( i < _dotControls.Length )
  573. {
  574. x += offsets[offsetIndex++];
  575. _dotControls[i].Location = new Point( x, y );
  576. x += _dotControls[i].Width;
  577. x += offsets[offsetIndex++];
  578. }
  579. }
  580. ResumeLayout( false );
  581. }
  582. private void OnCedeFocus( Object sender, CedeFocusEventArgs e )
  583. {
  584. switch ( e.Action )
  585. {
  586. case Action.Home:
  587. _fieldControls[0].TakeFocus( Action.Home );
  588. return;
  589. case Action.End:
  590. _fieldControls[FieldCount - 1].TakeFocus( Action.End );
  591. return;
  592. case Action.Trim:
  593. if ( e.FieldIndex == 0 )
  594. {
  595. return;
  596. }
  597. _fieldControls[e.FieldIndex - 1].TakeFocus( Action.Trim );
  598. return;
  599. }
  600. if ( ( e.Direction == Direction.Reverse && e.FieldIndex == 0 ) ||
  601. ( e.Direction == Direction.Forward && e.FieldIndex == ( FieldCount - 1 ) ) )
  602. {
  603. return;
  604. }
  605. int fieldIndex = e.FieldIndex;
  606. if ( e.Direction == Direction.Forward )
  607. {
  608. ++fieldIndex;
  609. }
  610. else
  611. {
  612. --fieldIndex;
  613. }
  614. _fieldControls[fieldIndex].TakeFocus( e.Direction, e.Selection );
  615. }
  616. private void OnFieldGotFocus( Object sender, EventArgs e )
  617. {
  618. if ( !_focused )
  619. {
  620. _focused = true;
  621. base.OnGotFocus( EventArgs.Empty );
  622. }
  623. }
  624. private void OnFieldKeyDown( Object sender, KeyEventArgs e )
  625. {
  626. OnKeyDown( e );
  627. }
  628. private void OnFieldKeyPressed( Object sender, KeyPressEventArgs e )
  629. {
  630. OnKeyPress( e );
  631. }
  632. private void OnFieldPreviewKeyDown( Object sender, PreviewKeyDownEventArgs e )
  633. {
  634. OnPreviewKeyDown( e );
  635. }
  636. private void OnFieldKeyUp( Object sender, KeyEventArgs e )
  637. {
  638. OnKeyUp( e );
  639. }
  640. private void OnFieldLostFocus( Object sender, EventArgs e )
  641. {
  642. if ( !Focused )
  643. {
  644. _focused = false;
  645. base.OnLostFocus( EventArgs.Empty );
  646. }
  647. }
  648. private void OnFieldTextChanged( Object sender, TextChangedEventArgs e )
  649. {
  650. if ( null != FieldChangedEvent )
  651. {
  652. FieldChangedEventArgs args = new FieldChangedEventArgs();
  653. args.FieldIndex = e.FieldIndex;
  654. args.Text = e.Text;
  655. FieldChangedEvent( this, args );
  656. }
  657. OnTextChanged( EventArgs.Empty );
  658. }
  659. private void OnSubControlClicked( object sender, EventArgs e )
  660. {
  661. OnClick( e );
  662. }
  663. private void OnSubControlDoubleClicked( object sender, EventArgs e )
  664. {
  665. OnDoubleClick( e );
  666. }
  667. private void OnSubControlMouseClicked( object sender, MouseEventArgs e )
  668. {
  669. OnMouseClick( e );
  670. }
  671. private void OnSubControlMouseDoubleClicked( object sender, MouseEventArgs e )
  672. {
  673. OnMouseDoubleClick( e );
  674. }
  675. private void OnSubControlMouseEntered( object sender, EventArgs e )
  676. {
  677. OnMouseEnter( e );
  678. }
  679. private void OnSubControlMouseHovered( object sender, EventArgs e )
  680. {
  681. OnMouseHover( e );
  682. }
  683. private void OnSubControlMouseLeft( object sender, EventArgs e )
  684. {
  685. OnMouseLeave( e );
  686. }
  687. private void OnSubControlMouseMoved( object sender, MouseEventArgs e )
  688. {
  689. OnMouseMove( e );
  690. }
  691. private void Parse( String text )
  692. {
  693. Clear();
  694. if ( null == text )
  695. {
  696. return;
  697. }
  698. int textIndex = 0;
  699. int index = 0;
  700. for ( index = 0; index < _dotControls.Length; ++index )
  701. {
  702. int findIndex = text.IndexOf( _dotControls[index].Text, textIndex, StringComparison.Ordinal );
  703. if ( findIndex >= 0 )
  704. {
  705. _fieldControls[index].Text = text.Substring( textIndex, findIndex - textIndex );
  706. textIndex = findIndex + _dotControls[index].Text.Length;
  707. }
  708. else
  709. {
  710. break;
  711. }
  712. }
  713. _fieldControls[index].Text = text.Substring( textIndex );
  714. }
  715. // a hack to remove an FxCop warning
  716. private void ResetBackColorChanged()
  717. {
  718. _backColorChanged = false;
  719. }
  720. #endregion Private Methods
  721. #region Private Data
  722. private bool _autoHeight = true;
  723. private bool _backColorChanged;
  724. private BorderStyle _borderStyle = BorderStyle.Fixed3D;
  725. private DotControl[] _dotControls = new DotControl[FieldCount - 1];
  726. private FieldControl[] _fieldControls = new FieldControl[FieldCount];
  727. private bool _focused;
  728. private bool _hasMouse;
  729. private bool _readOnly;
  730. private Size Fixed3DOffset = new Size( 3, 3 );
  731. private Size FixedSingleOffset = new Size( 2, 2 );
  732. private TextBox _referenceTextBox = new TextBox();
  733. #endregion // Private Data
  734. }
  735. }