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

WINDOWS FORMS

1. : [1] 25
2. . . # 2005
3. .., ...
C#.

2.
, TextBox, RichTextBox, ListBox, ComboBox,
CheckListBox, DomainUpDown, NumericUpDown,
DateTimePicker, MonthCalendar, DataGridView


- OpenFileDialog
.

Form1():
OpenFileDialog openFileDialog1 = new OpenFileDialog();
:
private void button1_Click(object sender, System.EventArgs e)
{
Stream myStream;

// OpenFileDialog openFileDialog1 = new OpenFileDialog();


openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK &&
openFileDialog1.FileName.Length > 0)
{
myStream = openFileDialog1.OpenFile(); //
if (myStream != null)
{
// .
myStream.Close();
}
}
}

OpenFileDialog:
InitialDirectory -
, .
DefaultExt -
.
Filter -
,
.
FilterIndex - ,

.
RestoreDirectory - bool ,
,
.

OpenFile() -
.
FileName.
FileName.Length .

- SaveFileDialog
VS:
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- OpenFileDialog.
//
saveFileDialog1.DefaultExt = "*.rtf";
saveFileDialog1.Filter = "RTF Files|*.rtf";
if (saveFileDialog1.ShowDialog() == DialogResult.OK &&
saveFileDialog1.FileName.Length > 0)
{
//
// saveFileDialog1.FileName
}

- FontDialog

.

. label1.
private void _Click(object sender, EventArgs e)
{
fontDialog1.ShowColor = true; //
fontDialog1.Font = label1.Font;
fontDialog1.Color = label1.ForeColor;

//
//

if ( fontDialog1.ShowDialog() == DialogResult.OK )
{
label1.Font = fontDialog1.Font;
//
label1.ForeColor = fontDialog1.Color; //
}
}

- ColorDialog
,
.

private void 1_Click(object sender, EventArgs e)


{
colorDialog1.Color = color1;
//
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
color1 = colorDialog1.Color; //
}
}

TextBox
TextBox
, .
,

.
TextBox
.

RichTextBox.
, ,
Text.
2048 .
MultiLine true,
32 .

textBox1.Text = " ";


SelectionStart
, 0
.

SelectionLength

.

textBox1 Enter.
private void textBox1_Enter (object sender, EventArgs e)
{
textBox1.SelectionStart = 0;
textBox1.SelectionLength = textBox1.Text.Length;
str = textBox1.SelectedText;
}
TextLength
,
, ,
.


Windows Forms
TextBox
.

, .

,
MultiLine, WordWrap
ScrollBars.
WordWrap - ,


.
AcceptsReturn

,
,

TextBox:
(= true)

(=false).
AcceptsTab - , ,
TAB
:


.

TextBox
.
" ",
,
AcceptsTab, AcceptsReturn WordWrap.
private void CreateMyMultilineTextBox()
{
TextBox textBox1 = new TextBox();
textBox1.Multiline = true;

textBox1.ScrollBars = ScrollBars.Vertical;
textBox1.AcceptsReturn = true;
textBox1.AcceptsTab = true;
textBox1.WordWrap = true;
textBox1.Text = "Welcome!";
}


private void InitializeTextBox()
{
// .
textBox1.Text = "";
// .
textBox1.PasswordChar = '*';
// 14 .
textBox1.MaxLength = 14;
}

.
public virtual int TextLength {get;} .
1. AppendText() - .

textBox1.AppendText (RichTextBox1.SelectedText);
2.

textBox1.Text += " ";


textBox1.Text += " ";
3. ( Multiline = true).

textBox1.Text = " 1\r\n 2\r\n 3\r\n";


textBox1.Text += " 4\r\n";

RichTextBox
RichTextBox

, ,
Microsoft Word.
RichTextBox ,
TextBox,
:
, ,
,
,
.
RichTextBox, TextBox,
, TextBox
, ,
.
RichTextBox ,

