Friday, May 17, 2013

How to passing the value from One form to another form in windows application c#

 Write code on Form -1 :--
=======================

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

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

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 objForm2 = new Form2(this);
            objForm2.Show();

        }

      public void PassValue(string strValue)
    {
        textBox1.Text = strValue;
    }


    }
}
------------------------------------------------------------------------------------------------------------


Write code on form : 2:-
=====================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        Form1 ownerForm = null;
        public Form2()
        {
            InitializeComponent();
        }
        public Form2(Form1 ownerForm)            //create parameterized  constructor for form 1
        {
            InitializeComponent();
            this.ownerForm = ownerForm;
        }
        private void Form2_Load(object sender, EventArgs e)
        {
        
        }

      

        private void button1_Click(object sender, EventArgs e)
        {
            this.ownerForm.PassValue(textBox1.Text);
            this.Close();
        }
    }
}


Sunday, April 21, 2013

How to get Network Adapter Mac Address in C#


 

First you need to add refererense from for get the mac address of network adapter.
Add Reference -->  .NET --> System.Management.

----------------------------*****************--------------------------
To get mac address of Local system.
using System.Managemant;
public static string GetMacAddress()
            {
                string Mac = string.Empty;
                ManagementClass MC = new ManagementClass("Win32_NetworkAdapter");
                ManagementObjectCollection MOCol = MC.GetInstances();
                foreach (ManagementObject MO in MOCol)
                    if (MO != null)
                    {
                        if (MO["MacAddress"] != null)
                        {
                            Mac = MO["MACAddress"].ToString();
                            if (Mac != string.Empty)
                                break;
                        }
                    }
                return Mac;
            }

-------------------------------------------------------------------------------

To get mac address of when your system in on LAN on etharnet .

Using System.Management;

  public PhysicalAddress GetMacAddress()           //Get MAC Address of the machine.
        {
            foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
            {
                // Only consider Ethernet network interfaces
                if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet &&
                    nic.OperationalStatus == OperationalStatus.Up)
                {
                    return nic.GetPhysicalAddress();
                }
            }
            return null;
        }