@@ -17,9 +17,12 @@ public partial class Form1 : Form
1717 {
1818 private FindReplace MyFindReplace ;
1919 private int maxLineNumberCharLength ;
20+ private int previousNumberMarginWidth ;
2021
21- // The default line number is 1, anything else is a custom line number
22- private const int customStartingLineNumber = 8 ;
22+ // The default line number is 1, anything else is a custom line number
23+ private const int CustomStartingLineNumber = 8 ;
24+ private const int DefaultStartingLineIndex = 0 ;
25+ private const int DefaultNumberMarginWidth = 30 ;
2326
2427 public Form1 ( )
2528 {
@@ -125,31 +128,24 @@ private void Scintilla_Delete(object sender, ModificationEventArgs e)
125128
126129 private void UpdateMarginLineNumbers ( int startingAtIndex )
127130 {
128- // Starting at the specified line index, update each
131+ // Starting at the specified line index, update each
129132 // subsequent line margin text with the offset.
130- for ( int i = 0 ; i < scintilla1 . Lines . Count ; i ++ )
133+ for ( int i = startingAtIndex ; i < scintilla1 . Lines . Count ; i ++ )
131134 {
132135 scintilla1 . Lines [ i ] . MarginStyle = Style . LineNumber ;
133- scintilla1 . Lines [ i ] . MarginText = ( i + customStartingLineNumber ) . ToString ( ) ;
136+ scintilla1 . Lines [ i ] . MarginText = ( i + CustomStartingLineNumber ) . ToString ( ) ;
134137 }
135138 }
136139
137- private void ResetNumberMargin ( )
138- {
139- for ( int i = 0 ; i < scintilla1 . Lines . Count ; i ++ ) // Count - 1?
140- {
141- scintilla1 . Lines [ i ] . MarginStyle = Style . LineNumber ;
142- scintilla1 . Lines [ i ] . MarginText = string . Empty ;
143- }
144- }
145-
146- private void Scintilla_TextChanged ( object sender , EventArgs e )
140+ private void Scintilla_TextChanged ( object sender , EventArgs e )
147141 {
148142 // Adjust the width of the number margin to allow the line numbers to fit in the number margin
143+ int lineCount = scintilla1 . Lines . Count ;
149144
150- int lineCount = customStartingLineNumber + scintilla1 . Lines . Count ;
145+ if ( enableCustomLineNumber . Checked )
146+ lineCount += CustomStartingLineNumber ;
151147
152- // Did the number of characters in the line number display change?
148+ // Did the number of characters in the line number display change?
153149 // i.e. nnn VS nn, or nnnn VS nn, etc...
154150
155151 // First find the number of digits\characters in the line count
@@ -176,25 +172,34 @@ private void InitNumberMargin()
176172 scintilla1 . Insert += Scintilla_Insert ;
177173 scintilla1 . Delete += Scintilla_Delete ;
178174
179- // Set the defaults for the number margin
180- scintilla1 . Margins [ 0 ] . Width = 30 ;
181- scintilla1 . Margins [ 0 ] . Type = MarginType . Number ;
175+ // Set the defaults for the number margin
176+ scintilla1 . Margins [ 0 ] . Type = MarginType . Number ;
182177 scintilla1 . Margins [ 0 ] . Sensitive = true ;
183178 scintilla1 . Margins [ 0 ] . Mask = 0 ;
184-
179+
185180 // Line numbers can only customised by using a text margin rather than the default number margin
186181 scintilla1 . Margins [ 0 ] . Type = MarginType . Text ;
187182
188183 // Light Theme - use defaults by not setting anything
189184 scintilla1 . Styles [ Style . LineNumber ] . ForeColor = Color . CadetBlue ;
190- }
185+
186+ UpdateMarginLineNumbers ( DefaultStartingLineIndex ) ;
187+ }
191188
192189 private void EnableCustomLineNumber_CheckedChanged ( object sender , EventArgs e )
193190 {
194- if ( enableCustomLineNumber . Checked )
195- UpdateMarginLineNumbers ( customStartingLineNumber ) ;
196- else
197- ResetNumberMargin ( ) ;
198- }
191+ // Show the number margin?
192+ if ( enableCustomLineNumber . Checked )
193+ {
194+ // Show the number margin
195+ scintilla1 . Margins [ 0 ] . Width = previousNumberMarginWidth == 0 ? DefaultNumberMarginWidth : previousNumberMarginWidth ;
196+ }
197+ else
198+ {
199+ // Hide the number margin
200+ previousNumberMarginWidth = scintilla1 . Margins [ 0 ] . Width ;
201+ scintilla1 . Margins [ 0 ] . Width = 0 ;
202+ }
203+ }
199204 }
200205}
0 commit comments