Skip to content

Commit b110d76

Browse files
committed
fix compile error
1 parent ce06450 commit b110d76

2 files changed

Lines changed: 50 additions & 38 deletions

File tree

snippets/csharp/System.Windows.Forms/CheckBox/CreateParams/Project.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net10.0-windows</TargetFramework>
66
<UseWindowsForms>true</UseWindowsForms>
7+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
78
</PropertyGroup>
89

910
</Project>

snippets/csharp/System.Windows.Forms/CheckBox/CreateParams/createparams.cs

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// <snippet1>
22
using System;
3+
using System.Diagnostics;
34
using System.Drawing;
4-
using System.Windows.Forms;
55
using System.Runtime.InteropServices;
6-
using System.Diagnostics;
7-
using System.IO;
6+
using System.Windows.Forms;
7+
using System.ComponentModel;
88

99
public class MyIconButton : Button
1010
{
11-
private Icon icon;
11+
private Icon _icon;
1212

1313
public MyIconButton()
1414
{
@@ -20,11 +20,11 @@ public MyIconButton(Icon ButtonIcon)
2020
: this()
2121
{
2222
// Assign the icon to the private field.
23-
this.icon = ButtonIcon;
23+
_icon = ButtonIcon;
2424

2525
// Size the button to 4 pixels larger than the icon.
26-
this.Height = icon.Height + 4;
27-
this.Width = icon.Width + 4;
26+
Height = _icon.Height + 4;
27+
Width = _icon.Width + 4;
2828
}
2929

3030
//<snippet3>
@@ -42,29 +42,31 @@ protected override CreateParams CreateParams
4242
}
4343
//</snippet3>
4444

45+
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
4546
public Icon Icon
4647
{
4748
get
4849
{
49-
return icon;
50+
return _icon;
5051
}
5152

5253
set
5354
{
54-
icon = value;
55+
_icon = value;
5556
UpdateIcon();
5657
// Size the button to 4 pixels larger than the icon.
57-
this.Height = icon.Height + 4;
58-
this.Width = icon.Width + 4;
58+
Height = _icon.Height + 4;
59+
Width = _icon.Width + 4;
5960
}
6061
}
6162

6263
protected override void OnHandleCreated(EventArgs e)
6364
{
6465
base.OnHandleCreated(e);
6566

66-
// Update the icon on the button if there is currently an icon assigned to the icon field.
67-
if (icon != null)
67+
// Update the icon on the button if there is
68+
// currently an icon assigned to the icon field.
69+
if (_icon != null)
6870
{
6971
UpdateIcon();
7072
}
@@ -75,13 +77,17 @@ private void UpdateIcon()
7577
IntPtr iconHandle = IntPtr.Zero;
7678

7779
// Get the icon's handle.
78-
if (icon != null)
80+
if (_icon != null)
7981
{
80-
iconHandle = icon.Handle;
82+
iconHandle = _icon.Handle;
8183
}
8284

8385
// Send Windows the message to update the button.
84-
SendMessage(Handle, 0x00F7 /*BM_SETIMAGE value*/, 1 /*IMAGE_ICON value*/, (int)iconHandle);
86+
SendMessage(
87+
Handle,
88+
0x00F7 /*BM_SETIMAGE value*/,
89+
1 /*IMAGE_ICON value*/,
90+
(int)iconHandle);
8591
}
8692

8793
// Import the SendMessage method of the User32 DLL.
@@ -95,9 +101,9 @@ private void UpdateIcon()
95101
// <snippet2>
96102
public class MyApplication : Form
97103
{
98-
private MyIconButton myIconButton;
99-
private Button stdButton;
100-
private OpenFileDialog openDlg;
104+
private readonly MyIconButton _myIconButton;
105+
private readonly Button _stdButton;
106+
private OpenFileDialog _openDlg;
101107

102108
static void Main()
103109
{
@@ -109,30 +115,32 @@ public MyApplication()
109115
try
110116
{
111117
// Create the button with the default icon.
112-
myIconButton = new MyIconButton(new Icon(Application.StartupPath + "\\Default.ico"));
118+
_myIconButton = new MyIconButton(new Icon(Application.StartupPath + "\\Default.ico"));
113119
}
114120
catch (Exception ex)
115121
{
116122
// If the default icon does not exist, create the button without an icon.
117-
myIconButton = new MyIconButton();
123+
_myIconButton = new MyIconButton();
118124
Debug.WriteLine(ex.ToString());
119125
}
120126
finally
121127
{
122-
stdButton = new Button();
128+
_stdButton = new Button();
123129

124130
// Add the Click event handlers.
125-
myIconButton.Click += new EventHandler(this.myIconButton_Click);
126-
stdButton.Click += new EventHandler(this.stdButton_Click);
131+
_myIconButton.Click += new EventHandler(myIconButton_Click);
132+
_stdButton.Click += new EventHandler(stdButton_Click);
127133

128134
// Set the location, text and width of the standard button.
129-
stdButton.Location = new Point(myIconButton.Location.X, myIconButton.Location.Y + myIconButton.Height + 20);
130-
stdButton.Text = "Change Icon";
131-
stdButton.Width = 100;
135+
_stdButton.Location = new Point(
136+
_myIconButton.Location.X,
137+
_myIconButton.Location.Y + _myIconButton.Height + 20);
138+
_stdButton.Text = "Change Icon";
139+
_stdButton.Width = 100;
132140

133141
// Add the buttons to the Form.
134-
this.Controls.Add(stdButton);
135-
this.Controls.Add(myIconButton);
142+
Controls.Add(_stdButton);
143+
Controls.Add(_myIconButton);
136144
}
137145
}
138146

@@ -144,16 +152,19 @@ private void myIconButton_Click(object Sender, EventArgs e)
144152

145153
private void stdButton_Click(object Sender, EventArgs e)
146154
{
147-
// Use an OpenFileDialog to allow the user to assign a new image to the derived button.
148-
openDlg = new OpenFileDialog();
149-
openDlg.InitialDirectory = Application.StartupPath;
150-
openDlg.Filter = "Icon files (*.ico)|*.ico";
151-
openDlg.Multiselect = false;
152-
openDlg.ShowDialog();
153-
154-
if (openDlg.FileName != "")
155+
// Use an OpenFileDialog to allow the user
156+
// to assign a new image to the derived button.
157+
_openDlg = new OpenFileDialog
158+
{
159+
InitialDirectory = Application.StartupPath,
160+
Filter = "Icon files (*.ico)|*.ico",
161+
Multiselect = false
162+
};
163+
_openDlg.ShowDialog();
164+
165+
if (_openDlg.FileName != "")
155166
{
156-
myIconButton.Icon = new Icon(openDlg.FileName);
167+
_myIconButton.Icon = new Icon(_openDlg.FileName);
157168
}
158169
}
159170
}

0 commit comments

Comments
 (0)