вариант №1

advertisement
ФЕДЕРАЛЬНОЕ ГОСУДАРСТВЕННОЕ АВТОНОМНОЕ ОБРАЗОВАТЕЛЬНОЕ УЧРЕЖДЕНИЕ ВЫСШЕГО
ПРОФЕССИОНАЛЬНОГО ОБРАЗОВАНИЯ
БЕЛГОРОДСКИЙ ГОСУДАРСТВЕННЫЙ НАЦИОНАЛЬНЫЙ
ИССЛЕДОВАТЕЛЬСКИЙ УНИВЕРСИТЕТ
(НИУ «БелГУ»)
ФАКУЛЬТЕТ КОМПЬЮТЕРНЫХ НАУК И ТЕЛЕКОММУНИКАЦИЙ
КАФЕДРА МАТЕМАТИЧЕСКОГО И ПРОГРАММНОГО ОБЕСПЕЧЕНИЯ
ИНФОРМАЦИОННЫХ СИСТЕМ
Отчет по лабораторной работе №10
студента дневного отделения 4 курса группы 140901
Батищева Дениса Сергеевича
вариант №1
Редактор текста.
Проверили:
Сунцова А.И., Великая Я.Г.
Оценка:
__________ дата _________
Подпись:
________________________
БЕЛГОРОД 2012
Порядок выполнения работы 1.
1. Создаем новый проект Windows Forms.
2. Добавляем в проект еще одну форму для подтверждения
изменений.
3. Добавляем на форму 1 RichTextBox, 2 ToolStrip, 1 Menu strip
4. Добавляем в тулстрипы 6 и 2 кнопки соответственно, в
менюстрип 3 основных кнопки и 8, 3 и 2 родительских
соответственно.
5. Программируем обработчики для всех кнопок и для изменения
текста ричтекстбокса
6. Пишем отчет по перовой части.
Результаты выполнения работы
Рис. 1. Работа приложения 1.
Листинг работы
using
using
using
using
using
using
using
using
System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
namespace IDE_Lab10.a {
public partial class Form1 : Form {
public Form1( ) {
InitializeComponent( );
}
private Form2 form2 = new Form2( );
private
private
private
private
private
StatusBar statusBar1;
static StatusBarPanel statusBar1PanelL;
static StatusBarPanel statusBar1PanelM;
static StatusBarPanel statusBar1PanelR;
static StatusBarPanel[ ] sbpa;
private OpenFileDialog openFileDialog1;
private SaveFileDialog saveFileDialog1;
private string saveFilePath;
private FontDialog fontDialog1;
private ColorDialog colorDialog1;
private
private
private
private
PageSetupDialog pageSetupDialog1;
PrintDialog printDialog1;
PrintPreviewDialog printPreviewDialog1;
System.Drawing.Printing.PrintDocument documentToPrint1;
private bool textIsChanged, changesIsSaved;
private List<string> wordsList;
private int symbolsCount, wordsCount;
private void Form1_Load( object sender, EventArgs e ) {
this.statusBar1 = new StatusBar( );
statusBar1PanelL = new StatusBarPanel( );
statusBar1PanelM = new StatusBarPanel( );
statusBar1PanelR = new StatusBarPanel( );
sbpa = new StatusBarPanel[ ] { statusBar1PanelL, statusBar1PanelM,
statusBar1PanelR };
this.openFileDialog1 = new OpenFileDialog( );
this.saveFileDialog1 = new SaveFileDialog( );
this.saveFilePath = "";
this.fontDialog1 = new FontDialog( );
this.colorDialog1 = new ColorDialog( );
pageSetupDialog1 = new PageSetupDialog( );
printDialog1 = new PrintDialog( );
printPreviewDialog1 = new PrintPreviewDialog( );
documentToPrint1 = new System.Drawing.Printing.PrintDocument( );
documentToPrint1.PrintPage += new
System.Drawing.Printing.PrintPageEventHandler( documentToPrint1_PrintPage );
this.textIsChanged = false;
this.changesIsSaved = true;
this.wordsList = new List<string>( );
this.wordsCount = this.symbolsCount = 0;
}
private void Form1_Shown( object sender, EventArgs e ) {
statusBar1PanelL.AutoSize = statusBar1PanelM.AutoSize =
statusBar1PanelR.AutoSize = StatusBarPanelAutoSize.Spring;
statusBar1.ShowPanels = true;
statusBar1.SetBounds( 0, 0, this.Size.Width, 24 );
statusBar1.Panels.AddRange( sbpa );
this.Controls.Add( statusBar1 );
this.richTextBox1_TextChanged( null, null );
}
private void toolStripMenuItem11_Click( object sender, EventArgs e ) {
richTextBox1.Cut( );
}
private void toolStripMenuItem12_Click( object sender, EventArgs e ) {
richTextBox1.Copy( );
}
private void toolStripMenuItem13_Click( object sender, EventArgs e ) {
richTextBox1.Paste( );
}
private void toolStripMenuItem17_Click( object sender, EventArgs e ) {
this.toolStripMenuItem11_Click( sender, e );
}
private void toolStripMenuItem18_Click( object sender, EventArgs e ) {
this.toolStripMenuItem12_Click( sender, e );
}
private void toolStripMenuItem19_Click( object sender, EventArgs e ) {
this.toolStripMenuItem13_Click( sender, e );
}
private void toolStripMenuItem20_Click( object sender, EventArgs e ) {
if ( this.fontDialog1.ShowDialog( ) == DialogResult.OK )
this.richTextBox1.Font = fontDialog1.Font;
}
private void richTextBox1_TextChanged( object sender, EventArgs e ) {
this.textIsChanged = true && ( !this.changesIsSaved );
this.changesIsSaved = false;
this.wordsList.AddRange( this.richTextBox1.Text.Split( Char.Parse("
") ) );
this.wordsCount = 0;
foreach ( string word in wordsList )
if ( word != "" )
this.wordsCount++;
this.wordsList.Clear( );
statusBar1PanelM.Text = String.Format( "Бж {0}",
this.richTextBox1.Text.Length );
statusBar1PanelR.Text = String.Format( "Дж {0}", this.wordsCount );
this.Text = "Form1 " + ( ( textIsChanged ) ? "*" : "" );
}
private void toolStripMenuItem5_Click( object sender, EventArgs e ) {
if ( !this.textIsChanged ) {
if ( this.openFileDialog1.ShowDialog( ) == DialogResult.OK ) {
this.richTextBox1.LoadFile( openFileDialog1.FileName,
RichTextBoxStreamType.PlainText );
this.changesIsSaved = true;
}
}
else {
DialogResult dr = form2.ShowDialog( );
if ( dr == DialogResult.OK ) {
if ( this.saveFileDialog1.ShowDialog( ) ==
DialogResult.OK ) {
richTextBox1.SaveFile(
this.saveFileDialog1.FileName, RichTextBoxStreamType.PlainText );
if ( this.openFileDialog1.ShowDialog( ) ==
DialogResult.OK ) {
this.richTextBox1.LoadFile(
openFileDialog1.FileName );
this.changesIsSaved = true;
}
}
}
else if ( dr == DialogResult.Cancel ) {
if ( this.openFileDialog1.ShowDialog( ) ==
DialogResult.OK ) {
this.richTextBox1.LoadFile(
openFileDialog1.FileName, RichTextBoxStreamType.PlainText );
this.changesIsSaved = true;
}
}
else
this.changesIsSaved = false;
}
this.saveFilePath = ( this.openFileDialog1.FileName != "" ) ?
this.openFileDialog1.FileName : this.saveFilePath;
this.richTextBox1_TextChanged( sender, e );
}
private void toolStripMenuItem6_Click( object sender, EventArgs e ) {
if ( this.saveFilePath == "" )
if ( this.saveFileDialog1.ShowDialog( ) == DialogResult.OK )
this.saveFilePath = this.saveFileDialog1.FileName;
if ( this.saveFilePath != "" ) {
this.richTextBox1.SaveFile( saveFilePath,
RichTextBoxStreamType.PlainText );
this.changesIsSaved = true;
}
this.richTextBox1_TextChanged( sender, e );
}
private void toolStripMenuItem4_Click( object sender, EventArgs e ) {
if ( !this.textIsChanged ) {
this.richTextBox1.Text = "";
this.changesIsSaved = true;
}
else {
DialogResult dr = form2.ShowDialog( );
if ( dr == DialogResult.OK ) {
if ( this.saveFileDialog1.ShowDialog( ) ==
DialogResult.OK ) {
richTextBox1.SaveFile(
this.saveFileDialog1.FileName, RichTextBoxStreamType.PlainText );
this.richTextBox1.Text = "";
this.changesIsSaved = true;
}
}
else if ( dr == DialogResult.Cancel ) {
this.richTextBox1.Text = "";
this.changesIsSaved = true;
}
}
this.richTextBox1_TextChanged( sender, e );
}
private void toolStripMenuItem7_Click( object sender, EventArgs e ) {
this.pageSetupDialog1.Document = this.documentToPrint1;
this.pageSetupDialog1.PageSettings = new
System.Drawing.Printing.PageSettings( );
this.pageSetupDialog1.PrinterSettings = new
System.Drawing.Printing.PrinterSettings( );
this.pageSetupDialog1.ShowDialog( );
}
private void toolStripMenuItem15_Click( object sender, EventArgs e ) {
this.toolStrip1.Visible = !this.toolStrip1.Visible;
}
private void toolStripMenuItem16_Click( object sender, EventArgs e ) {
this.toolStrip2.Visible = !this.toolStrip2.Visible;
}
private void toolStripMenuItem10_Click( object sender, EventArgs e ) {
this.Close( );
}
private void Form1_FormClosing( object sender, FormClosingEventArgs e ) {
if ( this.textIsChanged ) {
DialogResult dg = this.form2.ShowDialog( );
if ( dg == DialogResult.OK ) {
this.toolStripMenuItem6_Click( sender, e );
this.Close( );
}
else if ( dg == DialogResult.Abort ) {
e.Cancel = true;
}
}
}
private void toolStripMenuItem9_Click( object sender, EventArgs e ) {
this.printDialog1.PrinterSettings = new
System.Drawing.Printing.PrinterSettings( );
this.printDialog1.Document = new
System.Drawing.Printing.PrintDocument( );
if ( this.printDialog1.ShowDialog( ) == DialogResult.OK )
this.documentToPrint1.Print( );
}
private void documentToPrint1_PrintPage( object sender,
System.Drawing.Printing.PrintPageEventArgs e ) {
e.Graphics.DrawString( this.richTextBox1.Text,
this.richTextBox1.Font,
System.Drawing.Brushes.Black, 10, 10 );
}
private void toolStripMenuItem8_Click( object sender, EventArgs e ) {
this.printPreviewDialog1.Document = this.documentToPrint1;
this.printPreviewDialog1.ShowDialog( );
}
private void toolStripButton1_Click( object sender, EventArgs e ) {
this.toolStripMenuItem4_Click( sender, e );
}
private void toolStripButton2_Click( object sender, EventArgs e ) {
this.toolStripMenuItem5_Click( sender, e );
}
private void toolStripButton3_Click( object sender, EventArgs e ) {
this.toolStripMenuItem6_Click( sender, e );
}
private void toolStripButton4_Click( object sender, EventArgs e ) {
this.toolStripMenuItem9_Click( sender, e );
}
private void toolStripButton5_Click( object sender, EventArgs e ) {
this.toolStripMenuItem8_Click( sender, e );
}
private void toolStripButton6_Click( object sender, EventArgs e ) {
if ( this.fontDialog1.ShowDialog( ) == DialogResult.OK )
this.richTextBox1.Font = this.fontDialog1.Font;
}
private void toolStripButton7_Click( object sender, EventArgs e ) {
if ( this.colorDialog1.ShowDialog( ) == DialogResult.OK )
this.richTextBox1.ForeColor = colorDialog1.Color;
}
}
}
Листинг 1. Листинг программы. Файл
Задание 2.
<Window x:Class="IDE_Lab10.b.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:w="clr-namespace:IDE_Lab10.b"
Title="MainWindow" mc:Ignorable="d"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="348" d:DesignWidth="612" SizeToContent="WidthAndHeight">
<Window.CommandBindings>
<CommandBinding Command="w:FCK.ths" Executed="exitCommand_Executed" />
<CommandBinding Command="w:FCK.tht" Executed="wordWrapCommand_Executed" />
</Window.CommandBindings>
<DockPanel >
<Menu DockPanel.Dock="Top" Height="auto">
<MenuItem Header="File">
<MenuItem Header="_Exit" Command="w:FCK.ths" />
</MenuItem>
<MenuItem Header="Edit">
<MenuItem Header="_Copy" Command="Copy" />
<MenuItem Header="_Cut" Command="Cut"/>
<MenuItem Header="_Paste" Command="Paste"/>
</MenuItem>
<MenuItem Header="View">
<MenuItem Name="wordWrap" Header="_WordWrap" />
</MenuItem>
</Menu>
<ToolBarTray DockPanel.Dock="Top">
<ToolBar>
<Button Name="button1" Width="25" Height="20" Command="Copy">
ДЖ
</Button>
<Button Name="button2" Width="25" Height="20" >
БЖ
</Button>
<CheckBox Name="checkbox1" IsChecked="{Binding
ElementName=wordWrap, Mode=TwoWay, Path=IsChecked}" Command="w:FCK.tht">
Клац
</CheckBox>
</ToolBar>
</ToolBarTray>
<TextBox Name="textBox1" DockPanel.Dock="Top" AcceptsReturn="True"
AcceptsTab="True" HorizontalScrollBarVisibility="Auto"
HorizontalContentAlignment="Stretch" VerticalScrollBarVisibility="Auto" Height="260" />
<StatusBar DockPanel.Dock="Bottom">
<TextBlock>
<TextBlock Text="{Binding ElementName=textBox1,
Path=Text.Length}" /> ДЖ
</TextBlock>
</StatusBar>
</DockPanel>
</Window>
using
using
using
using
using
using
using
using
using
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Windows;
System.Windows.Controls;
System.Windows.Data;
System.Windows.Documents;
System.Windows.Input;
System.Windows.Media;
System.Windows.Media.Imaging;
System.Windows.Navigation;
System.Windows.Shapes;
namespace IDE_Lab10.b {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public static CommandBinding exitCommand;
public static CommandBinding wordWrapCommand;
public MainWindow( ) {
InitializeComponent( );
MainWindow.exitCommand = new CommandBinding( new RoutedUICommand(
"Exit", "Exit", typeof( MainWindow ) ) );
MainWindow.exitCommand.Executed += new ExecutedRoutedEventHandler(
exitCommand_Executed );
MainWindow.wordWrapCommand = new CommandBinding( new RoutedUICommand(
"Wrap", "Wrap", typeof( MainWindow ) ) );
MainWindow.wordWrapCommand.Executed += new
ExecutedRoutedEventHandler( wordWrapCommand_Executed );
this.CommandBindings.Add( MainWindow.exitCommand );
this.CommandBindings.Add( MainWindow.wordWrapCommand );
}
private void exitCommand_Executed( object sender, ExecutedRoutedEventArgs e
) {
this.Close( );
}
private void wordWrapCommand_Executed( object sender,
ExecutedRoutedEventArgs e ) {
textBox1.TextWrapping = (textBox1.TextWrapping ==
TextWrapping.NoWrap) ? TextWrapping.Wrap : TextWrapping.NoWrap ;
}
}
public static
public
typeof( MainWindow )
public
typeof( MainWindow )
}
}
class FCK {
static RoutedUICommand ths = new RoutedUICommand( "Exit", "Exit",
);
static RoutedUICommand tht = new RoutedUICommand( "Wrap", "Wrap",
);
Листинг 1. Листинг программы. Файл
Вывод
Узнал о xaml. Много думал о Microsoft.
Download