@@ -36,16 +36,16 @@ internal static RichTextBox CreateRichTextBox(string name, Form parentForm)
3636 /// <param name="name">Name of the control.</param>
3737 /// <param name="searchControl">Control in which we're searching for control.</param>
3838 /// <returns>A value of null if no control has been found.</returns>
39- internal static Control FindControl ( string name , Control searchControl )
39+ internal static Control ? FindControl ( string name , Control ? searchControl )
4040 {
41- if ( searchControl . Name == name )
41+ if ( searchControl is null || searchControl . Name == name )
4242 {
43- return searchControl ;
43+ return searchControl ;
4444 }
4545
46- foreach ( Control childControl in searchControl . Controls )
46+ foreach ( var childControl in searchControl . Controls )
4747 {
48- Control foundControl = FindControl ( name , childControl ) ;
48+ var foundControl = FindControl ( name , childControl as Control ) ;
4949 if ( foundControl != null )
5050 {
5151 return foundControl ;
@@ -61,25 +61,25 @@ internal static Control FindControl(string name, Control searchControl)
6161 /// <param name="name">Name of the ToolStripItem</param>
6262 /// <param name="searchCollection">Collection of ToolStripItem we are looking for the item in.</param>
6363 /// <returns>A value of null of no item has been found.</returns>
64- internal static ToolStripItem FindToolStripItem ( string name , ToolStripItemCollection searchCollection )
64+ internal static ToolStripItem ? FindToolStripItem ( string name , ToolStripItemCollection searchCollection )
6565 {
66- foreach ( ToolStripItem childItem in searchCollection )
66+ foreach ( ToolStripItem ? childItem in searchCollection )
6767 {
68- if ( childItem . Name == name )
68+ if ( childItem ? . Name == name )
6969 {
7070 return childItem ;
7171 }
72- if ( childItem is ToolStripDropDownItem )
73- {
74- ToolStripDropDownItem childDropDown = childItem as ToolStripDropDownItem ;
75- ToolStripItem foundItem = FindToolStripItem ( name , childDropDown . DropDownItems ) ;
7672
73+ if ( childItem is ToolStripDropDownItem childDropDown )
74+ {
75+ var foundItem = FindToolStripItem ( name , childDropDown . DropDownItems ) ;
7776 if ( foundItem != null )
7877 {
7978 return foundItem ;
8079 }
8180 }
8281 }
82+
8383 return null ;
8484 }
8585
@@ -92,22 +92,24 @@ internal static ToolStripItem FindToolStripItem(string name, ToolStripItemCollec
9292 /// <returns>
9393 /// A value of null if no control has been found.
9494 /// </returns>
95- internal static TControl FindControl < TControl > ( string name , Control searchControl )
95+ internal static TControl ? FindControl < TControl > ( string name , Control ? searchControl )
9696 where TControl : Control
9797 {
98+ if ( searchControl is null )
99+ return null ;
100+
98101 if ( searchControl . Name == name )
99102 {
100- TControl foundControl = searchControl as TControl ;
103+ var foundControl = searchControl as TControl ;
101104 if ( foundControl != null )
102105 {
103106 return foundControl ;
104107 }
105108 }
106109
107- foreach ( Control childControl in searchControl . Controls )
110+ foreach ( var childControl in searchControl . Controls )
108111 {
109- TControl foundControl = FindControl < TControl > ( name , childControl ) ;
110-
112+ var foundControl = FindControl < TControl > ( name , childControl as Control ) ;
111113 if ( foundControl != null )
112114 {
113115 return foundControl ;
@@ -171,15 +173,18 @@ internal static Form CreateForm(string name, int width, int height, bool show, b
171173 return f ;
172174 }
173175
174- private static Icon GetNLogIcon ( )
176+ private static Icon ? GetNLogIcon ( )
175177 {
176178 using ( var stream = typeof ( FormHelper ) . Assembly . GetManifestResourceStream ( "NLog.Windows.Forms.Resources.NLog.ico" ) )
177179 {
180+ if ( stream is null )
181+ return null ;
182+
178183 return new Icon ( stream ) ;
179184 }
180185 }
181186
182- #region Link support
187+ #region Link support
183188 /// <summary>
184189 /// Replaces currently selected text in the RTB control with a link
185190 /// </summary>
@@ -191,9 +196,7 @@ private static Icon GetNLogIcon()
191196 /// </remarks>
192197 internal static void ChangeSelectionToLink ( RichTextBox textBox , string text , string hyperlink )
193198 {
194- #if NETCOREAPP
195- textBox . SelectedRtf = @"{\rtf1\ansi{\field{\*\fldinst{HYPERLINK """ + text + @"#" + hyperlink + @""" }}{\fldrslt{" + text + @"}}}}" ;
196- #else
199+ #if NETFRAMEWORK
197200 int selectionStart = textBox . SelectionStart ;
198201
199202 //using \v tag to hide hyperlink part of the text, and \v0 to end hiding. See http://stackoverflow.com/a/14339531/376066
@@ -202,9 +205,12 @@ internal static void ChangeSelectionToLink(RichTextBox textBox, string text, str
202205
203206 textBox . Select ( selectionStart , text . Length + 1 + hyperlink . Length ) ; //now select both visible and invisible part
204207 SetSelectionStyle ( textBox , CFM_LINK , CFE_LINK ) ; //and turn into a link
208+ #else
209+ textBox . SelectedRtf = @"{\rtf1\ansi{\field{\*\fldinst{HYPERLINK """ + text + @"#" + hyperlink + @""" }}{\fldrslt{" + text + @"}}}}" ;
205210#endif
206211 }
207212
213+ #if NETFRAMEWORK
208214 /// <summary>
209215 /// Sets selection style for RichTextBox
210216 /// https://msdn.microsoft.com/en-us/library/windows/desktop/bb787883(v=vs.85).aspx
@@ -269,6 +275,7 @@ private struct CHARFORMAT2_STRUCT
269275
270276 [ DllImport ( "user32.dll" , CharSet = CharSet . Auto ) ]
271277 private static extern IntPtr SendMessage ( IntPtr hWnd , int msg , IntPtr wParam , IntPtr lParam ) ;
278+ #endif
272279#endregion
273280 }
274281}
0 commit comments