.

,
.
.
, , ,
,
,
.
SelectionFont
.

,
.
SelectionColor .
SelectionBullet
.

SelectionIndent, SelectionRightIndent
SelectionHangingIndent.

RichTextBox
,
RTF.
LoadFile().
LoadFile()
.
SaveFile()
. SaveFile()
,
LoadFile().
. btnOpenFile
.
richTextBox1.LoadFile().
private void btnOpenFile_Click(object sender, System.EventArgs e)
{
if(openFileDialog1.ShowDialog() == DialogResult.OK)
richTextBox1.LoadFile (openFileDialog1.FileName); // RTF
}



RichTextBox
C:\MyDocument.RTF
.
,

C:\MyDocument2.RTF.

.
private void Form1_Load (object sender, EventArgs e)
{
richTextBox1.LoadFile ("C:\\MyDocument.RTF");
richTextBox1.Find ( "", RichTextBoxFinds.MatchCase);
richTextBox1.SelectionFont = new Font ("Verdana", 12,
FontStyle.Bold);
richTextBox1.SelectionColor = Color.Red;
richTextBox1.SaveFile ("C:\\MyDocument2.RTF",
RichTextBoxStreamType.RichText);
}

RichTextBoxStreamType:

PlainText
RichNoOleObjs
RichText
TextTextOleObjs

UnicodePlainText

OLE.
RTF OLE.

SaveFile RichTextBox.
RTF.

OLE.
SaveFile
RichTextBox.
.
OLE.

MyDocument.rtf :

RichTextBox, RTF

.
,

RTF C.
:

SaveFile()
,
ASCII,
RTF.
SaveFileDialog saveFile1 = new SaveFileDialog();
saveFile1.DefaultExt = "*.rtf";
saveFile1.Filter = "RTF Files|*.rtf";
if(saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK
&& saveFile1.FileName.Length > 0)
{
// ASCII
richTextBox1.SaveFile (saveFile1.FileName,
RichTextBoxStreamType.PlainText);
}

.
TextBox:
richTextBox1.AppendText (textBox1.SelectedText);
richTextBox1.Text += " ";
\r\n \n:
richTextBox1.Text = " 1\n 2\n";
richTextBox1.Text += " 3\n";
2

ListBox,
ComboBox CheckListBox
: ComboBox, ListBox CheckListBox

ListBox
,
.
ComboBox
.
ComboBox
:
,
.
,
,
.

CheckedListBox
ListBox.
, ,
.

,
DrawMode.Normal;

. ,

.

ListBox
.

, ListBox
.
MultiColumn true,



.
MultiColumn false,

.
ScrollAlwaysVisible true,
.
.

SelectionMode ,
(,
: (MultiSimple)
(MultiExtended) ).

SelectedIndex

,
.

, SelectedIndex;

Windows.

SelectedIndex -1.

,
SelectedIndex 0.
,
SelectedIndex ,
.
SelectedItem SelectedIndex,
,
.
BeginUpdate() EndUpdate()
ListBox ,

,
EndUpdate().
FindString() FindStringExact()
, .

Items,
SelectedItems

SelectedIndices
,
ListBox.

Items ListBox.
public ListBox.ObjectCollection Items {get;}
public class ListBox.ObjectCollection : IList, ICollection, IEnumerable
SelectedItems ListBox.
public ListBox.SelectedObjectCollection SelectedItems {get;}
public class ListBox.SelectedObjectCollection : IList, ICollection, IEnumerable
SelectedIndices
ListBox.
public ListBox.SelectedIndexCollection SelectedIndices {get;}
public class ListBox.SelectedIndexCollection : IList, ICollection, IEnumerable

,
ListBox.
ListBox.SelectedObjectCollection
,
,
ListBox.
ListBox.SelectedIndexCollection
,

ListBox.ObjectCollection.

ListBox.ObjectCollection

Items

ListBox.ObjectCollection

11

