Saturday, 13 July 2013

CSHARP WINDOWS EXAMPLES - K MANOHAR GOUD

9866550012

CSHARP WINDOWS EXAMPLES


1.
Windows Forms program with TextBox: C# 
 
  
using System;
using System.Drawing;
using System.Windows.Forms;

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

 private void Form1_Load(object sender, EventArgs e)
 {
     this.textBox1.Font = new Font("Consolas", 15.0f);
     this.textBox1.Text = "Dot Net Perls";
 }
    }
}
 
 
2.
 
RichTextBox 
 
 Example that demonstrates RichTextBox: C#

using System;
using System.Drawing;
using System.Windows.Forms;

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

 private void Form1_Load(object sender, EventArgs e)
 {
     richTextBox1.Font = new Font("Consolas", 18f, FontStyle.Bold);
     richTextBox1.BackColor = Color.AliceBlue;
     string[] words =
     {
  "Dot",
  "Net",
  "Perls",
  "is",
  "a",
  "nice",
  "website."
     };
     Color[] colors =
     {
  Color.Aqua,
  Color.CadetBlue,
  Color.Cornsilk,
  Color.Gold,
  Color.HotPink,
  Color.Lavender,
  Color.Moccasin
     };
     for (int i = 0; i < words.Length; i++)
     {
  string word = words[i];
  Color color = colors[i];
  {
      richTextBox1.SelectionBackColor = color;
      richTextBox1.AppendText(word);
      richTextBox1.SelectionBackColor = Color.AliceBlue;
      richTextBox1.AppendText(" ");
  }
     }
 }
    }
}
 
 
3.
 
CheckBox: ThreeStateProgram that uses CheckState and Checked properties: C#

using System;
using System.Windows.Forms;

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

     // Acquire the state of the CheckBox.
     CheckState state = checkBox1.CheckState;

     // Demonstrate how the CheckBox can be switched upon.
     switch (state)
     {
  case CheckState.Checked:
  case CheckState.Indeterminate:
  case CheckState.Unchecked:
      {
   MessageBox.Show(state.ToString());
   break;
      }
     }

     // Tell if the box is checked.
     MessageBox.Show(checkBox1.Checked.ToString());
 }
    }
}
 
 
 4.
 
Button in Windows Forms program 
 
Windows Forms code that uses Button control: C#

using System;
using System.Windows.Forms;

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

 private void button1_Click(object sender, EventArgs e)
 {
     MessageBox.Show("Dot Net says hello.", "How is your day going?");
 }
    }
} 
 
 
5.
 
 Examples of MessageBox.Show in Windows Forms
 
 
 
Windows Forms program that uses MessageBox: C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

 private void Form1_Load(object sender, EventArgs e)
 {
     //
     // The simplest overload of MessageBox.Show. [1]
     //
     MessageBox.Show("Dot Net Perls is awesome.");
     //
     // Dialog box with text and a title. [2]
     //
     MessageBox.Show("Dot Net Perls is awesome.",
  "Important Message");
     //
     // Dialog box with two buttons: yes and no. [3]
     //
     DialogResult result1 = MessageBox.Show("Is Dot Net Perls awesome?",
  "Important Question",
  MessageBoxButtons.YesNo);
     //
     // Dialog box with question icon. [4]
     //
     DialogResult result2 = MessageBox.Show("Is Dot Net Perls awesome?",
  "Important Query",
  MessageBoxButtons.YesNoCancel,
  MessageBoxIcon.Question);
     //
     // Dialog box with question icon and default button. [5]
     //
     DialogResult result3 = MessageBox.Show("Is Visual Basic awesome?",
  "The Question",
  MessageBoxButtons.YesNoCancel,
  MessageBoxIcon.Question,
  MessageBoxDefaultButton.Button2);
     //
     // Test the results of the previous three dialogs. [6]
     //
     if (result1 == DialogResult.Yes &&
  result2 == DialogResult.Yes &&
  result3 == DialogResult.No)
     {
  MessageBox.Show("You answered yes, yes and no.");
     }
     //
     // Dialog box that is right-aligned (not useful). [7]
     //
     MessageBox.Show("Dot Net Perls is the best.",
  "Critical Warning",
  MessageBoxButtons.OKCancel,
  MessageBoxIcon.Warning,
  MessageBoxDefaultButton.Button1,
  MessageBoxOptions.RightAlign,
  true);
     //
     // Dialog box with exclamation icon. [8]
     //
     MessageBox.Show("Dot Net Perls is super.",
  "Important Note",
  MessageBoxButtons.OK,
  MessageBoxIcon.Exclamation,
  MessageBoxDefaultButton.Button1);
 }
    }
}
 
 
 
6.
 
ComboBox screenshot 
 
 Program that demonstrates ComboBox event handlers: C#

using System;
using System.Windows.Forms;

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

 int _selectedIndex;
 string _text;

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
 {
     // Called when a new index is selected.
     _selectedIndex = comboBox1.SelectedIndex;
     Display();
 }

 private void comboBox1_TextChanged(object sender, EventArgs e)
 {
     // Called whenever text changes.
     _text = comboBox1.Text;
     Display();
 }

 void Display()
 {
     this.Text = string.Format("Text: {0}; SelectedIndex: {1}",
  _text,
  _selectedIndex);
 }
    }
}
 
 
7.
 
ContextMenuStrip screenshot 
 
 
 
 
 
private void copyToolStripMenuItem1_Click(object sender, EventArgs e)
{
    // Here, add the copy command to the relevant control, such as...
    textBox1.Copy(); // Added manually.
}
 
Fragment that implements Opening event: C#

private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
    // Runs before the user sees anything. A great place to set
    // Enabled to true or false.
}
 
More detailed Opening event handler: C#

private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
    // An elegant way to make the ContextMenu item always be Enabled
    // when there is a selection, and always disabled when there isn't.
    copyToolStripMenuItem1.Enabled = textBox1.SelectionLength > 0;
}
 
8.
 
MenuStrip control 
 
 
Program that implements Click event handlers: C#

using System;
using System.Windows.Forms;

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

 private void newToolStripMenuItem_Click(object sender, EventArgs e)
 {
     MessageBox.Show("New command executed");
 }

 private void openToolStripMenuItem_Click(object sender, EventArgs e)
 {
     MessageBox.Show("Open command executed");
 }
    }
}

Result
    Message boxes will appear when New/Open are clicked.
    Message boxes will appear when Ctrl-N/Ctrl-O are pressed.
 
  
 
 
 
 
 
 
 
 

No comments:

Post a Comment