This blog is the continuation of .Net Globalization blog.
If you want to have a global market for your project then you have to plan for implementing multi language support.
Create a form as shown below.
Add Another form. This is the form whose language text we are planning to change based on the selection from the combobox of first form. Give English Texts to Form and Controls.
Select the form set the Localizable property to True and Language to French(France).
Now select the existing controls and change its Text property to corresponding French equivalent as shown in the figure.
Go back to the first form where you can select the language.
In this form we are going to change the culture of the current thread based on the culture
Explanations and additional information will follow soon.
using System.Globalization;
using System.Threading;
private void cboLang_SelectedIndexChanged(object
sender, EventArgs e)
{
if(cboLang.Text == "French")
Thread.CurrentThread.CurrentUICulture = new
CultureInfo("fr-FR");
else
Thread.CurrentThread.CurrentUICulture = new
CultureInfo("en-US");
}
private void btnShowForm_Click(object sender, EventArgs
e)
{
frmLang fl = new frmLang();
fl.Show();
}
VB .Net
Imports System.Globalization
Imports System.Threading
Public Class Form1
Private Sub cboLang_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboLang.SelectedIndexChanged
If cboLang.Text = "English" Then
Thread.CurrentThread.CurrentUICulture = New CultureInfo("en-US")
Else
Thread.CurrentThread.CurrentUICulture = New CultureInfo("fr-FR")
End If
End Sub
Private Sub btnShowForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowForm.Click
Dim fl As New frmLang
fl.Show()
End Sub
End Class
Now when you run the application, select French from the ComboBox and click show form the French form will appear and when you select English, English form will appear.
Windows Forms VB .Net Generated Code
'Form1
Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.cboLang = New System.Windows.Forms.ComboBox
Me.btnShowForm = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(52, 70)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(88, 13)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Select Language"
'
'cboLang
'
Me.cboLang.FormattingEnabled = True
Me.cboLang.Items.AddRange(New Object() {"English", "French"})
Me.cboLang.Location = New System.Drawing.Point(159, 62)
Me.cboLang.Name = "cboLang"
Me.cboLang.Size = New System.Drawing.Size(121, 21)
Me.cboLang.TabIndex = 1
'
'btnShowForm
'
Me.btnShowForm.Location = New System.Drawing.Point(159, 113)
Me.btnShowForm.Name = "btnShowForm"
Me.btnShowForm.Size = New System.Drawing.Size(75, 23)
Me.btnShowForm.TabIndex = 2
Me.btnShowForm.Text = "&Show Form"
Me.btnShowForm.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.btnShowForm)
Me.Controls.Add(Me.cboLang)
Me.Controls.Add(Me.Label1)
Me.Name = "Form1"
Me.Text = "Shalvin.com"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents cboLang As System.Windows.Forms.ComboBox
Friend WithEvents btnShowForm As System.Windows.Forms.Button
'frmLang
Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.txtName = New System.Windows.Forms.TextBox
Me.Label2 = New System.Windows.Forms.Label
Me.txtPhone = New System.Windows.Forms.TextBox
Me.btnSave = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(41, 55)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(35, 13)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Name"
'
'txtName
'
Me.txtName.Location = New System.Drawing.Point(103, 48)
Me.txtName.Name = "txtName"
Me.txtName.Size = New System.Drawing.Size(100, 20)
Me.txtName.TabIndex = 1
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(41, 105)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(38, 13)
Me.Label2.TabIndex = 2
Me.Label2.Text = "Phone"
'
'txtPhone
'
Me.txtPhone.Location = New System.Drawing.Point(103, 98)
Me.txtPhone.Name = "txtPhone"
Me.txtPhone.Size = New System.Drawing.Size(100, 20)
Me.txtPhone.TabIndex = 3
'
'btnSave
'
Me.btnSave.Location = New System.Drawing.Point(103, 140)
Me.btnSave.Name = "btnSave"
Me.btnSave.Size = New System.Drawing.Size(75, 23)
Me.btnSave.TabIndex = 4
Me.btnSave.Text = "Save"
Me.btnSave.UseVisualStyleBackColor = True
'
'frmLang
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.btnSave)
Me.Controls.Add(Me.txtPhone)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.txtName)
Me.Controls.Add(Me.Label1)
Me.Name = "frmLang"
Me.Text = "English"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents txtName As System.Windows.Forms.TextBox
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents txtPhone As System.Windows.Forms.TextBox
Friend WithEvents btnSave As System.Windows.Forms.Button
Monday, December 21, 2009
Globalization in Windows Forms - Application with multi language support
Friday, December 4, 2009
Contact Management System VB .Net Windows Generated Code
The purpose of this blog is easy recreation of the visual intereface of the project for my student comunity.
The .Net Windows Form of Visual Studion 2008 is made up of two partial classes and a resource file.
If you click the Show All Files icon in the Solution solution explorer then you cann see all the files I have mentioned before.
If you replace the InitializeComponents function with your visual intereface code then you will get a new visual interface.
Private Sub InitializeComponent()
Me.MenuStrip1 = New System.Windows.Forms.MenuStrip
Me.FileToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
Me.ExitToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
Me.MasterToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
Me.GroupsToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
Me.ContactsToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
Me.MenuStrip1.SuspendLayout()
Me.SuspendLayout()
'
'MenuStrip1
'
Me.MenuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.FileToolStripMenuItem, Me.MasterToolStripMenuItem})
Me.MenuStrip1.Location = New System.Drawing.Point(0, 0)
Me.MenuStrip1.Name = "MenuStrip1"
Me.MenuStrip1.Size = New System.Drawing.Size(292, 24)
Me.MenuStrip1.TabIndex = 1
Me.MenuStrip1.Text = "MenuStrip1"
'
'FileToolStripMenuItem
'
Me.FileToolStripMenuItem.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.ExitToolStripMenuItem})
Me.FileToolStripMenuItem.Name = "FileToolStripMenuItem"
Me.FileToolStripMenuItem.Size = New System.Drawing.Size(35, 20)
Me.FileToolStripMenuItem.Text = "&File"
'
'ExitToolStripMenuItem
'
Me.ExitToolStripMenuItem.Name = "ExitToolStripMenuItem"
Me.ExitToolStripMenuItem.Size = New System.Drawing.Size(152, 22)
Me.ExitToolStripMenuItem.Text = "&Exit"
'
'MasterToolStripMenuItem
'
Me.MasterToolStripMenuItem.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.GroupsToolStripMenuItem, Me.ContactsToolStripMenuItem})
Me.MasterToolStripMenuItem.Name = "MasterToolStripMenuItem"
Me.MasterToolStripMenuItem.Size = New System.Drawing.Size(52, 20)
Me.MasterToolStripMenuItem.Text = "&Master"
'
'GroupsToolStripMenuItem
'
Me.GroupsToolStripMenuItem.Name = "GroupsToolStripMenuItem"
Me.GroupsToolStripMenuItem.Size = New System.Drawing.Size(152, 22)
Me.GroupsToolStripMenuItem.Text = "&Groups"
'
'ContactsToolStripMenuItem
'
Me.ContactsToolStripMenuItem.Name = "ContactsToolStripMenuItem"
Me.ContactsToolStripMenuItem.Size = New System.Drawing.Size(152, 22)
Me.ContactsToolStripMenuItem.Text = "&Contacts"
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 272)
Me.Controls.Add(Me.MenuStrip1)
Me.IsMdiContainer = True
Me.MainMenuStrip = Me.MenuStrip1
Me.Name = "Form1"
Me.Text = "Contact Mangement System"
Me.WindowState = System.Windows.Forms.FormWindowState.Maximized
Me.MenuStrip1.ResumeLayout(False)
Me.MenuStrip1.PerformLayout()
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents MenuStrip1 As System.Windows.Forms.MenuStrip
Friend WithEvents FileToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
Friend WithEvents ExitToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
Friend WithEvents MasterToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
Friend WithEvents GroupsToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
Friend WithEvents ContactsToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
'frmGroups
Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.lstGroups = New System.Windows.Forms.ListBox
Me.txtGroup = New System.Windows.Forms.TextBox
Me.btnAdd = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(26, 33)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(36, 13)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Group"
'
'lstGroups
'
Me.lstGroups.FormattingEnabled = True
Me.lstGroups.Location = New System.Drawing.Point(90, 113)
Me.lstGroups.Name = "lstGroups"
Me.lstGroups.Size = New System.Drawing.Size(120, 95)
Me.lstGroups.TabIndex = 1
'
'txtGroup
'
Me.txtGroup.Location = New System.Drawing.Point(90, 26)
Me.txtGroup.Name = "txtGroup"
Me.txtGroup.Size = New System.Drawing.Size(100, 20)
Me.txtGroup.TabIndex = 0
'
'btnAdd
'
Me.btnAdd.Location = New System.Drawing.Point(90, 64)
Me.btnAdd.Name = "btnAdd"
Me.btnAdd.Size = New System.Drawing.Size(75, 23)
Me.btnAdd.TabIndex = 1
Me.btnAdd.Text = "&Add"
Me.btnAdd.UseVisualStyleBackColor = True
'
'frmGroups
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 272)
Me.Controls.Add(Me.btnAdd)
Me.Controls.Add(Me.txtGroup)
Me.Controls.Add(Me.lstGroups)
Me.Controls.Add(Me.Label1)
Me.Name = "frmGroups"
Me.Text = "frmGroups"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents lstGroups As System.Windows.Forms.ListBox
Friend WithEvents txtGroup As System.Windows.Forms.TextBox
Friend WithEvents btnAdd As System.Windows.Forms.Button
'frmContacts
Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.cboGroups = New System.Windows.Forms.ComboBox
Me.Label2 = New System.Windows.Forms.Label
Me.txtContact = New System.Windows.Forms.TextBox
Me.btnAdd = New System.Windows.Forms.Button
Me.Label3 = New System.Windows.Forms.Label
Me.txtPhone = New System.Windows.Forms.TextBox
Me.Label4 = New System.Windows.Forms.Label
Me.txtEmail = New System.Windows.Forms.TextBox
Me.DataGridView1 = New System.Windows.Forms.DataGridView
CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(31, 45)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(41, 13)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Groups"
'
'cboGroups
'
Me.cboGroups.FormattingEnabled = True
Me.cboGroups.Location = New System.Drawing.Point(102, 37)
Me.cboGroups.Name = "cboGroups"
Me.cboGroups.Size = New System.Drawing.Size(121, 21)
Me.cboGroups.TabIndex = 1
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(262, 45)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(49, 13)
Me.Label2.TabIndex = 2
Me.Label2.Text = "Contacts"
'
'txtContact
'
Me.txtContact.Location = New System.Drawing.Point(337, 37)
Me.txtContact.Name = "txtContact"
Me.txtContact.Size = New System.Drawing.Size(100, 20)
Me.txtContact.TabIndex = 3
'
'btnAdd
'
Me.btnAdd.Location = New System.Drawing.Point(168, 133)
Me.btnAdd.Name = "btnAdd"
Me.btnAdd.Size = New System.Drawing.Size(75, 23)
Me.btnAdd.TabIndex = 4
Me.btnAdd.Text = "&Add"
Me.btnAdd.UseVisualStyleBackColor = True
'
'Label3
'
Me.Label3.AutoSize = True
Me.Label3.Location = New System.Drawing.Point(34, 96)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(38, 13)
Me.Label3.TabIndex = 5
Me.Label3.Text = "Phone"
'
'txtPhone
'
Me.txtPhone.Location = New System.Drawing.Point(102, 89)
Me.txtPhone.Name = "txtPhone"
Me.txtPhone.Size = New System.Drawing.Size(100, 20)
Me.txtPhone.TabIndex = 6
'
'Label4
'
Me.Label4.AutoSize = True
Me.Label4.Location = New System.Drawing.Point(262, 96)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(32, 13)
Me.Label4.TabIndex = 7
Me.Label4.Text = "Email"
'
'txtEmail
'
Me.txtEmail.Location = New System.Drawing.Point(337, 89)
Me.txtEmail.Name = "txtEmail"
Me.txtEmail.Size = New System.Drawing.Size(100, 20)
Me.txtEmail.TabIndex = 8
'
'DataGridView1
'
Me.DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
Me.DataGridView1.Location = New System.Drawing.Point(12, 175)
Me.DataGridView1.Name = "DataGridView1"
Me.DataGridView1.Size = New System.Drawing.Size(439, 222)
Me.DataGridView1.TabIndex = 9
'
'frmContacts
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(463, 405)
Me.Controls.Add(Me.DataGridView1)
Me.Controls.Add(Me.txtEmail)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.txtPhone)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.btnAdd)
Me.Controls.Add(Me.txtContact)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.cboGroups)
Me.Controls.Add(Me.Label1)
Me.Name = "frmContacts"
Me.Text = "frmContacts"
CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents cboGroups As System.Windows.Forms.ComboBox
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents txtContact As System.Windows.Forms.TextBox
Friend WithEvents btnAdd As System.Windows.Forms.Button
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents txtPhone As System.Windows.Forms.TextBox
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents txtEmail As System.Windows.Forms.TextBox
Friend WithEvents DataGridView1 As System.Windows.Forms.DataGridView
Wednesday, December 2, 2009
Many To Many Relationship Table Script
I am creating a database for tracking the Cricketers and their types. By types I mean Batsman, Bowler, Wicket Keeper, etc.
The issue is a Cricketer might belong to different type. For Example Kapil Dev is both a Batsman and a Bowler.
Inorder to solve this I should create a third table referred to as Junction Table apart from Cricketer and Type tables which is having reference to both Crickter and Type tables and a Composite Key. Here I name it Cricketer Type.
Create database Cricket
use Cricket
Create table Cricketer (CricketerId int identity(1,1) primary key,Cricketer varchar(40))
Create table Type (TypeId int identity(1,1) primary key,Type varchar(40))
insert into Cricketer values ('Sumesh')
insert into Cricketer values ('Nithin')
insert into Cricketer values ('Shalvin')
insert into Cricketer values ('Mongia')
insert into Type values ('Batsman')
insert into Type values ('Bowler')
insert into Type values ('Wicket Keeper')
create table CricketerType(primary key (CricketerId, TypeId),CricketerId int references Cricketer(CricketerId),TypeId int references Type(TypeId) )
insert into CricketerType values (1, 1)
insert into CricketerType values (1, 3)
insert into CricketerType values (2, 2)
insert into CricketerType values (3, 3)
insert into CricketerType values (4, 3)
select * from Cricketer
select * from Type
select * from CricketerType
select C.CricketerId, C.Cricketer, T.Type from Cricketer C, Type T,CricketerType CT where CT.CricketerId = C.CricketerId and Ct.TypeId = T.TypeId
Sunday, November 29, 2009
Excel Integration with .Net
Here I am populating both a DataGridView and ComboBox with Excel Data.
OleDbConnection cnn;
public DataSet GetData(String SheetName){
String Query = "SELECT * FROM [" + SheetName + "];"; OleDbDataAdapter myCommand = new OleDbDataAdapter(Query, cnn);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet, "ExcelInfo");
return myDataSet;
}
private void Form1_Load(object sender, EventArgs e){
cnn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + "Alloc.xls" + ";" + "Extended Properties=Excel 8.0");
OleDbDataAdapter da;
DataSet ds = GetData("Sheet1$");
DataTable dt = ds.Tables[0];
dataGridView1.DataSource = ds.Tables[0];
comboBox1.DataSource = ds.Tables[0]; comboBox1.DisplayMember = "CategoryName";
}
Friday, November 6, 2009
Microsoft Office SharePoint Server (MOSS) Installation ScreenShots
Microsoft Office SharePoint Server (MOSS) is Web based collaboration, content and document management platform from Microsoft.
MOSS requires Windows Server 2003 Sp1 or above, .Net Framework 2.x and 3.x. The contents are stored in Sql Server 2005.
To start the installation double click the OfficeServer.exe present inside MOSS2007_SETUP folder.
Wednesday, November 4, 2009
Deploying and Migrating Your Client/Server Infrastructure to Windows 7 / Windows Server 2008 R2
Date : 5th November 2009
Venue : Taj Gateway Hotel
Waterfront Hall
Marine Drive, Ernakulam 682 011
Kerala, India
Time : 10.00 AM to 1.30 PM
10:00 am -11:30 am
Introducing Windows 7
In this session we will have a look into the new Windows 7 feature enhancements like Security, Web enhancements, UI Enhancements and tools for IT Pros
11:30 -11:45 am
Break
11:45 am – 12:30 pm
Windows 7 – Upgrade and Deployment
In this session you will get to see and know the tools available to upgrade, migrate and deploy Windows 7 in the enterprise like User State Migration Tool, Microsoft Assessment and Planning Toolkit, Windows Easy Transfer, and Microsoft Deployment Toolkit 2010.
12:30 – 1:30 pm
What’s New in Windows Server 2008 R2
In this session we will have a look into the new Active Directory features, Group Policy changes and other new features in Windows Server 2008 R2.
Click here to Register now!
For More Details, you can call 080 41161412.
Friday, October 30, 2009
Silverlight 3 support in Visual Web Developer 2010 Beta 2
Starting With Visual Web Developer 2008 sp1 there is the New Project option in Vwd. So go to File, New Project and Select Silverlight Application.
Let host the Silverlight Application in Asp .Net Application.
If you inspect the Properties Window you can notice that there is both Alphabetical and Categorized View of Entries and also Events. In C# Express Edition 2080 there was only the Categorized View in Properties. I am basically a lover of Alphabetical View.
private void button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Welcome to Silverlight 3");
}
In short Working with Silverlight 3 in Vwd 2010 is as cool as working with Asp .Net Application.
Wednesday, October 14, 2009
Expense Tracking System Sql Script
create database ExpenseTrack
use ExpenseTrack
create table ExpenseGroup(GroupId int primary key, GroupName varchar(40) unique)
insert into ExpenseGroup values (1, 'Food')
insert into ExpenseGroup values (2, 'Clothes')
insert into ExpenseGroup values (3, 'Travelling')
select * from ExpenseGroup
create table Expenses (ExpenseId int primary key,
GroupId int references ExpenseGroup(GroupId),
Expense varchar(40))
insert into Expenses values (1, 1, 'Vegetables')
insert into Expenses values (2, 1, 'Hotel')
insert into Expenses values (3, 2, 'Clothes')
insert into Expenses values (4, 3, 'Petrol')
create table ExpenseTrans (TransId int primary key,
ExpenseId int references Expenses(ExpenseId),
TransDate datetime, Amount numeric(8,2), Description varchar(200))
select * from Expenses
create procedure spGetExpenses as
select G.GroupName, E.Expense from ExpenseGroup G, Expenses E
where G.GroupId = E.GroupId
go
spGetExpenses
insert into ExpenseTrans values (1, 3, '14-Oct-09', 400, 'Bought shirt from Navigator')
insert into ExpenseTrans values (2, 2, '15-Oct-09', 40, 'Had two Masala Dosa from Megha Hotel Kacheripady')
insert into ExpenseTrans values (3, 4, '15-Oct-09', 100, 'Petrol for Rs. 100 from Vaduthal Pump')
select * from ExpenseTrans
create procedure spGetExpenseTrans as
select E.Expense, T.TransDate, T.Amount, T.Description
from Expenses E, ExpensTrans T where E.ExpenseId = T.ExpenseId
go
spGetExpenseTrans
'DbConnect
Imports System.Data.SqlClient
Public Class DbConnect
Public cnn As SqlConnection
Dim ds As New DataSet
Dim da As SqlDataAdapter
Public Sub Open()
cnn = New SqlConnection("Integrated security=sspi;Initial Catalog=ExpenseTrack")
cnn.Open()
End Sub
Public Function GetExpenses() As DataTable
Open()
da = New SqlDataAdapter("select * from Expenses", cnn)
da.Fill(ds, "Expense")
Return ds.Tables("Expense")
End Function
End Class
'frmTransDetial
Imports System.Data.SqlClient
Public Class Form1
Dim dbc As New DbConnect
Dim b As Boolean = False
Dim cmd As SqlCommand
Dim intMax As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cboExpense.DataSource = dbc.GetExpenses
cboExpense.DisplayMember = "Expense"
cboExpense.ValueMember = "ExpenseId"
b = True
cmd = New SqlCommand("select Max(TransId) from ExpenseTrans", dbc.cnn)
b = Int32.TryParse(cmd.ExecuteScalar().ToString(), intMax)
intMax += 1
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim strSql As String
strSql = "insert into ExpenseTrans values (" + intMax.ToString + ", "
strSql += cboExpense.SelectedValue.ToString + ", '"
strSql += dtpDate.Value.ToString("d").ToString + "', "
strSql += txtAmount.Text + ", '" + txtRemarks.Text + "')"
cmd = New SqlCommand(strSql, dbc.cnn)
cmd.ExecuteNonQuery()
End Sub
End Class
Windows Generated Code for Expense Transaction Form
Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.dtpDate = New System.Windows.Forms.DateTimePicker
Me.Label2 = New System.Windows.Forms.Label
Me.cboExpense = New System.Windows.Forms.ComboBox
Me.Label3 = New System.Windows.Forms.Label
Me.txtRemarks = New System.Windows.Forms.TextBox
Me.btnSave = New System.Windows.Forms.Button
Me.Label4 = New System.Windows.Forms.Label
Me.txtAmount = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(38, 43)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(30, 13)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Date"
'
'dtpDate
'
Me.dtpDate.CustomFormat = "dd MMM yyyy"
Me.dtpDate.Format = System.Windows.Forms.DateTimePickerFormat.Custom
Me.dtpDate.Location = New System.Drawing.Point(97, 36)
Me.dtpDate.Name = "dtpDate"
Me.dtpDate.Size = New System.Drawing.Size(140, 20)
Me.dtpDate.TabIndex = 1
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(38, 96)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(48, 13)
Me.Label2.TabIndex = 2
Me.Label2.Text = "Expense"
'
'cboExpense
'
Me.cboExpense.FormattingEnabled = True
Me.cboExpense.Location = New System.Drawing.Point(97, 88)
Me.cboExpense.Name = "cboExpense"
Me.cboExpense.Size = New System.Drawing.Size(121, 21)
Me.cboExpense.TabIndex = 3
'
'Label3
'
Me.Label3.AutoSize = True
Me.Label3.Location = New System.Drawing.Point(38, 180)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(49, 13)
Me.Label3.TabIndex = 4
Me.Label3.Text = "Remarks"
'
'txtRemarks
'
Me.txtRemarks.Location = New System.Drawing.Point(97, 173)
Me.txtRemarks.Multiline = True
Me.txtRemarks.Name = "txtRemarks"
Me.txtRemarks.Size = New System.Drawing.Size(178, 48)
Me.txtRemarks.TabIndex = 5
'
'btnSave
'
Me.btnSave.Location = New System.Drawing.Point(97, 251)
Me.btnSave.Name = "btnSave"
Me.btnSave.Size = New System.Drawing.Size(75, 23)
Me.btnSave.TabIndex = 6
Me.btnSave.Text = "Save"
Me.btnSave.UseVisualStyleBackColor = True
'
'Label4
'
Me.Label4.AutoSize = True
Me.Label4.Location = New System.Drawing.Point(38, 141)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(43, 13)
Me.Label4.TabIndex = 7
Me.Label4.Text = "Amount"
'
'txtAmount
'
Me.txtAmount.Location = New System.Drawing.Point(97, 134)
Me.txtAmount.Name = "txtAmount"
Me.txtAmount.Size = New System.Drawing.Size(100, 20)
Me.txtAmount.TabIndex = 8
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(372, 321)
Me.Controls.Add(Me.txtAmount)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.btnSave)
Me.Controls.Add(Me.txtRemarks)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.cboExpense)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.dtpDate)
Me.Controls.Add(Me.Label1)
Me.Name = "Form1"
Me.Text = "Expense Trans"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents dtpDate As System.Windows.Forms.DateTimePicker
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents cboExpense As System.Windows.Forms.ComboBox
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents txtRemarks As System.Windows.Forms.TextBox
Friend WithEvents btnSave As System.Windows.Forms.Button
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents txtAmount As System.Windows.Forms.TextBox
Related Blog
Simple Contact Management System In C# and VB .Net
Simple Contact Management System In C# and VB .Net
create database ContactManagement
use ContactManagement
create table ContactGroups(GroupId int primary key, GroupName varchar(40))
select * from ContactGroups
insert into ContactGroups values (1, 'Students')
insert into ContactGroups values (2, 'Relatives')
create table Contacts (ContactId int primary key, GroupId int references ContactGroups(GroupId), ContactName varchar(40), Phone varchar(15), Email varchar(40))
insert into Contacts values (1, 1, 'Sumesh S', '9961321010', 'mail4sumo@yahoo.co.in')
insert into Contacts values (2, 1, 'Nithin T M', '9446737393', 'nithintm@gmail.com')
insert into Contacts values (3, 2, 'Shalvin', '998765778', 'shalvin@gmail.com')
select * from Contacts
select G.GroupName, C.ContactName, C.Phone, C.Email from
ContactGroups G, Contacts C where G.GroupId = C.GroupId
I am adding a class called DbConnect which is to contain Database Connectivity and retrieval code.
using System.Data;
using System.Data.SqlClient;
namespace ContactManagementSystem
{
class DbConnect
{
public SqlConnection cnn;
public void Open()
{
cnn = new SqlConnection("Integrated Security=sspi;Initial Catalog=ContactManagement");
cnn.Open();
}
public DataTable GetGroups()
{
Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select * from ContactGroups", cnn);
da.Fill(ds, "Grp");
return ds.Tables["Grp"];
}
}
frmGroups
Please visit my blog Windows Forms Input Validation if your are not familiar with Validation event and ErrorProvider Control.
using System.Data.SqlClient;
DbConnect dbc = new DbConnect();
Boolean b = false;
int intMax = 0;
SqlCommand cmd;
private void frmGroups_Load(object sender, EventArgs e)
{
dbc.Open();
cmd = new SqlCommand("select Max(GroupId) from ContactGroups", dbc.cnn);
b = Int32.TryParse(cmd.ExecuteScalar().ToString(), out intMax);
intMax += 1;
ShowData();
}
private void ShowData()
{
dgvGroups.DataSource = dbc.GetGroups();
}
private void btnAdd_Click(object sender, EventArgs e)
{
cmd = new SqlCommand("insert into ContactGroups values (" + intMax.ToString() + ", '" + txtContactGroup.Text + "')", dbc.cnn);
cmd.ExecuteNonQuery();
MessageBox.Show("Record Saved");
ShowData();
}
private void txtContactGroup_Validating(object sender, CancelEventArgs e)
{
if (txtContactGroup.Text == "")
{
errorProvider1.SetError(txtContactGroup, "Group Name cannot be blank");
txtContactGroup.Focus();
}
else
{
errorProvider1.SetError(txtContactGroup, "");
}
}
Obviously creating an sql statement for passing to SqlCommand is not the right approach. But let me keep it simple and short and not end up in feature creep. I will take up the better approach in a later blog.
frmContact
Now let's come to the child table data insertion.
using System.Data.SqlClient;
DbConnect dbc = new DbConnect();
SqlCommand cmd;
Boolean b = false;
int intMax = 0;
private void frmContacts_Load(object sender, EventArgs e)
{
dbc.Open();
cboGroup.DataSource = dbc.GetGroups();
cboGroup.DisplayMember = "GroupName";
cboGroup.ValueMember = "GroupId";
}
private void btnSave_Click(object sender, EventArgs e)
{
dbc.Open();
cmd = new SqlCommand("select Max(ContactId) from Contacts", dbc.cnn);
b = Int32.TryParse(cmd.ExecuteScalar().ToString(), out intMax);
intMax += 1;
cmd = new SqlCommand("insert into Contacts values (" + intMax.ToString() + ", " + cboGroup.SelectedValue.ToString() + ", '" + txtContact.Text + "', '" + txtPhone.Text + "', '" + txtEmail.Text + "')", dbc.cnn);
cmd.ExecuteNonQuery();
MessageBox.Show("Record Saved");
}
VB .Net Code
'DbConnect - Class Module
Imports System.Data.SqlClient
Public Class DbConnect
Public cnn As SqlConnection
Public Sub Open()
cnn = New SqlConnection("Integrated Security=sspi;Initial Catalog=ContactManagement")
cnn.Open()
End Sub
Public Function GetGroups() As DataTable
Open()
Dim da As New SqlDataAdapter("select * from ContactGroups", cnn)
Dim ds As New DataSet
da.Fill(ds, "Grp")
Return ds.Tables("Grp")
End Function
Public Function GetContacts() As DataTable
Open()
Dim strSql As String
strSql = "select G.GroupName, C.ContactName, C.Phone, C.Email " strSql += "from ContactGroups G, Contacts C where G.GroupId = C.GroupId"
Dim da As New SqlDataAdapter(strSql, cnn)
Dim ds As New DataSet
da.Fill(ds, "Grp")
Return ds.Tables("Grp")
End Function
End Class
'frmMain
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Close()
End Sub
Private Sub GroupsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupsToolStripMenuItem.Click
Dim fg As New frmGroups
fg.MdiParent = Me fg.Show()
End Sub
Private Sub ContactsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContactsToolStripMenuItem.Click
Dim fc As New frmContacts
fc.MdiParent = Me fc.Show()
End Sub
'frmContactGroups
Imports System.Data.SqlClient
Public Class frmGroups
Dim dbc As New DbConnect
Dim cmd As SqlCommand
Dim intMax As Integer
Dim b As Boolean
Dim be As Boolean = False
Private Sub frmGroups_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load dbc.Open()
IncrementId()
FillGroups()
End Sub
Private Sub IncrementId()
cmd = New SqlCommand("select Max(GroupId) from ContactGroups", dbc.cnn)
b = Int32.TryParse(cmd.ExecuteScalar().ToString(), intMax)
intMax += 1
End Sub
Private Sub FillGroups()
lstGroups.DataSource = dbc.GetGroups
lstGroups.DisplayMember = "GroupName"
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
If be = True Then IncrementId()
End If
cmd = New SqlCommand("insert into ContactGroups values (" + intMax.ToString + ", '" + txtGroup.Text + "')", dbc.cnn)
cmd.ExecuteNonQuery()
MessageBox.Show("Record Saved")
FillGroups()
BlankControls()
be = True
End Sub
Private Sub BlankControls()
txtGroup.Text = "" txtGroup.Focus()
End Sub
End Class
'frmContacts
Imports System.Data.SqlClient
Public Class frmContacts
Dim dbc As New DbConnect
Dim cmd As SqlCommand
Dim intMax As Integer
Dim b As Boolean
Dim be As Boolean = False
Private Sub frmContacts_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FillGroupsCombo()
IncrementId()
FillContactsGrid()
End Sub
Private Sub FillGroupsCombo()
cboGroups.DataSource = dbc.GetGroups
cboGroups.DisplayMember = "GroupName"
cboGroups.ValueMember = "GroupId"
End Sub
Private Sub IncrementId()
cmd = New SqlCommand("select Max(ContactId) from Contacts", dbc.cnn)
b = Int32.TryParse(cmd.ExecuteScalar().ToString(), intMax)
intMax += 1
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
If be = True Then IncrementId()
End If
cmd = New SqlCommand("insert into Contacts values (" + intMax.ToString() + ", " + cboGroups.SelectedValue.ToString + ", '" + txtContact.Text + "', '" + txtPhone.Text + "', '" + txtEmail.Text + "')", dbc.cnn)
cmd.ExecuteNonQuery()
MessageBox.Show("Record Saved")
FillContactsGrid()
be = True
End Sub
Private Sub FillContactsGrid()
DataGridView1.DataSource = dbc.GetContacts
End Sub
Related Blog
Contact Management System VB .Net Windows Generated Code
Tuesday, October 13, 2009
Microsoft Community Tech Day - 24th Oct at ParkCenter, Technopark, Trivandrum
http://k-mug.org/content/ctd09.aspx
Microsoft Community Tech Day - 24th Oct at ParkCenter, Technopark, Trivandrum
Technical Sessions
09.45AM-10.30AM Rapid Prototyping - Lessons from the trenches
10.30AM-11.30AM SQL Server - Tips and Tricks
Tea Break
11.45AM-12.30PM Language Integrated Query (LINQ)
12.30PM-01.30PM Writing Secure Code
Lunch Break
02.30PM-03.30PM Windows Azure & Clouds
03.30PM-04.15PM Windows Presentation Foundation (WPF)
04.15PM-05.00PM Coding For Fun
Tuesday, October 6, 2009
C# Delegates
.Net is coming packed with EventHandler delegate.
public delegate void EventHandler(object? sender, EventArgs e);
+= operator is user to wire up an Event handler to an event.
using System;
using System.Windows.Forms;
using System.Drawing;
class frmHello : Form
{
Button btn;
public static void Main()
{
Application.Run(new frmHello());
}
public frmHello()
{
this.Text = "shalvin.com";
btn = new Button();
btn.Location = new Point(50, 50);
btn.Text = "Hello";
btn.Click += new EventHandler(btn_Click);
this.Controls.Add(btn);
}
private void btn_Click(Object sender, EventArgs e)
{
MessageBox.Show("Welcome to C#");
}
}
Delegate Inference
btnDelInf.Click += BtnDelInf_Click;
private void BtnDelInf_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Delegate Inference");
}
Mentioning the EvenHandler delegate along with event wiring is not not mandatory.
MenuStrip
using System;
using System.Windows.Forms;
using System.Drawing;
class frmHello : Form
{
MenuStrip menuStrip1;
ToolStripMenuItem fileToolStripMenuItem;
ToolStripMenuItem examplesToolStripMenuItem;
ToolStripMenuItem exitToolStripMenuItem;
public static void Main()
{
Application.Run(new frmHello());
}
public frmHello()
{
this.Load += new EventHandler(Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
this.menuStrip1 = new MenuStrip();
this.Controls.Add(menuStrip1);
fileToolStripMenuItem = new ToolStripMenuItem();
fileToolStripMenuItem.Text = "File";
menuStrip1.Items.Add(fileToolStripMenuItem);
examplesToolStripMenuItem = new ToolStripMenuItem();
examplesToolStripMenuItem.Text = "Examples";
menuStrip1.Items.Add(examplesToolStripMenuItem);
exitToolStripMenuItem = new ToolStripMenuItem();
exitToolStripMenuItem.Text = "Exit";
fileToolStripMenuItem.DropDownItems.Add("Exit");
}
}
ListBox
using System.Windows.Forms;
using System;
using System.Drawing;
namespace ConsoleApplication1
{
class Program : Form
{
Label lbl, lblStudents;
TextBox txtName;
Button btn;
ListBox lstStudents;
static void Main(string[] args)
{
Application.Run(new Program());
}
public Program()
{
this.Text = "Shavin.net";
lblStudents = new Label { Text = "2012", Location = new Point(50, 50) };
this.Controls.Add(lblStudents);
lstStudents = new ListBox { Location = new Point(150, 50) };
this.Controls.Add(lstStudents);
lbl = new Label { Text = "Name", Location = new Point(50, 150) };
this.Controls.Add(lbl);
txtName = new TextBox{Location = new Point(150, 150)};
this.Controls.Add(txtName);
btn = new Button();
btn.Text = "Add";
btn.Location = new Point(100, 200);
btn.Click +=new EventHandler(btn_Click);
this.Controls.Add(btn);
}
private void btn_Click(object sender, EventArgs e)
{
lstStudents.Items.Add(txtName.Text);
txtName.Text = "";
txtName.Focus();
}
}
}
Mono
Thursday, September 24, 2009
Gdi+ in Asp .Net Code Snippets
This blog is the continuation of Gdi+ Code Snippets
and Gdi+ Brushes Code Snippets
using System.Drawing.Imaging;
using System.Drawing;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(100, 100);
Graphics g = Graphics.FromImage(bmp);
Pen p = new Pen(Color.Yellow );
g.DrawLine(p, 5, 50, 50, 5);
bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
}
}
Custom Font
using System.Drawing;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(200, 200);
Graphics g = Graphics.FromImage(bmp);
Font f = new Font(FontFamily.GenericSerif, 20, FontStyle.Bold ^ FontStyle.Italic );
g.DrawString("Shalvin.com", f, Brushes.White, 50, 50);
bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
}
Drawing a Pie Chart
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(200, 200);
Rectangle r = new Rectangle(0, 0, 200, 200);
Graphics g = Graphics.FromImage(bmp);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.FillRectangle(Brushes.White, r);
//25%
g.FillPie(Brushes.LightBlue, r, 0, 90);
//50%
g.FillPie(Brushes.LightGreen, r, 90, 180);
//25%
g.FillPie(Brushes.LightGray, r, 270, 90);
bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
}
}
Gdi+ Brushes Code Snippets
This blog is the continuation of Gdi+ Code Snippets
HatchBrush
using System.Drawing.Drawing2D;
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
HatchBrush hb;
hb = new HatchBrush(HatchStyle.DarkHorizontal, Color.Red);
g.FillRectangle(hb, 50, 50, 100, 100);
hb = new HatchBrush(HatchStyle.Cross, Color.Blue, Color.Brown);
g.FillRectangle(hb, 100, 100, 150, 150);
hb = new HatchBrush(HatchStyle.DottedDiamond, Color.Green, Color.Blue);
g.FillRectangle(hb, 150, 150, 200, 200);
}
VB .Net
Imports System.Drawing.Drawing2D
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim g As Graphics = e.Graphics
Dim hb As HatchBrush
hb = New HatchBrush(HatchStyle.DarkHorizontal, Color.Red)
g.FillRectangle(hb, 50, 50, 100, 100)
hb = New HatchBrush(HatchStyle.Cross, Color.Blue, Color.Brown)
g.FillRectangle(hb, 100, 100, 150, 150)
hb = New HatchBrush(HatchStyle.DottedDiamond, Color.Green, Color.Blue)
g.FillRectangle(hb, 150, 150, 200, 200)
End Sub
LinearGradientBrush
using System.Drawing.Drawing2D;
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Brush b = new LinearGradientBrush(new Rectangle(50, 50, 100, 100), Color.Red, Color.Blue, 0, false );
g.FillRectangle(b, 50, 50, 100, 100);
}
VB .Net
Imports System.Drawing.Drawing2D
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim g As Graphics = e.Graphics
Dim b As New LinearGradientBrush(New Rectangle(50, 50, 100, 100), Color.Red, Color.Blue, 0, False)
g.FillRectangle(b, 50, 50, 100, 100)
End Sub
Setting LinearGradientBrush as Background for Form
using System.Drawing.Drawing2D;
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Brush b = new LinearGradientBrush(new Rectangle(0, 0, this.Width, this.Height ), Color.Red, Color.Blue, 0, false);
g.FillRectangle(b, 0, 0, this.Width, this.Height);
}
VB .Net
Imports System.Drawing.Drawing2D
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim g As Graphics = e.Graphics
Dim b As Brush = New LinearGradientBrush(New Rectangle(0, 0, Me.Width, Me.Height), Color.Red, Color.Blue, 0, False)
g.FillRectangle(b, 0, 0, Me.Width, Me.Height)
End Sub
Color.FromArgb
private void btnRed_Click(object sender, EventArgs e)
{
this.BackColor = Color.FromArgb(255, 0, 0);
}
private void btnGreen_Click(object sender, EventArgs e)
{
this.BackColor = Color.FromArgb(0, 255, 0);
}
private void btnBlue_Click(object sender, EventArgs e)
{
this.BackColor = Color.FromArgb(0, 0, 255);
}
private void btnYellow_Click(object sender, EventArgs e)
{
this.BackColor = Color.FromArgb(255, 255, 0);
}
private void btnWhite_Click(object sender, EventArgs e)
{
this.BackColor = Color.FromArgb(255, 255, 255);
}
private void btnBlack_Click(object sender, EventArgs e)
{
this.BackColor = Color.FromArgb(0, 0, 0);
}
Exploring Random Class
Random rnd = new Random();
private void btnBigNumber_Click(object sender, EventArgs e)
{
lblValue.Text = rnd.Next().ToString();
}
private void btnL20_Click(object sender, EventArgs e)
{
lblValue.Text = rnd.Next(10).ToString();
}
private void btn10To20_Click(object sender, EventArgs e)
{
lblValue.Text = rnd.Next(10, 20).ToString();
}
Random Color ScreenSaver
This example makes use of a Timer control.
private void timer1_Tick(object sender, EventArgs e)
{
Random rnd = new Random();
this.BackColor = Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255));
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
Close();
}
Random Multicolor Rectangle Screen Saver and Invalidate Method
Invalidate method is used for a forced repainting of the window. Here I am using Invalidate method in the Timer's Tick event to redraw the rectangle based on the randomized values .
Random rnd = new Random();
private void timer1_Tick(object sender, EventArgs e)
{
Invalidate();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Brush b = new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255)));
g.FillRectangle(b , rnd.Next(this.Width), rnd.Next(this.Height), rnd.Next(this.Width), rnd.Next(this.Height));
}
Custom Pen
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen p = new Pen(Brushes.Blue, 3);
g.DrawLine(p, 50, 50, 200, 50);
}
Working with Bitmaps
private void Form1_Paint(object sender, PaintEventArgs e)
{
Bitmap bmp;
bmp = new Bitmap("d:\\ShalvinSmall.jpg");
Graphics g = e.Graphics;
g.DrawImage(bmp, 50, 50);
}
Saturday, September 12, 2009
Asp .Net Themes and Skins
Asp .Net Themes can be used for getting a consistent look and feel for Asp .Net Controls.
Add a skin file the existing project by selecting Skin from the Add New Item Dialog.
By default themes are added to Asp .Net folder called App_Thmes.
Inside a skin file you can specify the way in which Asp .Net control should render by specifying the properties as attributes to Asp .Net Control tags.
For applying a theme to web page go to document's Theme property and select the theme which you have defined.
Since I have specified properties for TextBox and Label the TextBoxes and Labels will inherit those properties.
Friday, August 28, 2009
Asp .Net Mvc Resources
A list of Asp .Net Mvc Resources which I found very userful.
http://weblogs.asp.net/shijuvarghese/
ASP.NET MVC Tutorials
http://code-inside.de/blog-in/2008/11/25/howto-basics-of-aspnet-mvc-or-why-mvc/
ASP.NET MVC View Overview (C#)
DevConnections - The ASP.NET MVC Framework - Scott Hanselman
ASP.NET MVC using Visual Basic XML Literals
http://www.asp.net/learn/
ASP.NET MVC Preview 4 - Using Ajax and Ajax.Form
Free ASP.NET MVC eBook Tutorial
http://en.wikipedia.org/wiki/Model_View_Controller
http://en.wikipedia.org/wiki/Front_Controller_pattern
http://martinfowler.com/eaaCatalog/pageController.html
http://www.codeasp.net/articles/asp.net/30/aspnet-mvc-framework-tutorial
http://msdn.microsoft.com/en-us/magazine/cc337884.aspx (Old version)
Friday, August 21, 2009
WPF Windows Application, WPF Browser Application and Silverlight Clarified
WPF is Microsoft's presentation technology which is based on XAML.
WPF Windows Application in long run will act as a replacement for Windows Forms.
It runs in fuly trusted environment.
Output
WPF Browser Application
WPF Browser Application or XBAP as name suggest is browser based application. But the drawback is the that it required .net runtime present in the running machine.
It runs in partially trusted environment with limited access to system resources.
Silverlight
Silverlight is Microsoft's alternative to Flash like applications. It is based on plug in model.
Thursday, August 13, 2009
Sql Server 200 Generating Script
An Sql Script is a text file containing the database objects like like table, views, stored procedures etc.
Sql Server 2000 Enterprise Manager or Sql Server 2005/2008 Management Studio can be used for generating Sql Script.
Here is the generated script
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK__Trans__ExpenseId__7C8480AE]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)ALTER TABLE [dbo].[Trans] DROP CONSTRAINT FK__Trans__ExpenseId__7C8480AEGO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK__Expense__GroupId__79A81403]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)ALTER TABLE [dbo].[Expense] DROP CONSTRAINT FK__Expense__GroupId__79A81403GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[spAllExpense]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)drop procedure [dbo].[spAllExpense]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Expense]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[Expense]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ExpenseGroup]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[ExpenseGroup]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Trans]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[Trans]
GO
CREATE TABLE [dbo].[Expense] ( [ExpenseId] [int] IDENTITY (1, 1) NOT NULL , [GroupId] [int] NULL , [ExpenseName] [varchar] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Description] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]
GO
CREATE TABLE [dbo].[ExpenseGroup] ( [GroupId] [int] IDENTITY (1, 1) NOT NULL , [GroupName] [varchar] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Description] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Trans] ( [TranId] [int] IDENTITY (1, 1) NOT NULL , [ExpenseId] [int] NULL , [Date] [datetime] NULL , [Amount] [numeric](8, 2) NULL , [Description] [varchar] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]GO
SET QUOTED_IDENTIFIER ON GOSET ANSI_NULLS ON
GO
create procedure spAllExpense asselect * from Expense
GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON
GO
DataGridView - Extracting the Contents of Currently Selected Row
One of the common requirement is CRUD operation is extracting the contents of the currently selected row in a Grid to be updated in TextBoxes or other controls.
SqlConnection cnn;
SqlDataAdapter da;
DataSet ds = new DataSet();
private void Form1_Load(object sender, EventArgs e)
{
cnn = new SqlConnection("Integrated Security=sspi;Initial catalog=Northwind");
cnn.Open();
da = new SqlDataAdapter("select CategoryId, CategoryName, Description from Categories", cnn);
da.Fill(ds, "Cat");
dataGridView1.DataSource = ds.Tables["Cat"];
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
listBox1.Items.Clear();
for (int colindex = 0; colindex < dataGridView1.Columns.Count ; colindex++)
{
listBox1.Items.Add(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[colindex].Value.ToString());
}
}
DataGridView ComboBox Placing and Child Table Insertion
SqlConnection cnn;
SqlDataAdapter da;
DataSet ds = new DataSet();
SqlCommand cmd;
SqlDataReader dre;
DataGridViewTextBoxColumn newCol;
DataGridViewComboBoxColumn cboCol;
private void Form1_Load(object sender, EventArgs e)
{
cnn = new SqlConnection("Integrated security=sspi;INitial Catalog=ContactManagement;Data Source=.\\shalvin");
cnn.Open();
da = new SqlDataAdapter("select * from ContactGroups", cnn);
da.Fill(ds, "Grp");
CreateGridViewColumns();
}
private void CreateGridViewColumns()
{
cboCol = new DataGridViewComboBoxColumn();
cboCol.DataSource = ds.Tables["Grp"];
cboCol.DisplayMember = "GroupName";
dataGridView1.Columns.Add(cboCol);
newCol = new DataGridViewTextBoxColumn();
dataGridView1.Columns.Add(newCol);
}
private void btnSave_Click(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
string strGroupId = "";
DataGridViewRow row = dataGridView1.Rows[i];
string strSql;
strSql = "select GroupId from ContactGroups where GroupName= '"
+ row.Cells[0].Value.ToString() + "'";
cmd = new SqlCommand(strSql, cnn);
dre = cmd.ExecuteReader();
while(dre.Read())
strGroupId = dre["GroupId"].ToString();
dre.Close();
cmd = new SqlCommand("Insert into Contacts (GroupId, ContactName) values ('" + strGroupId + "', '" + row.Cells[1].Value.ToString() + "')", cnn);
cmd.ExecuteNonQuery();
}
MessageBox.Show("Record saved");
}
Related Blog
Windows Forms ListView
Filling Windows Forms ListView with DataTable