Showing posts with label Winforms. Show all posts
Showing posts with label Winforms. Show all posts

Apr 26, 2012

How to opean and read xml file attributes in c#.net using for each loop

XML File:



 <books>
  <book name="let us c" price="300" author="ykanitkar" />
  <book name="let us c++" price="1300" author="Vamsi" />
  </books>

Source Code:

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

namespace Xml_ex
{
    public partial class Readxml : Form
    {
        public Readxml()
        {
            InitializeComponent();
        }

        private void Readxml_Load(object sender, EventArgs e)
        {

        }

        

        private void button2_Click(object sender, EventArgs e)
        {

            XmlDocument xmldoc = new XmlDocument();
            XmlNodeList xmlnodes;
            XmlNode node;
            string strDocelement = "";
            int i = 0;
            string str = null;
            string strXmlLoc = "C:\\Users\\vmunnamgi3\\Desktop\\books.xml";
            xmldoc.Load(strXmlLoc);
            node = xmldoc.DocumentElement;
            if (node.HasChildNodes)
            {
                foreach (XmlNode xmlWrknode in node.ChildNodes)
                {

                   // str = xmlWrknode.Attributes.GetNamedItem("name").Value.ToString().Trim();
                    str = xmlWrknode.Attributes.GetNamedItem("name").Value.ToString().Trim() + "_" + xmlWrknode.Attributes.GetNamedItem("price").Value.ToString().Trim() + "_" + xmlWrknode.Attributes.GetNamedItem("author").Value.ToString().Trim();
                    MessageBox.Show(str);
                }
            }
        }
    }
}

How to opean and read xml file in c#.net using for each loop

XML File:



<Table>
<Product0>
<Product>
<Product_id>1</Product_id>
<Product_name>Product 1</Product_name>
<Product_price>1000</Product_price>
</Product>
<Product>
<Product_id>2</Product_id>
<Product_name>Product 2</Product_name>
<Product_price>2000</Product_price>
</Product>
<Product>
<Product_id>3</Product_id>
<Product_name>Product 3</Product_name>
<Product_price>3000</Product_price>
</Product>
<Product>
<Product_id>4</Product_id>
<Product_name>Product 4</Product_name>
<Product_price>4000</Product_price>
</Product>
</Product0>
<Product1>
<Product>
<Product_id>11</Product_id>
<Product_name>Product 11</Product_name>
<Product_price>11000</Product_price>
</Product>
<Product>
<Product_id>21</Product_id>
<Product_name>Product 21</Product_name>
<Product_price>12000</Product_price>
</Product>
<Product>
<Product_id>31</Product_id>
<Product_name>Product 31</Product_name>
<Product_price>13000</Product_price>
</Product>
<Product>
<Product_id>41</Product_id>
<Product_name>Product 41</Product_name>
<Product_price>14000</Product_price>
</Product>
</Product1>

</Table>


Source Code:





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

namespace Xml_ex
{
    public partial class Readxml : Form
    {
        public Readxml()
        {
            InitializeComponent();
        }

        private void Readxml_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            XmlDocument xmldoc = new XmlDocument();
            XmlNodeList xmlnodes;
            XmlNode node;
            string strDocelement = "";
            int i = 0;
            string str = null;
            string strXmlLoc = "C:\\Users\\vamsi\\Desktop\\products.xml";
            xmldoc.Load(strXmlLoc);
            node = xmldoc.DocumentElement;
            if (node.HasChildNodes)
            {
                foreach (XmlNode xmlWrknode in node.ChildNodes)
                {
                    foreach (XmlNode xmlsubWrknode in xmlWrknode.ChildNodes)
                    {
                        foreach (XmlNode xmlsemisubWrknode in xmlsubWrknode.ChildNodes)
                        {

                            str = xmlsemisubWrknode.InnerText.ToString().Trim();
                            MessageBox.Show(str + "-");
                        }
                    }
                }
            }
           
        }
    }
}


Apr 25, 2012

How to open and read an XML file in C#

Xml file:


<Table>

<Product>

<Product_id>1</Product_id>

<Product_name>Product 1</Product_name>

<Product_price>1000</Product_price>
</Product>

<Product>

<Product_id>2</Product_id>

<Product_name>Product 2</Product_name>

<Product_price>2000</Product_price>
</Product>

<Product>

<Product_id>3</Product_id>

<Product_name>Product 3</Product_name>

<Product_price>3000</Product_price>
</Product>

<Product>

<Product_id>4</Product_id>

<Product_name>Product 4</Product_name>

<Product_price>4000</Product_price>
</Product>
</Table>


Source code for C#:

using System;
using System.Data;
using System.Windows.Forms;
using System.Xml;
using System.IO; 

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

        private void button1_Click(object sender, EventArgs e)
        {
            XmlDataDocument xmldoc = new XmlDataDocument();
            XmlNodeList xmlnode ;
            int i = 0;
            string str = null;
            string strXmlLoc=null;
          string strXmlLoc = "C:\\Users\\Desktop\\products.xml";
            xmldoc.Load(strXmlLoc);
           
            xmlnode = xmldoc.GetElementsByTagName("Product");
            for (i = 0; i <= xmlnode.Count - 1; i++)
            {
                xmlnode[i].ChildNodes.Item(0).InnerText.Trim();
                str = xmlnode[i].ChildNodes.Item(0).InnerText.Trim() + " | " + xmlnode[i].ChildNodes.Item(1).InnerText.Trim() + " | " + xmlnode[i].ChildNodes.Item(2).InnerText.Trim();
                MessageBox.Show (str);
            }
        }
    }
}