22

SelectedItems

ListBox.SelectedObjectCollection ListBox.SelectedIndexCollection

33

44

55

66

SelectedIndices

22

44

66

11
33

55

Items.Count .

SelectedIndex, SelectedIndex
.

ListBox,
Items.Add(), Items.Insert(), Items.Clear()
Items.Remove().
, Items
.

Add()
ListBox.
ListBox
, ToString()
,
DisplayMember.

Add()

ListBox.ObjectCollection,
DataSource ListControl (. ).

1.

ListBox, .
.
, , 50 ListBox
Add ListBox.ObjectCollection,
SetSelected.
ListBox.SelectedObjectCollection (
SelectedItems) ListBox.SelectedIndexCollection (
SelectedIndices). , Form
.
listBox1.SelectionMode = SelectionMode.MultiSimple; // VS
private void button1_Click_1 (object sender, EventArgs e)
{

listBox1.BeginUpdate();
for (int x = 1; x <= 20; x++)

// .
// 50

listBox1.Items.Add(" " + x);


}

listBox1.EndUpdate();

//

// 1, 3 5
listBox1.SetSelected(1, true);
listBox1.SetSelected(3, true);
listBox1.SetSelected(5, true);
// [1] [0]
richTextBox1.Text = listBox1.SelectedItems[1] + "\n";
richTextBox1.Text += listBox1.SelectedIndices[0];
}
SetSelected() - ListBox
.

2 .
ListBox PhoneList,
string name, phone ;
. , ,
, ?

,
DisplayMember. ,
, ToString().
.
. DisplayMember
,
.
, .
SelectedItem ,
.
,
.
using System;
using System.Collections.Generic;
using System.Text;

namespace Ctrl_ListBox
{
class PhoneList
{
string name, phone ;
public PhoneList (string n, string p)
{
name = n;
phone = p;
}
public string Name
{
get { return name; }
}
public string Phone
{
get { return phone; }
}
public override string ToString()
{
return Phone;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
listBox1.DisplayMember = "Name";
listBox1.BeginUpdate();
// , DisplayMember

listBox1.Items.Add (new PhoneList ("", "123-45-67"));


listBox1.Items.Add (new PhoneList ("", "223-45-68"));
listBox1.Items.Add (new PhoneList ("", "323-45-69"));
listBox1.Items.Add (new PhoneList ("", "423-45-00"));
listBox1.Items.Add (new PhoneList ("", "523-55-67"));

listBox1.Items.Add (new PhoneList ("", "623-66-67"));


listBox1.Items.Add (new PhoneList ("", "723-77-67"));
listBox1.Items.Add (new PhoneList ("", "823-88-67"));
listBox1.Items.Add (new PhoneList ("", "923-99-67"));
listBox1.EndUpdate();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
PhoneList obj;
obj = (PhoneList) listBox1.SelectedItem;
richTextBox1.Text = obj.Name + "\n" ;
richTextBox1.Text += obj.Phone + "\n\n " ;
// , ToString(), .. Phone.

richTextBox1.Text += listBox1.SelectedItem.ToString() + "\n";


richTextBox1.Text += listBox1.SelectedIndex.ToString() + "\n";
// ,
// DataSource
// listBox1.ValueMember = "Phone";
// richTextBox1.Text = listBox1.SelectedValue + "\n";

}
}
}
:

1. ,
.
obj = (PhoneList) listBox1.SelectedItem;
2. ToString() ,
: +=, +.
3.

listBox1.DisplayMember = "Name";

listBox1.DisplayMember = "Phone";

ListBox ComboBox
ListBox ComboBox
.

,
.
DataSource -
ListControl.
SelectedValue -
, ValueMember.
ValueMember - ,
,
.
ListBox
ArrayList
DataSource:
listBox1.DataSource = arrayList;

