Windows Controls (part 7)

Web Browser

ဟိုဘက်က ADO.NET ဘက် လှည့်လိုက်တာ ဒီဘက်မှာ နဲနဲ ပြတ်သွားတယ်.. ဒါကြောင့် ဒီတစ်ခါ နဲနဲ အထူးအဆန်းလေး ပြမယ်.. သိတဲ့သူတွေကတော့ ငြိမ်ငြိမ်နေပေါ့ဗျာ.. မသိတဲ့သူတွေကို မျက်လှည့်ပြလိုက်အုံးမယ်.. 😀 ဒီတစ်ခါ ခေါင်းလဲ သိပ်မစားမဲ့ မထင်ထားတဲ့ အရာတစ်ခု ပြပါမယ်။ အဲဒါက web browser တစ်ခုကို ဘယ်လို ဆွဲမယ်ဆိုတာပါပဲ။ web browser ကို တကယ်ဆွဲဖို ့ကတော့ ခေါင်းစားမှာပါ.. ဒါပေမဲ့ အခုက ဘယ်လိုလုပ်လို ့ရနိုင်မလဲ ဒါတွေက ဒီလိုပါလား ဆိုတဲ့ sense လေးကို သိဖို ့ အတွက်တော့ ခေါင်းသိပ်မစားဘူး လို ့ဆိုလိုတာပါ…

Namespace: System.Windows.Forms

web browser ဆိုတာ web pages တွေ display and host ပေ့ါဗျာ.. သူ ့မှာ အခြား controls တွေလိုပဲ methods တွေ၊ properties တွေ၊ events တွေ များစွာ ရှိပါတယ်။ သူရဲ ့လက်တွေ ့အသုံးဝင်တဲ့ methods အချို ့ကို အောက်မှာ ရေးပြပါမယ်။

  • Url
  • Navigate
  • GoForward
  • GoBack
  • Stop
  • Refresh
  • GoHome
  • GoSearch
  • Print

ကဲ Sample တစ်ခုလောက် ကြည့်ရအောင်။

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

public class MainClass
{
[STAThread]
public static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.Run(new MyWebBrowser());
}
}

public class MyWebBrowser : Form
{
public MyWebBrowser()
{
InitializeComponent();
webBrowser.Navigate(“http://www.myanmarfamily.org”);
}

private WebBrowser webBrowser;
private Label lblAddress;
private TextBox AddressBox;
private Button goButton;
private Button forwardButton;
private Button backwardButton;

private void InitializeComponent()
{
this.webBrowser     = new WebBrowser();
this.lblAddress     = new Label();
this.AddressBox     = new TextBox();
this.goButton       = new Button();
this.forwardButton  = new Button();
this.backwardButton = new Button();

// webBrowser
webBrowser.Anchor = ((AnchorStyles)((((AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left) | AnchorStyles.Right)));
webBrowser.Location = new Point(16, 42);
webBrowser.Size = new Size(917, 524);
webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);

// lblAddress
lblAddress.AutoSize = true;
lblAddress.Location = new Point(15, 11);
lblAddress.Size = new Size(60, 17);
lblAddress.Text = “Address”;

// AddressBox
AddressBox.Anchor = ((AnchorStyles)(((AnchorStyles.Top | AnchorStyles.Left) | AnchorStyles.Right)));
AddressBox.Location = new Point(77, 7);
AddressBox.Size = new Size(500, 22);
AddressBox.TabIndex = 0;

// goButton
goButton.Anchor = ((AnchorStyles)((AnchorStyles.Top | AnchorStyles.Right)));
goButton.Location = new Point(587, 5);
goButton.Size = new Size(100, 28);
goButton.TabIndex = 1;
goButton.Text = “Go”;
goButton.Click += new EventHandler(goButton_Click);

// forwardButton
forwardButton.Anchor = ((AnchorStyles)((AnchorStyles.Top | AnchorStyles.Right)));
forwardButton.Enabled = false;
forwardButton.Location = new Point(807, 5);
forwardButton.Size = new Size(100, 28);
forwardButton.Text = “Forward >>”;
forwardButton.Click += new EventHandler(forwardButton_Click);

// backButton
backwardButton.Anchor = ((AnchorStyles)((AnchorStyles.Top | AnchorStyles.Right)));
backwardButton.Enabled = false;
backwardButton.Location = new Point(697, 5);
backwardButton.Size = new Size(100, 28);
backwardButton.Text = “<< Back”;
backwardButton.Click += new EventHandler(backwardButton_Click);

// Form
this.ClientSize = new Size(949, 581);
this.Controls.Add(goButton);
this.Controls.Add(AddressBox);
this.Controls.Add(lblAddress);
this.Controls.Add(webBrowser);
this.Controls.Add(forwardButton);
this.Controls.Add(backwardButton);
this.Text = “GreenLeaf’s WebBrowser”;
}

private void goButton_Click(object sender, EventArgs e){
webBrowser.Navigate(AddressBox.Text);
}

private void forwardButton_Click(object sender, EventArgs e){
webBrowser.GoForward();
}

private void backwardButton_Click(object sender, EventArgs e){
webBrowser.GoBack();
}

private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
AddressBox.Text = webBrowser.Url.ToString();

if (webBrowser.CanGoBack)
{ backwardButton.Enabled = true; }
else
{ backwardButton.Enabled = false; }

if (webBrowser.CanGoForward)
{ forwardButton.Enabled = true; }
else
{ forwardButton.Enabled = false; }
}
}

