using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Printing;
namespace ListBox_Simple
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btn_Add;
private System.Windows.Forms.Button btn_Get;
private System.Windows.Forms.ListBox lst_Fruits;
private System.Windows.Forms.Label lbl_FruitSelected;
private System.Windows.Forms.TextBox txt_NewFruit;
private System.Drawing.Printing.PrintDocument printDocumentFruit;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItemPrint;
private System.Windows.Forms.PrintPreviewDialog printPreviewDialogFruit;
private System.Windows.Forms.MenuItem menuItemPrintPreview;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.lst_Fruits = new System.Windows.Forms.ListBox();
this.btn_Add = new System.Windows.Forms.Button();
this.btn_Get = new System.Windows.Forms.Button();
this.lbl_FruitSelected = new System.Windows.Forms.Label();
this.txt_NewFruit = new System.Windows.Forms.TextBox();
this.printDocumentFruit = new System.Drawing.Printing.PrintDocument();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItemPrint = new System.Windows.Forms.MenuItem();
this.menuItemPrintPreview = new System.Windows.Forms.MenuItem();
this.printPreviewDialogFruit = new System.Windows.Forms.PrintPreviewDialog();
this.SuspendLayout();
//
// lst_Fruits
//
this.lst_Fruits.Items.AddRange(new object[] {
"Apple",
"Banana",
"Orange",
"Pear",
"Plum"});
this.lst_Fruits.Location = new System.Drawing.Point(48, 32);
this.lst_Fruits.Name = "lst_Fruits";
this.lst_Fruits.Size = new System.Drawing.Size(200, 121);
this.lst_Fruits.TabIndex = 0;
//
// btn_Add
//
this.btn_Add.Location = new System.Drawing.Point(64, 216);
this.btn_Add.Name = "btn_Add";
this.btn_Add.TabIndex = 1;
this.btn_Add.Text = "Add Item:";
this.btn_Add.Click += new System.EventHandler(this.btn_Add_Click);
//
// btn_Get
//
this.btn_Get.Location = new System.Drawing.Point(64, 248);
this.btn_Get.Name = "btn_Get";
this.btn_Get.Size = new System.Drawing.Size(128, 23);
this.btn_Get.TabIndex = 2;
this.btn_Get.Text = "Get Current Selection";
this.btn_Get.Click += new System.EventHandler(this.btn_Get_Click);
//
// lbl_FruitSelected
//
this.lbl_FruitSelected.BackColor = System.Drawing.Color.Red;
this.lbl_FruitSelected.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lbl_FruitSelected.Location = new System.Drawing.Point(72, 176);
this.lbl_FruitSelected.Name = "lbl_FruitSelected";
this.lbl_FruitSelected.Size = new System.Drawing.Size(120, 23);
this.lbl_FruitSelected.TabIndex = 3;
//
// txt_NewFruit
//
this.txt_NewFruit.Location = new System.Drawing.Point(152, 216);
this.txt_NewFruit.Name = "txt_NewFruit";
this.txt_NewFruit.Size = new System.Drawing.Size(88, 20);
this.txt_NewFruit.TabIndex = 4;
this.txt_NewFruit.Text = "";
//
// printDocumentFruit
//
this.printDocumentFruit.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocumentFruit_PrintPage);
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItemPrint,
this.menuItemPrintPreview});
//
// menuItemPrint
//
this.menuItemPrint.Index = 0;
this.menuItemPrint.Text = "Print List";
this.menuItemPrint.Click += new System.EventHandler(this.menuItemPrint_Click);
//
// menuItemPrintPreview
//
this.menuItemPrintPreview.Index = 1;
this.menuItemPrintPreview.Text = "Print Preview";
this.menuItemPrintPreview.Click += new System.EventHandler(this.menuItemPrintPreview_Click);
//
// printPreviewDialogFruit
//
this.printPreviewDialogFruit.AutoScrollMargin = new System.Drawing.Size(0, 0);
this.printPreviewDialogFruit.AutoScrollMinSize = new System.Drawing.Size(0, 0);
this.printPreviewDialogFruit.ClientSize = new System.Drawing.Size(400, 300);
this.printPreviewDialogFruit.Enabled = true;
this.printPreviewDialogFruit.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialogFruit.Icon")));
this.printPreviewDialogFruit.Location = new System.Drawing.Point(280, 19);
this.printPreviewDialogFruit.MaximumSize = new System.Drawing.Size(0, 0);
this.printPreviewDialogFruit.Name = "printPreviewDialogFruit";
this.printPreviewDialogFruit.Opacity = 1;
this.printPreviewDialogFruit.TransparencyKey = System.Drawing.Color.Empty;
this.printPreviewDialogFruit.Visible = false;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 293);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.txt_NewFruit,
this.lbl_FruitSelected,
this.btn_Get,
this.btn_Add,
this.lst_Fruits});
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Simple List Box";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btn_Add_Click(object sender, System.EventArgs e)
{
lst_Fruits.Items.Add(txt_NewFruit.Text);
txt_NewFruit.Text = "";
}
private void btn_Get_Click(object sender, System.EventArgs e)
{
int i = lst_Fruits.SelectedIndex;
lbl_FruitSelected.Text = lst_Fruits.Items[i].ToString();
}
// Print List menu item handler
private void menuItemPrint_Click(object sender, System.EventArgs e)
{
printDocumentFruit.Print();
}
//PrintPage handler -- contains the code to do the printing of the next page
private void printDocumentFruit_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Font printFont = new Font("Arial", 12);
float fltLineHeight = printFont.GetHeight() + 2;
float x = e.MarginBounds.Left;
float y = e.MarginBounds.Top;
string strPrintLine;
for (int i = 0; i<= lst_Fruits.Items.Count-1; i++)
{
strPrintLine = lst_Fruits.Items[i].ToString();
e.Graphics.DrawString(strPrintLine, printFont, Brushes.Black, x, y);
y += fltLineHeight;
}
}
// Print Preview menu item handler
private void menuItemPrintPreview_Click(object sender, System.EventArgs e)
{
printPreviewDialogFruit.Document = printDocumentFruit;
printPreviewDialogFruit.ShowDialog();
}
}
}