Вы находитесь на странице: 1из 31

Form - Win

Form
, Form.
, VS Windows application ,
( Form1) Form
.
,

namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}

, InitializeComponent()
, .
Main.
Win Main Program.cs, Main
Form1

static void Main()


{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}

Application.EnableVisualStyles();
XP
Ctrl+F5 .

.
1. FormBorderStyle - , ,
, .
2. Text -
3. MaximumSize MinimumSize -
.
4. StartPosition - . :
CenterScreen CenterParent
5. WindowState - ( )
6. Icon -
7. MaximizeBox MinimizeBox - (
)
8. Opacity -
9. ShowIcon -
10. ShowInTaskBar - windows explorer
11. TopMost - .

Win - Button

.
.
Button
toolbox Common Controls button


1. Text -
2. FlatStyle -
3. Enabled - true
4. Visible - false ,
5. TabInsex - Tab-
6. Dock - ( )
,
7. Anchor -

.NET ,
,
.
:
, ,
Click

.
.
. ?

.
:

this.button1.Click += new System.EventHandler(this.button1_Click);

Visual Studio ,
Form1.Disigner.cs

private void button1_Click(object sender, EventArgs e)


{
MessageBox.Show(button1.Size.Width + " x " + button1.Size.Height);
}

Win - TextBox
TextBox

.
, ,
.
:
1. Text -
2. CharacterCasing -
3. MultiLine -
4. PasswordChar - ,
( )
5. ReadOnly - true .
:
TextChanged - .
.
Label

private void textBox1_TextChanged(object sender, EventArgs e)


{
label1.Text = textBox1.Text;
}

Win - CheckBox
CheckBox
CheckBox /,
- .
:
1. Text - (). .
2. CheckState - . (//)
3. ThreeState - ()
4. Checked - true , false
CheckedChanged -

private void checkBox1_CheckedChanged(object sender, EventArgs e)


{
MessageBox.Show(checkBox1.Checked.ToString());
}

Win - RadioButton
RadioButton
.
,
.
:
1. Text -
2. Checked - true , false
,
GroupBox

CheckedChanged -

, (
MicroSofta )
Checked

Win - ComboBox
ComboBox


( )
:
1. Text - .
.
.
2. Sorted -
3. Items -
4. DropDownStyle -
Items
1. Add -
2. Clear -
3. Remove -
0 100

for (int i = 0; i < 100; i++)


comboBox1.Items.Add(i);

SelectedItem, SelectedText, SelectedIndex

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)


{
MessageBox.Show(comboBox1.SelectedItem.ToString());
}

SelectedIndexChanged


, .
1.0.0.0

Load

private void Form1_Load(object sender, EventArgs e)


{
this.Text += "
Version: " + System.Reflection.Assembly.GetExecutingAsse
mbly().GetName().Version.ToString();
}


Form
.
. ,

() .

Show() - ( )

ShowDialog() - () (
)


child .
.
DialogResult OK, ,
DialogRegult == OK
.
,
Public
(
)

Form2 window = new Form2();


window.ShowDialog();

OK
( )

if (window.DialogResult == DialogResult.OK)
MessageBox.Show(window.textBox1.Text);


.
.NET 2.0 - MasketTextBox.
.
IP .
IP 4 (v. IP4) .
. .
.

- ErrorProvider
.NET 2.0 ,

( ) - ErrorProvider
, masketTextBox
.

, , IP IP

private void button1_Click(object sender, EventArgs e)


{
// IP
// ...........
//-----------errorProvider1.SetError(maskedTextBox1, " IP");
}

SetError , ,

: ,
,


.NET System.Microsoft.Win32
Regestry
.

Microsoft.Win32.RegistryKey myRegKey = Microsoft.Win32.Registry.LocalMachine;

myRegKey = myRegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");

Object oValue = myRegKey.GetValue("VendorIdentifier");


MessageBox.Show("The central processor of this machine is "+ oValue.ToString());


.NET ServiceProcess ServiceController

.
.
GetDevices() ServiceController.
System.ServiceProcess

using System.ServiceProcess;

string services = "";


foreach (ServiceController sc in ServiceController.GetDevices())
{
services += sc.DisplayName + "\n";
}
MessageBox.Show(services);



:
, ,

.

, Anchor Top, Bottom, Left, Right


, anchor
Bottom, Left
,


.
KeyPress

private void Form1_KeyPress(object sender, KeyPressEventArgs e)

label1.Text = e.KeyChar.ToString();

DLL
DLL.
DLL , Main
exe . ,
DLL .