,
IList.
IBindingList,
ListBox.
1. , ToString()
.
2. ,
listBox1.ValueMember = "Phone";
, PhoneList
SelectedValue:
richTextBox1.Text += listBox1.SelectedValue + "\n";
3. listBox1.DataSource = arrayList
SelectedIndexChanged ,
listBox1_SelectedIndexChanged
listBox1_Click.

private void button1_Click(object sender, EventArgs e)


{
listBox1.DataSource = null; //
ArrayList arrayList = new ArrayList();
arrayList.Add (new PhoneList ("", "123-45-67"));
arrayList.Add (new PhoneList ("", "223-45-68"));
arrayList.Add (new PhoneList ("", "323-45-69"));
arrayList.Add (new PhoneList ("", "423-45-00"));
arrayList.Add (new PhoneList ("", "523-55-67"));
arrayList.Add (new PhoneList ("", "623-66-67"));
arrayList.Add (new PhoneList ("", "723-77-67"));
arrayList.Add (new PhoneList ("", "823-88-67"));
arrayList.Add (new PhoneList ("", "923-99-67"));
listBox1.Items.Clear();
listBox1.DataSource = arrayList;
listBox1.DisplayMember = "Name";

}
private void listBox1_Click(object sender, EventArgs e)
{
PhoneList obj;
obj = (PhoneList)listBox1.SelectedItem;
richTextBox1.Text = obj.Name + " ";
richTextBox1.Text += obj.Phone + "\n\n";
richTextBox1.Text += listBox1.SelectedItem + "\n"; //.ToString()
richTextBox1.Text += listBox1.SelectedIndex + "\n"; //.ToString()
listBox1.ValueMember = "Phone";
richTextBox1.Text += listBox1.SelectedValue + "\n";
}

arrayList

PhoneList

DisplayMember = "Name"
ListBox

Name
Name Phone
Phone

Name
Name

ValueMember = "Phone"

DataSource = arrayList
SelectedValue

Windows Forms ,
.
, ,
, .


comboBox1.Items[i].ToString();


//
comboBox1.Items.Add ("Tokyo");
// 0:
checkedListBox1.Items.Insert (0, "Copenhagen");
// Items :
object[] ItemObject = new object[10];
for (int i = 0; i <= 9; i++)
ItemObject[i] = "Item" + i;
listBox1.Items.AddRange(ItemObject);


// 0:
comboBox1.Items.RemoveAt(0);
// :
comboBox1.Items.Remove(comboBox1.SelectedItem);
// "Tokyo":
comboBox1.Items.Remove("Tokyo");
//
listBox1.Items.Clear();

ComboBox

, .

,
, , ,
DropDownStyle.

DropDownStyle
:
DropDownn
.

,
.
DropDownList -
.
.
Simple ,
.

CheckedListBox
CheckedListBox :
,
CheckedItems,

GetItemChecked, ,
.
GetItemChecked
true false.
!!! SelectedItems SelectedIndices
: ,
.

1.
CheckedItems , 0,
. ,
,
.
, ,
: " 1
= 2".
if ( checkedListBox1.CheckedItems.Count != 0 )
{
string s = "";
for ( int x = 0; x < checkedListBox1.CheckedItems.Count ; x++)
{
s = s + "\n " + (x+1) + " = "
+ checkedListBox1.CheckedItems[x];
}
MessageBox.Show (s);
}

2.
Items , 0,
, GetItemChecked()
. ,
; ,
, :
" 2 = 2".
string s = " :\n" ;
for ( int i = 0; i < checkedListBox1.Items.Count ; i++ )
{
if ( checkedListBox1.GetItemChecked(i) == true)
s = s + "\n " + (i+1) + " = " + checkedListBox1.Items[i] ;
}
MessageBox.Show (s);

DomainUpDown
DomainUpDown
, Object
/
.


, ReadOnly ,
true ( ,
).
:
Items.
SelectedIndex -
.
SelectedItem -
,
, .