Output


Figure1_1 နဲ ့ Figure1_2 နဲ ့ Figure1_3 မှာ တစ်ချက် ကြည့်ကြည့်..

 


Figure1_1

 


Figure1_2

 


Figure1_3

 

This is pretty cool .. huh ??

How To Works

 

ဆိုတော့ code ကို လေ့လာကြည့်ရအောင်.. ထုံးစံအတိုင်း InitializeComponent() ထဲမှာ Form နဲ ့ Controls တွေရဲ ့ properties တွေ ရေးမှာပေါ့ဗျာ.. ဒီ properties တွေက အရင် post တွေမှာ ရှင်းပြပြီးသား ဖြစ်ပါတယ်။ ဒါပေမဲ့ နဲနဲ စွဲသွားအောင် တချို ့တလေ အရေးကြီးတာလေးတွေ ပြန်ပြောပြမယ်။ ဥပမာ

webBrowser.Anchor = ();
အဲဒီ့မှာ Anchor ဆိုတဲ့ property က ကျနော်တို ့form ကို resize ပေါ့လေ mouse နဲ ့ ချံု၊ ချဲ ့လုပ်တဲ့အခါ form ထဲက control တွေပါ resize အတိုင်း အကြီးအသေးပါနိုင်အောင် သူ ့ကို အသုံးပြုရပါတယ်။ Location ဆိုတာကတော့ controls တွေကို နေရာချထားမှုပေါ့ဗျာ.. (x,y) coordinate ကို အသုံးပြုပါတယ်။ သချင်္ာက (x,y) coordinate မဟုတ်ပါဘူး.. Display screen ကို programming မှာ ကိုယ့်ဘာသာ သတ်မှတ်ထားတဲ့ unit နဲ ့ပါ။ Display Screen (e.g., LCD) ရဲ ့ အပေါ်ဘယ်ထောင့်က (x,y) coordinate (0,0) ဖြစ်ပါတယ်။ x coordinate က ညာဘက်ကို အပေါင်း ကိန်းပြည့်တန်ဖိုး တိုးသွားပြီးတော့ y coordinate က အောက်ဘက်ကို အပေါင်းကိန်းပြည့် တန်ဖိုး တိုးသွားပါတယ်။ ကျန်တဲ့ property တွေကတော့ ပြောစရာမလိုတော့ဘူးထင်တာပဲ..


goButton.Click += new EventHandler(goButton_Click);