Apr 24, 2012

Checkbox in datagrid in windows application

use this code:
DataGridViewCheckBoxColumn chkcolumn = new DataGridViewCheckBoxColumn();
chkcolumn.Name = "Select";
chkcolumn.HeaderText = "Select";
DataGrid1.Columns.Add(chkcolumn);//For adding to the last index.
DataGrid1.Columns.Insert(0,chkcolumn)//For adding to the first index.

How Read XML file in C#


This article will guide you how to display XML content in Gridview

XMLFile content
<ProductList>
    <Products>
        <ProductId>1</ProductId>
        <ProductName>Aniseed Syrup</ProductName>
        <UnitPrice>27</UnitPrice>
    </Products>
    <Products>
        <ProductId>2</ProductId>
        <ProductName>Chef Anton's Cajun Seasoning</ProductName>
        <UnitPrice>20</UnitPrice>
    </Products>
    <Products>
        <ProductId>3</ProductId>
        <ProductName>Queso Cabrales</ProductName>
        <UnitPrice>28</UnitPrice>
    </Products>
    <Products>
        <ProductId>4</ProductId>
        <ProductName>Alice Mutton</ProductName>
        <UnitPrice>24</UnitPrice>
    </Products>
    <Products>
        <ProductId>5</ProductId>
        <ProductName>Sasquatch Ale</ProductName>
        <UnitPrice>30</UnitPrice>
    </Products>
</ProductList>

In Web Forms



using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(Server.MapPath("Products.xml"));
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }      
    }
}

In Windows Forms




using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Read_XML_file_Windows_Forms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            DataSet ds = new DataSet();
            ds.ReadXml("C:\\Products.xml");
            dataGridView1.DataSource = ds.Tables[0];
        }
    }
}



Jun 14, 2011

How Do I Check/Uncheck all CheckBoxes on a Form


private void chkSelectall_CheckedChanged(object sender, EventArgs e)
        {
            if (chkSelectall.Checked == true)
            {
                MessageBox.Show("checked");
check check boxes:

                CheckBox chkbox;
             
                foreach (Control c in this.Controls)
                {
                    if (c is CheckBox)
                    {
                        chkbox = (CheckBox)c;
                        chkbox.CheckState = CheckState.Checked;
                     
                    }
                }



else
            {
           
uncheck check boxes:
                MessageBox.Show("not cheked");


                CheckBox chkbox;
                foreach (Control c in this.Controls)
                {
                    if (c is CheckBox)
                    {
                        chkbox = (CheckBox)c;
                        chkbox.CheckState = CheckState.Unchecked;

                    }
                }


if you have any panel or group box add this code also

uncheck check boxes:

 foreach (Control c in grpLogFiles.Controls)
                {
                    if (c is CheckBox)
                    {
                        chkbox = (CheckBox)c;
                        chkbox.CheckState = CheckState.Unchecked;

                    }
                }
check check boxes:


 foreach (Control c in grpLogFiles.Controls)
                {
                    if (c is CheckBox)
                    {
                        chkbox = (CheckBox)c;
                        chkbox.CheckState = CheckState.Checked;

                    }
                }






May 5, 2011

Opening & Closing Notepad using .Net

A program is not Userinterface and database connections alone, it also needs to interact with other applications. Many a times, this interaction happens internally, like updating Word Template, Printing out a document etc, but there are times where one needs to open an document through the Application.

Let us have a sample app that opens a Notepad on click of a command button. We have a sample form with a button (see below).



Now add the process control from the Components Control (see below)





This control has no design features and will be used in run-time and it straightway gets docked in the tray (see below)




Change the following properties of the control.



This can also be changed during run time.



Now in the Button Click event write the following code:

Private Sub ButtonNotepad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles loadNotepadButton.Click
' http://dotnetdud.blogspot.com/
ProcessNotepad.EnableRaisingEvents = True
ProcessNotepad.Start()

End Sub


The EnableRaisingEvents property indicates whether the component should be notified when the operating system has shut down a process. The EnableRaisingEvents property is used in asynchronous processing to notify your application that a process has exited. To force your application to synchronously wait for an exit event (which interrupts processing of the application until the exit event has occurred), use the WaitForExit method.

This will start the Notepad Application. If you want receive the closure of application use the following event

Private Sub ProcessNotepad_Exited(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProcessNotepad.Exited
' Coded for http://dotnetdud.blogspot.com/
MessageBox.Show("Notepad has been closed ", "Dot Net Tips & Tricks", MessageBoxButtons.OK)
End Sub

If you want to close the application use the kill method.

Private Sub ButtonClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles closeNotepadButton.Click
If ProcessNotepad.HasExited = False Then
ProcessNotepad.Kill()
End If
End Sub


We have used Notepad, as it is simple and easy. Try it with other stuff!!

See Also:

Form's Keydown Event not Firing in VB.NET

How to enable Keydown, Keypress events for Windows forms (.NET)

If the Keydown, Keypress events etc are not fired in WinForms application, check if the KeyPreview is set