using System;
using System.Collections.Generic;
using System.Text;
namespace ClassLibrary1
{
public class Class1
{
public static string Today()
{
return DateTime.Now.DayOfWeek.ToString();
}
}
}

DLL, ctrl+shift+B

DLL
DLL .
SolutionExplorer
Add Reference
DLL

ClassLibrary1 Class1

MessageBox.Show(ClassLibrary1.Class1.Today());


.NET , ColorDialog
Label .
colorDialog .
.
,

private void button1_Click_1(object sender, EventArgs e)


{
DialogResult dr = colorDialog1.ShowDialog();
if (dr == DialogResult.OK)
label1.ForeColor = colorDialog1.Color;
}


- OpenFileDialog
.
.
textbox.
DialogResult

private void button1_Click_1(object sender, EventArgs e)


{
DialogResult dr = openFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
textBox1.Text = openFileDialog1.FileName;
}

OpenFileDialog ,

Title -

Filter - (C# |*.cs)

MultiSeelct -

initialDirectory -


- WebBrowser
,
URL.
, URL.
Navigate

private void button1_Click_1(object sender, EventArgs e)


{
webBrowser1.Navigate("http://"+textBox1.Text);
}

- TreeView
( TreeView),

DirectoryInfo System.IO
GetDirectoties ,
Add

DirectoryInfo di = new DirectoryInfo(@"c:\");


foreach (DirectoryInfo di2 in di.GetDirectories())
{
TreeNode tn = new TreeNode(di2.Name);
DirectoryInfo d3 = new DirectoryInfo(di2.FullName);
foreach (DirectoryInfo d4 in d3.GetDirectories())
{
tn.Nodes.Add(new TreeNode(d4.Name));
}
treeView1.Nodes.Add(tn);
}

explorer C


( Wopd'e)
IsMDIcontainer = true
,

MdiParent

Form2 w = new Form2();


w.MdiParent = this;
w.Show();


, ,
Windows.
DateTimeFormatInfo
System.Globalization ShortDatePattern
.

MessageBox.Show(DateTime.Now.ToString());
System.Globalization.DateTimeFormatInfo dtfi = new System.Globalization.Dat
eTimeFormatInfo();
dtfi.ShortDatePattern = "yyyy-MM-dd";
MessageBox.Show(DateTime.Now.ToString(dtfi));


double

Convert.ToDouble("2.5");

.
,

, .
.
NumberFormatInfo

System.Globalization.NumberFormatInfo nfi = new System.Globalization.Number


FormatInfo();
nfi.NumberDecimalSeparator = ".";
Convert.ToDouble("2.5", nfi);


GDI+.
, Graphics DrawEllipse

Graphics g = Graphics.FromHwnd(this.Handle);
g.DrawEllipse(new Pen(Color.Green), 100, 100, 30, 30);

: ,
.



.
. .NET .
Label
Language
Text - ""
Language , Text "Example"
,

UserInterface

public Form1()
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globali
zation.CultureInfo("en-US");
InitializeComponent();
}


,
Label.
ContextMenuStrip.
"Enabled" "Disabled"
contextMenuStrip .
Label
.
,

private void enabledToolStripMenuItem_Click(object sender, EventArgs e)


{
label1.Text = "Enabled";
}
private void disabledToolStripMenuItem_Click(object sender, EventArgs e)
{
label1.Text = "Disabled";
}


NotifyIcon
Icon

.
ShowBalloonTip.
,
,

notifyIcon1.ShowBalloonTip(5000, "Title", "Text", ToolTipIcon.Info);


ProgressBar
- Minimum
Maximum.
Value .
Style.
0 100

for (int i = progressBar1.Minimum; i < progressBar1.Maximum; i++)


{
progressBar1.Value = i;
System.Threading.Thread.Sleep(10);
}


StatusStrip
-
, splitbutton, dropdownbutton
( toolStripStatusLabel)
Text

toolStripStatusLabel1.Text = " ";


Timer
interval
Tick Interval
enable false

private void timer1_Tick(object sender, EventArgs e)


{
label1.Text = DateTime.Now.ToString();
}


WIN
.

XML

using System.Configuration;

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(C


onfigurationUserLevel.None);

AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");

MessageBox.Show(appSettings.Settings["server"].Value);

appSettings.Settings["server"].Value = "NewServer";

config.Save();


, NotifyIcon


Visible false

Deactivate
, ,

private void Form1_Deactivate(object sender, EventArgs e)


{
if (this.WindowState == FormWindowState.Minimized)
{
this.ShowInTaskbar = false;

notifyIcon1.Visible = true;
}
}

private void notifyIcon1_Click(object sender, EventArgs e)


{
if (this.WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
notifyIcon1.Visible = false;
}
}