private void goButton_Click(object sender, EventArgs e)
{
webBrowser.Navigate(AddressBox.Text);
}

Navigate() method ကို အသုံးပြုသွားတယ်ဆိုရုံပါပဲ။ ဘာမှကို မပါလို ့ ဘာမှကို ရှင်းပြစရာ မလိုဘူး ထင်ပါတယ်။ သူ ့အတိုင်းကို ရှင်းနေပြီလေ။ ထပ်ရှင်းလိုက်ရင် တော်ကြာ …. အဲ သူ ့မှာ AddressBox.Text ဆိုတာက ကျနော်တို ့ အပေါ်က ရိုက်လို ့ရတဲ့ TextBox (ရိုက်လို ့ရတာဆိုလို ့ သူ တစ်ခုတည်းရှိပါတယ်) ထဲမှာ ကျနော်တို ့ရိုက်သမျှ Url ကို WebBrowser Class ရဲ ့ Navigate() method အကူအညီနဲ ့ display လုပ်ပေးပါလိမ့်မယ်။

private void forwardButton_Click(object sender, EventArgs e)
{
webBrowser.GoForward();
}

private void backwardButton_Click(object sender, EventArgs e)
{
webBrowser.GoBack();
}

ဒီနှစ်ခုကတော့ မပြောတော့ဘူးဗျာ.. မြင်တဲ့အတိုင်းပဲလေ..

private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
AddressBox.Text = webBrowser.Url.ToString();

if (webBrowser.CanGoBack)
{
backwardButton.Enabled = true;
}
else
{
backwardButton.Enabled = false;
}

if (webBrowser.CanGoForward)
{
forwardButton.Enabled = true;
}
else
{
forwardButton.Enabled = false;
}
}

ဒီကောင်လေးကလဲ သိပ်ပြောစရာတော့ မရှိပါဘူး… ကျနော်တို ့ forwardButton, backButton ရဲ ့ property မှာ

forwardButton.Enabled = false;
ဆိုပြီး ပေးခဲ့တယ်။ သဘောက ပထမဆုံး Browser ဖွင့်လိုက်တဲ့အချိန်မှာ Home Page တက်လာမယ်။ အဲအချိ်န်မှာ အရင် page တွေ၊ နောက် page တွေ မရှိသေးဘူးလေ။ ဟုတ်တယ်မို ့လား.. ဒါကြောင့် ဒီ button နှစ်ခုကို ပထမမှာ  disable ပေးထားမယ်။ နောက် home page ကနေမှ တခြား page တစ်ခုကို သွားပြီဆိုတော့မှ ဒီနှစ်ကောင်ကို enable ပေးလိုက်မယ်။ ဒီ method မှာ ရေးထားတာ အဲဒါပါပဲ..

Home page ကို