:
MessageBox.Show ( "SelectedIndex: " + domainUpDown1.SelectedIndex + "\n" +
"SelectedItem: " + domainUpDown1.SelectedItem);

,
DomainUpDown, :

Items.Add()
Items.Remove().


, Items ...

// domainUpDown1.

. -

this.domainUpDown1.Items.Add (" -1");


this.domainUpDown1.Items.Add (" -2");
this.domainUpDown1.Items.Add (" -3");
this.domainUpDown1.Name = "domainUpDown1";
this.domainUpDown1.Text = " -1";
this.domainUpDown1.TabIndex = 0;
this.domainUpDown1.Location = new System.Drawing.Point (96, 48);

:

, Sorted true.

Wrap true,
(

).


/ ToString() .
:
public class UDC
{
int a, b;
public UDC (int A, int B)
{ a=A; b=B;}
public override string ToString()
{ return " " + a + "+" + b; }
}

UDC udc;
DomainUpDown domainUpDown1 ;
// . . .
private void InitializeComponent()
{
domainUpDown1 = new DomainUpDown();
SuspendLayout();
//
// domainUpDown1
//
udc = new UDC(0, 10);
domainUpDown1.Items.Add (udc);
udc = new UDC(1, 11);
domainUpDown1.Items.Add (udc);

udc = new UDC(2, 12);


domainUpDown1.Items.Add (udc);
udc = new UDC(3, 13);
domainUpDown1.Items.Add (udc);
domainUpDown1.Location = new System.Drawing.Point(96, 48);
domainUpDown1.Name = "domainUpDown1";
domainUpDown1.TabIndex = 0;
domainUpDown1.Text = " -1";

//
// Form1
// . . .
UpButton ( ) DownButton
( )
UpdateEditText,
.
UserEdit true,
.


DomainUpDown.
,
/.
, TextBox, CheckBox Button
. ,
, 32-
myCounter.
Items .
Sorted,
/.

// :
protected DomainUpDown domainUpDown1;
private void MySub()
{
// DomainUpDown

domainUpDown1 = new System.Windows.Forms.DomainUpDown();


// DomainUpDown
Controls.Add(domainUpDown1);
}
private void button1_Click (Object sender, System.EventArgs e)
{
// TextBox DomainUpDown
domainUpDown1.Items.Add ((textBox1.Text.Trim()) + " - " + myCounter);
myCounter = myCounter + 1;

//

textBox1.Text = "";

// TextBox.

}
private void checkBox1_Click(Object sender, System.EventArgs e)
{
// , ,

if (domainUpDown1.Sorted)
domainUpDown1.Sorted = false;
else
domainUpDown1.Sorted = true;
}
void domainUpDown1_SelectedItemChanged(Object s, EventArgs e)
{
// SelectedIndex and SelectedItem
MessageBox.Show(
"SelectedIndex: " + domainUpDown1.SelectedIndex+ "\n" +
"SelectedItem: " + domainUpDown1.SelectedItem);
}

NumericUpDown
NumericUpDown
,
,
. ,
ReadOnly true.

, Minimum
Maximum.
Value .
.
Increment - ,
.


, Hexadecimal true.

, ThousandsSeparator
true.
,
,
DecimalPlaces ,
.
UpButton DownButton, , ,
, , ,
. , UserEdit true,
ParseEditText . ,
Minimum Maximum,
UpdateEditText.



NumericUpDown, ,
.
,
CheckBox, Click.
DecimalPlaces, ThousandsSeparator Hexadecimal Click
.
public void InstantiateMyNumericUpDown()
{
// Create and initialize a NumericUpDown control.
numericUpDown1 = new NumericUpDown();
// Dock the control to the top of the form.
numericUpDown1.Dock = System.Windows.Forms.DockStyle.Top;
// Set the Minimum, Maximum, and initial Value.
numericUpDown1.Value = 5;
numericUpDown1.Maximum = 2500;
numericUpDown1.Minimum = -100;
// Add the NumericUpDown to the Form.
Controls.Add(numericUpDown1);
}

// Check box to toggle decimal places to be displayed.


private void checkBox1_Click(Object sender, EventArgs e)
{
/* If DecimalPlaces is greater than 0, set them to 0 and round the
current Value; otherwise, set DecimalPlaces to 2 and change the
Increment to 0.25. */
if (numericUpDown1.DecimalPlaces > 0)
{
numericUpDown1.DecimalPlaces = 0;
numericUpDown1.Value = Decimal.Round(numericUpDown1.Value, 0);
}
else
{
numericUpDown1.DecimalPlaces = 2;
numericUpDown1.Increment = 0.25M;
}
}
// Check box to toggle thousands separators to be displayed.

private void checkBox2_Click(Object sender, EventArgs e)


{
/* If ThousandsSeparator is true, set it to false;
otherwise, set it to true. */
if (numericUpDown1.ThousandsSeparator)
{
numericUpDown1.ThousandsSeparator = false;
}
else
{
numericUpDown1.ThousandsSeparator = true;
}
}

// Check box to toggle hexadecimal to be displayed.


private void checkBox3_Click(Object sender, EventArgs e)
{
/* If Hexadecimal is true, set it to false;
otherwise, set it to true. */
if (numericUpDown1.Hexadecimal)
{
numericUpDown1.Hexadecimal = false;
}
else
{
numericUpDown1.Hexadecimal = true;
}
}

DateTimePicker

MonthCalendar

DateTimePicker

.
MonthCalendar DateTimePicker.
.
ShowCheckBox
=true

ShowUpDown =

MonthCalenda
r

Format = Time

true

Value /,
.
Value
,
(DateTime.Now).

:
public DateTime Value {get; set;}

:
dateTimePicker1.Value = DateTime.Now.AddDays(1);
dateTimePicker2.Value = new System.DateTime(2007, 5, 13, 14, 34, 8, 0);
AddDays(1) - .


MinDate
MaxDate.
,
CalendarForeColor, CalendarFont, CalendarTitleBackColor,
CalendarTitleForeColor, CalendarTrailingForeColor CalendarMonthBackground.

Format DateTimePickerFormat
.
Format
DateTimePickerFormat.Long.
: Short, Time, Custom.
Format
DateTimePickerFormat.Custom,
CustomFormat
.
, CustomFormat
"ddMMMMyyyy HH:mm:ss",
: 13 2007 . 11:52:05.

,
ShowUpDown true.
, .
/

.



DateTimePicker MonthCalendar.
DateTimePicker,
/ .
. DateTimePicker
.


DateTimePicker .
CustomFormat . ,
ShowCheckBox ,
CheckBox, ShowUpDown
,
.
public void CreateMyDateTimePicker()
{
// DateTimePicker dateTimePicker1 = new DateTimePicker();
// .
dateTimePicker1.MinDate = new DateTime(1985, 6, 20);
dateTimePicker1.MaxDate = DateTime.Today;
//
dateTimePicker1.CustomFormat = "ddMMMMyyyy HH:mm:ss";
dateTimePicker1.Format =
System.Windows.Forms.DateTimePickerFormat.Custom;
// CheckBox up-down control.
dateTimePicker1.ShowCheckBox = true;
dateTimePicker1.ShowUpDown = true;
}

. 6 :
CalendarDimensions.Width = 3;
CalendarDimensions.Height = 2;

// . 1

SelectionStart

SelectionEnd

MaxSelectionCount .

. monthCalendar1:
DateTime dt = monthCalendar1.SelectionStart;
string = dt.Day + "." + dt.Month + "." + dt.Year;
SelectionStart SelectionEnd DateTime.

Day
Mouhth
Year
Millisecond
Second
Minute
Hour
DayOfWeek
DayOfYear
Ticks
TimeOfDay
Today
UtcNow







(coordinated universal time, UTC)