PhoneBook Lab13.
1- . (. 13.1).
Label, TextBox, NumericUpDown, MaskedTextBox Button.
Text Label Form1 . 13.1.
:
form1 | Name | MainForm |
FormBorderStyle | FixedSingle | |
StartPosition | CenterScreen | |
textBox1 | Name | LastNameTextBox |
textBox2 | Name | NameTextBox |
textBox3 | Name | PatronymicTextBox |
textBox4 | Name | StreetTextBox |
numericUpDown1 | Name | HouseNumericUpDown NumericUpDown NumericUpDown NumericUpDown |
Minimum | ||
Maximum | ||
numericUpDown2 | Name | FlatNumericUpDown |
Minimum | ||
Maximum | ||
maskedTextBox1 | Name | PhoneMaskedTextBox |
FormBorderStyle , FixedSingle , , StartPosition, CenterScreen, , .
MainForm. . Solution Explorer Form1.cs, , Rename MainForm.cs.
TextBox, NumericUpDown MaskedTextBox Enabled False.
Button :
Button1 | Name | PreviousButton |
Text | < | |
Font.Size | ||
Font.Bold | True | |
TabIndex | ||
Button2 | Name | NextButton |
Text | > | |
Font.Size | ||
Font.Bold | True | |
TabIndex |
Mask MaskedTextBox , . , MaskedTextBox .
PhoneMaskedTextBox, Properties Mask . Input Mask (999) 000-000 Phone Number .
( ) Font.Size 12. .
StatusStrip ( ). , MainForm.cs[Design] . , , . StatusLabel (. 13.2). .
|
|
.
toolStripStatusLabel1 | Name | Quantity_ToolStripStatusLabel |
Text | ||
BorderSides | All | |
BorderStyle | SunkenOuter | |
toolStripStatusLabel2 | Name | QuantityToolStripStatusLabel |
AutoSize | False | |
Size.Width | ||
Text | ||
BorderSides | All | |
BorderStyle | SunkenOuter | |
toolStripStatusLabel3 | Name | Number_ToolStripStatusLabel |
Text | ||
BorderSides | All | |
BorderStyle | SunkenOuter | |
toolStripStatusLabel4 | Name | NumberToolStripStatusLabel |
AutoSize | False | |
Size.Width | ||
Text | ||
BorderSides | All | |
BorderStyle | SunkenOuter |
MenuStrip (). MainMenuStrip.
, (. 13.3).
MainForm , , . , , , .
, . , Project Add Windows Form. AddForm.cs (. 13.4).
AddForm MainForm. , . Label, TextBox, NumericUpDown, MaskedTextBox MainForm < Ctrl+Insert >. AddForm < Shift+Insert >.
Text . , , TabIndex 0 6 .
AddForm . :
Button1 | Name | AddButton |
Text | ||
Font.Size | ||
Font.Bold | True | |
TabIndex |
2- . .
, LastName (), Name (), Patronymic () 25 , Street () 40 . MaxLength TextBox .
Note . Solution Explorer PhoneBook , , Add Class. Note.cs Ok.
Note ( public):
public class Note
{
public string LastName;
public string Name;
public string Patronymic;
public string Street;
public ushort House;
public ushort Flat;
public string Phone;
}
MainForm PhoneNote current, :
|
|
public partial class MainForm: Form
{
private List<Note> PhoneNote;
private int current;
MainForm PhoneNote current :
public MainForm()
{
InitializeComponent();
PhoneNote = new List<Note>();
current = -1;
}
, . current, 1, , .
PrintElement (), ( current) MainForm ( ). PrintElement () .
private void PrintElement()
{
if ((current >= 0) && (current < PhoneNote.Count))
{ //
// MyRecord - PhoneNote current
Note MyRecord = PhoneNote[current];
//
// MyRecord
LastNameTextBox.Text = MyRecord.LastName;
NameTextBox.Text = MyRecord.Name;
PatronymicTextBox.Text = MyRecord.Patronymic;
PhoneMaskedTextBox.Text = MyRecord.Phone;
StreetTextBox.Text = MyRecord.Street;
HouseNumericUpDown.Value = MyRecord.House;
FlatNumericUpDown.Value = MyRecord.Flat;
}
else // current -1, . .
{ //
LastNameTextBox.Text = "";
NameTextBox.Text = "";
PatronymicTextBox.Text = "";
PhoneMaskedTextBox.Text = "";
StreetTextBox.Text = "";
HouseNumericUpDown.Value = 1;
FlatNumericUpDown.Value = 1;
}
//
NumberToolStripStatusLabel.Text = (current + 1).ToString();
QuantityToolStripStatusLabel.Text = PhoneNote.Count.ToString();
}
, ElementAt (), ( ), . . .
Note MyRecord = PhoneNote[number];
Note MyRecord = PhoneNote.ElementAt(number);
. Properties Events Click, .
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
// - Note
Note MyRecord = new Note();
// AddForm
AddForm _AddForm = new AddForm(MyRecord);
//
_AddForm.ShowDialog();
//
current = PhoneNote.Count;
// PhoneNote - MyRecord,
// AddForm
PhoneNote.Add(_AddForm.MyRecord);
//
PrintElement();
}
. MyRecord. AddForm ( _ AddForm) MyRecord . : current (, , . . , , , ), MyRecord _ AddForm PhoneNote, PrintElement (), current, . . .
|
|
AddForm. -, . public, MainForm.
public partial class AddForm: Form
{
public Note MyRecord;
_MyRecord, Note. MyRecord.
public AddForm(Note _MyRecord)
{
InitializeComponent();
MyRecord = _MyRecord;
}
AddButton.
private void AddButton_Click(object sender, EventArgs e)
{
// -
//
MyRecord.LastName = LastNameTextBox.Text;
MyRecord.Name = NameTextBox.Text;
MyRecord.Patronymic = PatronymicTextBox.Text;
MyRecord.Phone = PhoneMaskedTextBox.Text;
MyRecord.Street = StreetTextBox.Text;
MyRecord.House = (ushort)HouseNumericUpDown.Value;
MyRecord.Flat = (ushort)FlatNumericUpDown.Value;
Close(); //
}
. . , , , .
PhoneBook . Click PreviousButton:
private void PreviousButton_Click(object sender, EventArgs e)
{
current--; // 1
PrintElement(); // current
}
: , current.
. ; , .
3.1. Click PreviousButton , , , ( !).
3.2. NextButton . , , PhoneNote. Count 1 ( !).
13.2. :
,
.
, .NET Framework :
File (, , );
Directory ;
StreamWriter ;
StreamReader .
1- . .
MainMenu , .
SaveFileDialog ( ) OpenFileDialog ( ).
2- . .
( MainForm.cs) :
using System.IO;
System.IO : File, Directory, StreamReader, StreamWriter .
|
|
( ) :
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
//
{
try //
{
// sw ( StreamWriter),
//
using (StreamWriter sw =
new StreamWriter(saveFileDialog1.FileName))
{
//
foreach (Note MyRecord in PhoneNote)
{
//
sw.WriteLine(MyRecord.LastName);
sw.WriteLine(MyRecord.Name);
sw.WriteLine(MyRecord.Patronymic);
sw.WriteLine(MyRecord.Street);
sw.WriteLine(MyRecord.House);
sw.WriteLine(MyRecord.Flat);
sw.WriteLine(MyRecord.Phone);
}
}
}
catch (Exception ex) //
{
//
MessageBox.Show(" ! : " +
ex.Message);
}
}
. ShowDialog () SaveFileDialog . OK, . sw StreamWriter, , foreach PhoneNote .
try ( ), , .
.
Note MyRecord;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
//
{
try //
{
//
using (StreamReader sr =
new StreamReader(openFileDialog1.FileName))
{
//
while (!sr.EndOfStream)
{
//
MyRecord = new Note();
//
MyRecord.LastName = sr.ReadLine();
MyRecord.Name = sr.ReadLine();
MyRecord.Patronymic = sr.ReadLine();
MyRecord.Street = sr.ReadLine();
MyRecord.House = ushort.Parse(sr.ReadLine());
MyRecord.Flat = ushort.Parse(sr.ReadLine());
MyRecord.Phone = sr.ReadLine();
//
PhoneNote.Add(MyRecord);
}
}
// , current -1,
// ( 0)
if (PhoneNote.Count == 0) current = -1;
else current = 0;
//
PrintElement();
}
catch (Exception ex) //
{
//
MessageBox.Show(" : "+
ex.Message);
}
}
. ShowDialog () OpenFileDialog . OK, . StreamReader, , while , . Note PhoneNote. current ( 0), 1, . ( current); , PrintElement () , , , .
, , .