public MyWebBrowser()
{
InitializeComponent();
webBrowser.Navigate(“http://www.myanmarfamily.org”);
}

ဒီ MyWebBrowser class constructor မှာ တစ်ခါတည်း ပေးထားလိုက်ရင် ရပါတယ်။

Remark
အပေါ်မှာ အသုံးဝင်တဲ့ method လေးတွေ ပြောခဲ့ပါတယ်။ ဒါပေမဲ့ code ထဲမှာ ယူသုံးသွားတာက Url, GoBack, GoForward လောက်ပဲ သုံးထားပါတယ်။ ဒါကြောင့် ကျန်တဲ့ GoHome တို ့၊ GoSearch တို ့၊ Refresh တို ့ကို  ခင်ဗျားစမ်းဖို ့အတွက် ချန်ထားခဲ့ပါတယ်။…

 

 

 

 

Ok, Happy Coding!!
Thanks for reading!!
See u around, buddy!!

Sql server, C# and ADO.NET (Final part)

ဒီဆွေးနွေးချက်ကို လတ်စသတ်လိုက်ရအောင်… ဒီတစ်ခါ နောက်ဆုံး data တွေကို update နဲ ့ delete လုပ်ကြည့်ရအောင်… form ပေါ်မှာ update နဲ ့ delete အတွက် button အတွက် နေရာချပြီးတာနဲ ့ အောက်က code တွေ ထည့်ပြီး trace လိုက်ကြည့်ပါ.. လွယ်လွယ်ကူကူပါပဲ.. ဘယ်လိုအလုပ်လုပ်တယ်ဆိုတာတော့ မရေးတော့ပါဘူး.. အရင်ပို ့စ်တွေ ဖတ်ပြီးသားဆိုရင် နားလည်ပြီးသားနေမှာပါ..

Update button အတွက် code

private void btnUpdate_Click(object sender, EventArgs e)
{
da.UpdateCommand = new SqlCommand("Update SAMPLE set FIRSTNAME = @FIRSTNAME, LASTNAME = @LASTNAME Where ID = @ID", conn);
da.UpdateCommand.Parameters.Add("@FIRSTNAME", SqlDbType.VarChar).Value = txtFirstName.Text;
da.UpdateCommand.Parameters.Add("@LASTNAME", SqlDbType.VarChar).Value = txtLastName.Text;
da.UpdateCommand.Parameters.Add("@ID", SqlDbType.Int).Value = da.Tables[0].Rows[bindsource.Position][0];

conn.Open();
int x = da.UpdateCommand.ExecuteNonQuery();
conn.Close();

if (x>=1)
MessageBox.Show("Record(s) has been updated");
}

Delete Button အတွက် Code

private void btnDelete_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Are you sure?\nThere is no undo once data is deleted.", "Confirm Message", MessageBoxButtons.YesNo);

if (result == DialogResult.Yes)
{
da.DeleteCommand = new SqlCommand("DELETE FROM SAMPLE Where ID = @ID", conn);
da.DeleteCommand.Parameters.Add("@ID", SqlDbType.Int).Value = ds.Tables[0].Rows[bindsource.Position][0];

conn.Open();
da.DeleteCommand.ExecuteNonQuery();
conn.Close();
}
else
{
MessageBox.Show("Delection Canceled.");
}
}

Sql Server, C# and ADO.NET(part 2)

ပြီးခဲ့တဲ့ part 1 မှာ Sql server ထဲကို data ထည့်သွင်းခြင်းကို ရှင်းပြခဲ့ပြီး ဖြစ်ပါတယ်… ဒီအပိုင်းမှာ အဲဒီ့ database ကို  form ပေါ်မှာ မြင်နိုင်အောင် DataGridView သုံးပြီးတော့ display လုပ်ပါမယ်.. ကဲ ကြည့်ရအောင်..

ကျနော်တို ့part 1 မှာတုန်းက Form ပေါ်မှာ Figure1_1 မှာ ပြထားသလို Sql Server ထဲကို data တွေ ထည့်သွင်းထားတယ် ဆိုပါစို ့..

အဲဒါကို Form ပေါ်မှာ display ဘယ်လိုလုပ်မလဲဆိုတာ ကြည့်ပါ… ပထမဆုံး Figure1_2 မှာ ပြထားတဲ့အတိုင်း Form ပေါ်မှာ Button အသစ်တစ်ခု ထပ်ထည့်လိုက်ပါအုံး..


Figure1_2

button’s property အနေနဲ ့ Name ကို btnDisplay နဲ ့ Text မှာ Display လို ့ပြင်ရေးလိုက်ပါ…
ပြီးရင် button ကို double click ပေးပြီး သူ ့အလုပ်လုပ်ပုံကို ရေးရအောင်..

SqlConnection conn = new SqlConnection(“Data Source=MRDREAM\\SQLEXPRESS;” +
“Initial Catalog=SQLSAMPLE; Integrated Security=SSPI”);
SqlDataAdapter da = new SqlDataAdapter();

ပထမဆုံး connection ချိတ်မယ်.. ပြီးရင် dataAdapter ကို create လုပ်မယ်.. part 1 က Add Record button အလုပ်လုပ်ပုံမှာ ရှင်းပြပြီးသားဖြစ်တဲ့အတွက် ထပ် မရှင်းတော့ပါဘူး..

