









Database Combo Selection
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
cnn = new SqlConnection(@"integrated Security=sspi;Data Source=.\sqlexpress;Initial Catalog=" + comboBox1.Text );
cnn.Open();
}
Object Browser
The project make use of two Context Menu dataContext and contextMenuStrip1. First for database related options like Create Database whose functionality is yet to be implemented. second for table related options like Show Table Data.
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
try
{
TreeNode tempNode = treeView1.GetNodeAt(e.X, e.Y);
treeView1.SelectedNode = tempNode;
if (treeView1.SelectedNode.Level == 1)
{
treeView1.ContextMenuStrip = null;
strDbCnn = (@"integrated Security=sspi;Data Source=.\sqlexpress;Initial Catalog=" + tempNode.Text);
cnn2 = new SqlConnection(strDbCnn);
cnn2.Open();
cmd = new SqlCommand("select * from sysobjects where xtype = 'u'", cnn2);
dr2 = cmd.ExecuteReader();
treeView1.SelectedNode.Nodes.Clear();
while (dr2.Read())
{
treeView1.SelectedNode.Nodes.Add(dr2[0].ToString());
}
cnn2.Close();
dr2.Close();
}
else if (treeView1.SelectedNode.Level == 0)
{
treeView1.ContextMenuStrip =DataContext;
}
else if (treeView1.SelectedNode.Level == 2)
{
treeView1.ContextMenuStrip = contextMenuStrip1;
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}
Execute Button Click Code
private void button1_Click(object sender, EventArgs e)
{
try
{
string strSql = "";
if (textBox1.SelectedText != "")
strSql = textBox1.SelectedText;
else
strSql = textBox1.Text;
string strtmp = strSql;
string[] ss = strSql.Split(new char[]{' '});
if(ss[0].Equals("Use"))
{
richTextBox1.Text = "Database context changed to" + ss[1];
return;
}
cmd = new SqlCommand(strSql, cnn);
dr = cmd.ExecuteReader();
richTextBox1.Text = "";
int irf = dr.RecordsAffected;
while (dr.Read())
{
for (int i = 0; i < dr.FieldCount; i++)
{
richTextBox1.Text += dr[i].ToString() + "\t";
}
richTextBox1.Text += "\n";
}
if (irf > 0)
{
richTextBox1.Text += irf + " records affected";
}
dr.Close();
}
catch (Exception ex)
{
richTextBox1.Text += ex.Message.ToString();
}
}
Showing the SELECT result in Grid View
private void showDataToolStripMenuItem_Click(object sender, EventArgs e)
{
DataGrid d1 = new DataGrid();
d1.Dock = DockStyle.Fill;
cnn2 = new SqlConnection(strDbCnn);
cnn2.Open();
SqlDataAdapter adp;
adp = new SqlDataAdapter("select * from " + treeView1.SelectedNode.Text, cnn2);
DataSet ds=new DataSet();
adp.Fill(ds,"T1");
d1.DataSource = ds.Tables["T1"];
cnn2.Close();
splitContainer2.Panel2.Controls.Add(d1);
d1.BringToFront();
}
Windows Generated Code
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.splitContainer2 = new System.Windows.Forms.SplitContainer();
this.textBox1 = new System.Windows.Forms.TextBox();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.btnExecute = new System.Windows.Forms.Button();
this.treeView1 = new System.Windows.Forms.TreeView();
this.label1 = new System.Windows.Forms.Label();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.DataContext = new System.Windows.Forms.ContextMenuStrip();
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip();
this.createDatabaseToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.showDataToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.panel1.SuspendLayout();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
this.splitContainer2.Panel1.SuspendLayout();
this.splitContainer2.Panel2.SuspendLayout();
this.splitContainer2.SuspendLayout();
this.DataContext.SuspendLayout();
this.contextMenuStrip1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.panel1.Controls.Add(this.comboBox1);
this.panel1.Controls.Add(this.label1);
this.panel1.Controls.Add(this.btnExecute);
this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(529, 77);
this.panel1.TabIndex = 0;
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.Location = new System.Drawing.Point(0, 77);
this.splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.treeView1);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.splitContainer2);
this.splitContainer1.Size = new System.Drawing.Size(529, 286);
this.splitContainer1.SplitterDistance = 176;
this.splitContainer1.TabIndex = 1;
//
// splitContainer2
//
this.splitContainer2.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer2.Location = new System.Drawing.Point(0, 0);
this.splitContainer2.Name = "splitContainer2";
this.splitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal;
//
// splitContainer2.Panel1
//
this.splitContainer2.Panel1.Controls.Add(this.textBox1);
//
// splitContainer2.Panel2
//
this.splitContainer2.Panel2.Controls.Add(this.richTextBox1);
this.splitContainer2.Size = new System.Drawing.Size(349, 286);
this.splitContainer2.SplitterDistance = 95;
this.splitContainer2.TabIndex = 0;
//
// textBox1
//
this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.textBox1.Location = new System.Drawing.Point(0, 0);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(349, 95);
this.textBox1.TabIndex = 0;
//
// richTextBox1
//
this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.richTextBox1.Location = new System.Drawing.Point(0, 0);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(349, 187);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// btnExecute
//
this.btnExecute.Location = new System.Drawing.Point(80, 10);
this.btnExecute.Name = "btnExecute";
this.btnExecute.Size = new System.Drawing.Size(75, 23);
this.btnExecute.TabIndex = 1;
this.btnExecute.Text = "&Execute";
this.btnExecute.UseVisualStyleBackColor = true;
this.btnExecute.Click += new System.EventHandler(this.btnExecute_Click);
//
// treeView1
//
this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.treeView1.Location = new System.Drawing.Point(0, 0);
this.treeView1.Name = "treeView1";
this.treeView1.Size = new System.Drawing.Size(176, 286);
this.treeView1.TabIndex = 0;
this.treeView1.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseClick);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(185, 20);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(86, 13);
this.label1.TabIndex = 2;
this.label1.Text = "Select Database";
//
// comboBox1
//
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point(277, 10);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 21);
this.comboBox1.TabIndex = 3;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// DataContext
//
this.DataContext.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.createDatabaseToolStripMenuItem});
this.DataContext.Name = "DataContext";
this.DataContext.Size = new System.Drawing.Size(157, 26);
//
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.showDataToolStripMenuItem});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(153, 48);
//
// createDatabaseToolStripMenuItem
//
this.createDatabaseToolStripMenuItem.Name = "createDatabaseToolStripMenuItem";
this.createDatabaseToolStripMenuItem.Size = new System.Drawing.Size(156, 22);
this.createDatabaseToolStripMenuItem.Text = "Create Database";
//
// showDataToolStripMenuItem
//
this.showDataToolStripMenuItem.Name = "showDataToolStripMenuItem";
this.showDataToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.showDataToolStripMenuItem.Text = "Show Data";
this.showDataToolStripMenuItem.Click += new System.EventHandler(this.showDataToolStripMenuItem_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(529, 363);
this.Controls.Add(this.splitContainer1);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Shalvin Sql Server Management Studio";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Load += new System.EventHandler(this.Form1_Load);
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel2.ResumeLayout(false);
this.splitContainer1.ResumeLayout(false);
this.splitContainer2.Panel1.ResumeLayout(false);
this.splitContainer2.Panel1.PerformLayout();
this.splitContainer2.Panel2.ResumeLayout(false);
this.splitContainer2.ResumeLayout(false);
this.DataContext.ResumeLayout(false);
this.contextMenuStrip1.ResumeLayout(false);
this.ResumeLayout(false);
}
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.SplitContainer splitContainer2;
private System.Windows.Forms.Button btnExecute;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ContextMenuStrip DataContext;
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem createDatabaseToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem showDataToolStripMenuItem;
C# is a modern object oriented programming language.I am going to have a series of blog on C# Windows forms. These series of blogs are intended to be the study material for my training sessions. So I might be following more of a tutorial walk through mode of blogging.
All the examples are based on Visual C# 2008 Express Edition which is a free development tool from Microsoft.
Fire up Visual Studio C# 2008 Express Edition. From the File menu select New Project. The New Project Dialog will appear, which will present you with different project types available in express edition. Note the WPF Application and WPF Browser Application project types which are new to 2008. This series of blogs are going to concentrate on windows forms applications, so select Windows Forms Applications and click Ok.
You will be greeted with the IDE or Integrated Development Environment. Integrated Development Environment is the interface with which a programmer interacts with and develops application in Visual Studio.It comprises ofMenu Bar, ToolBar, ToolBox, Form Window, Properties Window, Solution Explorer, Code Windowand a host of other windows.If these windows are not visible by default you can navigate to View menu and select the window or use the short cut keys.
Its better to memorize short cut keys instead of using the mouse.
The short cut keys form various IDE components are as follows ToolBox (Ctrl Alt X), Properties Window (F4), Solution Explorer (Ctrl Alt L).
ToolBox
MenuBar and ToolBar requires no explanation. All windows users are very much familiar with MenuBar and ToolBar.
Just like a mechanic or electrician is having his own toolbox comprising of Screw drivers, testers, wires, etc. we .net programmers do have a toolbox with which we work. Toolbox is having a collection of controls.
Controls inside toolbox are arranged as tabs. Toolbox is having 8 tabs. The first tab All Windows Forms contains all the controls including containers, menus, data, components, printing and dialogs.
Common Controls tab contains commonly used controls Button, TextBox, Label, ListBox, ComboBox, etc.
Containers tab contains container controls. Container controls are those controls which acts as containers for other controls. This including simple containers like GroupBox, Panel and SplitContainers to complext containers like FlowLayoutPanel, TableLayoutPanel and TabControl.
Menus and Toolbars tab contains controls required for creating menus, context menus and toolbars.
Data tab contains controls required for database interaction.
Components tab contains those controls which doesn't have a visual interface at runtime. This includes controls like Timer, ErrorProvider, etc.
Printing tab contains print related controls like PrintPreviewDialog, PrintPreviewControl, PrintPreviewControls, etc.
Dialogs tab contains commonly used dialogs like OpenFileDialog, SaveFileDialog, ColorDialog, FontDialog, etc.
In addition to the existing tabs you can add your own tabs and controls to it. This feature comes handy for having a collection of COM controls or adding yours own controls to toolbox.
GroupBox Panel and TabControl
GroupBox Panel and TabControl
Group Box and Panel are container controls in Windows Forms. By container control I mean those controls which hosts other controls. These controls are used for logically group controls
TabControl
A TabControl is used to have a collection of TabPages, thus avoiding the need for multiple forms as in the case with Windows Task Manager which can be invoked by pressing Ctl+Alt+Del. Here the functionalityn of 5 forms is incorporated in a single form.
Clicking the TabPages property in the properties window will invoke the TabPages Collection editor which can be used for adding Tab Pages.
Html is the language of web. Almost all the sites in the web is created using HTML. It is a tag based language.
HTML can be created with plain Notepad or a there are a slew of editors like Visual Studio Code, Atom, Sublime, etc. I am using Notepad to start with.
An HTML document contained two main section viz. Head section and Body section. Body section is where the contents of the page will appear. The Head section contains information like Title of the Page, Scripts, etc.
<html>
<body>
Shalvin P D
</body>
</html>
There are both opening and closing tags. Closing tag is denoted by /. Eg. </html>, </body>, etc. Notice that I have indented the tags so that it will be more readable. The relationship between tags can be better understood with indentation. Here HTML is the parent tag.
I saved the document with .htm extension. Double clicking on the file will open the html document inside the default browser.
The Browser will interpret the HTML tags and produce the web site.
Title Title tag is used to produce a title for the document. The title appears in the title bar of Browser.
<html>
<head>
<title>.Net and Angular Consultant and Trainer</title>
<head>
<body>
Shalvin P D
</body>
</html>
Br
Br tag is used for producing a break between contents
Result
//Adding Columns
listView1.Columns.Add("Blogs", 170);
listView1.Columns.Add("Blogger" , 100);
//Adding a ListView Item
ListViewItem item1 = new ListViewItem("shalvin.com", 0);
item1.SubItems.Add("Shalvin P D");
listView1.Items.Add(item1);
ListViewItem item2 = new ListViewItem("shalvinpd.blogspot.com", 0);
item2.SubItems.Add("Shalvin P D");
listView1.Items.Add(item2);
}
OleDbConnection cnn;
OleDbCommand cmd;
private void Form1_Load(object sender, EventArgs e)
{
cnn = new OleDbConnection("provider=msdaora.1;user id=scott;password=tiger;Data Source=Cargomar");
cnn.Open();
}
private void btnInsert_Click(object sender, EventArgs e)
{
cmd = new OleDbCommand("insert into Categories values (5, 'Mouse', 'Computer Mouse')", cnn);
cmd.ExecuteNonQuery();
MessageBox.Show("Record Saved");
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.Background = new LinearGradientBrush(Colors.Red, Colors.Blue, new Point(0, 0), new Point(1, 1));
}
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace WPFBrush
{
class Class1 : Window
{
[STAThread]
public static void Main()
{
Application app = new Application();
app.Run(new Class1 { Title = "Shalvin - Colors" });
}
public Class1()
{
Grid LayoutRoot = new Grid();
this.Content = LayoutRoot;
LayoutRoot.Background = new LinearGradientBrush(Colors.Red, Colors.Blue, 0);
}
}
}
Grid LayoutRoot = new Grid();
this.Content = LayoutRoot;
LayoutRoot.Background = new RadialGradientBrush (Colors.Red, Colors.Blue);
RadialGradientBrush brush = new RadialGradientBrush();
Background = brush;
brush.GradientStops.Add(new GradientStop(Colors.Red, 0));
brush.GradientStops.Add(new GradientStop(Colors.Green, .33));
brush.GradientStops.Add(new GradientStop(Colors.Blue, .66));
<Window x:Class="WpfApplication1.Calc"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Calc" Height="300" Width="300" Loaded="Window_Loaded">
<Grid>
<Label Content="Integer 1" Height="28" HorizontalAlignment="Left" Margin="44,33,0,0" Name="label1" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="120,33,0,0" Name="txtInt1" VerticalAlignment="Top" Width="120" />
<Label Content="Integer 2" Height="28" HorizontalAlignment="Left" Margin="53,88,0,0" Name="label2" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="120,80,0,0" Name="txtInt2" VerticalAlignment="Top" Width="120" />
<Label Content="Result" Height="28" HorizontalAlignment="Left" Margin="63,132,0,0" Name="label3" VerticalAlignment="Top" />
<Label Height="28" HorizontalAlignment="Left" Margin="120,132,0,0" Name="lblResult" VerticalAlignment="Top" Width="112" BorderBrush="Black" BorderThickness="1" />
<Button Content="+" Height="23" HorizontalAlignment="Left" Margin="98,196,0,0" Name="btnAdd" VerticalAlignment="Top" Width="75" Click="btnAdd_Click" />
</Grid>
</Window>