Hello readers.
This is our first post that matches our intention.
Here i'm describing you all how to
- use an XML file in our project.
- Why we should use XML file.
- Any better option.
So here we go.
The framework used is .NET Framework 2.0 Language used is c#
This is how it gonna look like.
[caption id="attachment_14" align="aligncenter" width="300" caption="A link Collection with browser"]

So Let's start from designing. Follow the steps
- Open Visual studio or Visual c# express edition whatever you use for development purpose.
- Create a new project by going file->new-> create project
- Choose language c# and windows application as project.
- It'll open a tab with a blank windows form Now go to toolbox and drop following items into your window form web browser, two combo box, button, menustrip,progressbar.
- To use our code the names of various controls must be same.. Name them as follows
- WebBrowser-webBrowser1
- Longer combobox used for link selection-cmbLink
- Smaller Combobox used for Catagoy Collection-cmbCatagory
- Button-btnGo.
- progressbar-progressBar1
- IN menu strip make following menus, Go HOme, Forward, Backward
- You can assign shortcut keys for them (i haven't )
- In text Propert of Button Write Go.
double click any where in form but outside any control.
you should be redirected to a .cs page and inside the funtion
private void Form1_Load(object sender, EventArgs e)
{
}
This is an event which get triggered when the form start loading.This way you actually create a class named Form1 you can see a constructor of this class Form1() in the same .cs page.
Now let's start adding code
following code you can add in both Constructor(but after function call initalizeComponent() ), or in the Load event of the form.
string strpathxml = "xmltest.xml"; // this is a path o our XML fileThis event get triggered when we select any index from combo box in our case any catagory.
DataSet ds = new DataSet();/* we have Taken a Dataset object to store information that we are going to extract from XML doc.*/'
//You can read more about Data Set by clicking here
ds.ReadXml(strpathxml); /* here we read the xml file*/
string home = "http://sanketdhingra.blogspot.com/"; /* this is the link to sam's blog. I have made this Home page of this project*/
XmlDataDocument xmldatadoc = new XmlDataDocument(ds); /* here we have taken object of XmlDataDocument it allows structured data to be stored, retrived and manipulated through Dataset*/
XmlNode[] nodeno = new XmlNode[19]; /* this is object of XmlNode that actually contains 19 nodes these are for the categories of our links*/
for (int i = 0; i < 19; i++)
{
string pathquerry = "/NewDataSet/Browser[Id=" + (i + 1).ToString() + "]";/* this is the query that will give us the categories from XmlDataDocument.*/
nodeno[i] = xmldatadoc.SelectSingleNode(pathquerry);/* here we are getting Categories in our XmlNode Object*/
cmbCatagory.Items.Add(nodeno[i].ChildNodes[1].InnerText.ToString()); /* Here i'm inserting catagories in my ComboBox of category ie in cmbCatagory.*/
}
ds.WriteXml(strpathxml, XmlWriteMode.WriteSchema);/* this actually writes the schema of xml in my xml file*/
webBrowser1.Navigate(new Uri(home));/* now we are navigating to the home page which is Sam's Blog.*/
So by now we just loaded our window which opens a webpage and load a combobox with cataories of link.
Now what we have to do is to when i choose any category from cmbCatagory corresponding links should be filled into cmbLink( combobox of links). for this do following
- click on cmbCatagory for selecting it and go to properties bar there are option for proerty or even go to event and double click on the event selected index changed.
- You'll be redirected to .cs page with function private void cmbCatagory_SelectedIndexChanged(object sender, EventArgs e){}
- add following codes
int id = cmbCatagory.SelectedIndex;/* int that tells which catagory number is seleted (it is on the '0' base)*/So by now we are done with inserting specific links that matches the categories. The project is almost complete We are just left with some finishing.
cmbLink.Text = "Seelect a link";/*We are adding text in our combox of link "Select a link"*/
string strpathxml = "xmltest.xml";/*path to our Xml Doc*/
DataSet ds = new DataSet();/* taking dataset as before*/
ds.ReadXml(strpathxml);/*reading xml file*/
XmlDataDocument xmldatadoc = new XmlDataDocument(ds);
XmlNode xnd;
string querry = "/NewDataSet/Link[Linkid=" + (id+1).ToString() + "]";/* this is query to select links against selected category*/
xnd = xmldatadoc.SelectSingleNode(querry);/*filling our Xml Node*/
int lnkid = Convert.ToInt32(xnd.ChildNodes[2].InnerText.ToString());/* here we get an Id from where the link o selected category starts*/
string querry1 = "/NewDataSet/Link[Linkid=" + (id+2).ToString() + "]";
xnd = xmldatadoc.SelectSingleNode(querry1);
int temp = Convert.ToInt32(xnd.ChildNodes[2].InnerText.ToString());/* here we get where links to next category starts*/
XmlNode[] nodeno = new XmlNode[80];//*here i'm taking aray of xmlnode of size 80, it is just because in future i'll be able to extend links, you can choose list too that'll be dynamic but as a basic application i have chosen this*/
for (int i = 0; i < 81; i++) /* in this loop i'm clearing all the items that are currently inside cmbLink. ie cleaning our link combobox*/
{
try
{
cmbLink.Items.RemoveAt(i);
--i;
}
catch(Exception x){}
}
for (int i = lnkid; i < temp; i++)/* here i'm filling my Combobox with Links*/
{
querry = "/NewDataSet/Link[id=" + i.ToString()+"]";
nodeno[i] = xmldatadoc.SelectSingleNode(querry);
if (nodeno[i]!=null)
{
cmbLink.Items.Add(nodeno[i].ChildNodes[0].InnerText.ToString());
}
}
- go to form1 design view select web browser
- go to property bar
- events
- double click on event named document completed and progress changed.
- add following code
- in DocumntCompleted event add following codes
cmbLink.Text = webBrowser1.Url.ToString(); /* writing link of the current web page into link combobox*/
progressBar1.Visible = false;/* setting visibility of progres bar to fales*/
- in progresschanged event add following codes
progressBar1.Visible = true;/*setting visibilty of progressbar to true to show the progress*/
progressBar1.Maximum = Convert.ToInt32(e.MaximumProgress);//*these are progress bar parameters*/
progressBar1.Value = Convert.ToInt32(e.CurrentProgress);Now we are left with coding of various buttons
Double click on go button, You'll be redirected to a function which is a click event of Button
add following codes
string link = cmbLink.Text.ToString();This is the for browsing to the link which is inside cmbLink
int q = link.Length;
string temp = link.Remove(7, q - 7);
if (temp != "http://" && temp != "Http://" && temp != "Http:///" && temp != "http:///")
{
link = "http://" + link;
webBrowser1.Navigate(new Uri(link));
}
else
{
webBrowser1.Navigate(new Uri(link));
}
Double click on menustrip Home add following codes in its click event
string home = "http://sanketdhingra.blogspot.com/";Double click on menustrip Forward add following codes in its click event
webBrowser1.Navigate(new Uri(home));
webBrowser1.GoForward();Double click on menustrip Backward add following codes in its click event
webBrowser1.GoBack();Namespace used are (using statemens)
using System;So here we are done with our project. press f5, If you are did exactly as written here you will not get any kind of error and your project will run easily.
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
This project you can refer s to get start with .Net windows programming. Hope u liked it.
thanks keep browsing for more.
If this is useful then please subscribe. Your suggestions and questions are respectfully invited.
Thank You
Hope to see you soon.
No comments:
Post a Comment