DataSet ds = new DataSet();

ADO.NET မှာ data source ထဲက data တွေဟာ dataAdapter အကူအညီနဲ ့ DataSet ထဲကို ရောက်ပါတယ်။ dataSet ထဲက data တွေကိုမှ Form Controls တွေပေါ် data binding လုပ်လိုက်တာပါ..
ဒါကြောင့် အခု DataSet ကို create လုပ်လိုက်ပါတယ်..

da.SelectCommand = new SqlCommand(“SELECT * FROM SAMPLE”, conn);

part 1 မှာတုန်းက sql server ထဲက database ထဲကို data တွေ ထည့်သွင်းမှာဖြစ်တဲ့အတွက်
dataAdapter.InsertCommand ကို အသုံးပြုခဲ့တာ မှတ်မိမှာပါ.. အခုက database ထဲက ဟာကို Form Controls ပေါ် တင်မှာဖြစ်တဲ့အတွက် dataAdapter.SelectCommand နဲ ့ အသုံးပြုပါမယ်။
SELECT * FROM SAMPLE  မှာ SAMPLE က database ရဲ ့  Table name၊ * ဆိုတာ Table ထဲမှာ ရှိသမျှ data အားလုံး…

ds.Clear();

DataSet ကို clear လုပ်ပေးတယ်ဆိုတော့ ဘာအတွက်လဲ မေးစရာ ရှိပါတယ်.. သူ ့ကို clear ပေးမထားရင် data အသစ် တစ်ခု ထည့်လိုက်တာနဲ ့ အရင် data တွေပါ ပြန်ပြီး display ထပ်လုပ်ပါလိမ့််မယ်.. အဲလိုမဖြစ်ရအောင်လို ့ကာကွယ်ထားတာပါ.. မသိသေးသူများ ရှေ ့မှာ comment လေးပိတ်ပြီး run ကြည့်ပါ…

da.Fill(ds);

dataAdapter နဲ ့ DataSet ထဲကို data တွေ Fill ဖြည့်လိုက်တာပါ..

dataGridView1.DataSource = ds.Tables[0];

နောက်ဆုံး DataGridView ရဲ ့ DataSource ထဲကို DataSet ထဲက dataTables တွေကို ထည့်လိုက်ခြင်းအားဖြင့် Form ရဲ ့ DataGridView ပေါ်မှာ မြင်ရပါလိမ့်မယ်..

ခုရေးခဲ့တဲ့ Display button ရဲ ့ code ပါ။

private void btnDisplay_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(“Data Source=MRDREAM\\SQLEXPRESS;” +
“Initial Catalog=SQLSAMPLE; Integrated Security=SSPI”);
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();

da.SelectCommand = new SqlCommand(“SELECT * FROM SAMPLE”, conn);
ds.Clear();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}

Output


Figure1_3

ကျနော်တို ့ data အသစ်လေးတွေ ထပ်ထည့်ကြည့်မယ်.. Figure1_4 မှာ ကြည့်ပါ…


Figure1_4

ဆက်ရန် (part 3)

ok!
Thanks for reading!!
See u around, buddy!!!

Sql Server, C# and ADO.NET (part 1)

C# နဲ ့ sql server အသုံးပြုပြီးတော့
* database ထဲကုုိ data တွေ ဘယ်လိုပို ့မလဲ
* database ထဲက data တွေကို form ပေါ်မှာ ဘယ်လိုပြမလဲ
* database ထဲက data တွေကို ဘယ်လို update လုပ်မလဲ
* database ထဲက data တွေကို ဘယ်လို delete လုပ်မလဲ
* BindingSource ကို ဘာအတွက် အသုံးပြုမှာလဲ
အဲဒါတွေကို အပိုင်းလိုက် ခွဲပြီးတော့ ရှင်းပြပါမယ်။

Continue reading “Sql Server, C# and ADO.NET (part 1)”