বুধবার, ৩০ নভেম্বর, ২০১১

Get MAC Address using C#

Add the reference System.Management and Import System.Management namespace.
Get the Mac address of all network cards in the machine 

protected void GetMACAddress()
{
    ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
    ManagementObjectCollection moc = mc.GetInstances();
    string MACAddress = "";
    foreach (ManagementObject mo in moc)
    {                
        if (mo["MacAddress"] != null)
        {                    
            MACAddress = mo["MacAddress"].ToString();
            MessageBox.Show(MACAddress);
        }                                
    } 
}
 
Get the Mac address of network card is being used in the machine
 
protected void GetMACAddress()
{ ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); string MACAddress = ""; foreach (ManagementObject mo in moc) { if (mo["MacAddress"] != null) { if ((bool)mo["IPEnabled"] == true) { MACAddress = mo["MacAddress"].ToString(); MessageBox.Show(MACAddress); } } } }

  
 Output:


Barcode Print in Crystal report WPF

Code:

  private void printBarcodebutton_Click(object sender, RoutedEventArgs e)
       {
           BarCodeCrpt pbarcode = new BarCodeCrpt();
           pbarcode.SetDataSource(SetbarcodeData());
           ReportViewer Viewer = new ReportViewer();
           Viewer.setReportSource(pbarcode);
           // frmViewer.MdiParent = mdiParent;
           Viewer.ShowDialog();
       }

  private List<Barcode> SetbarcodeData()
       {
           List<Barcode> code = new List<Barcode>();
           Barcode acode = new Barcode();
           Product secProduct = productobj;
           acode.ProductName = secProduct.ProductName;
           acode.ProductPrice = secProduct.SalesPrice;
           FileStream fs1 = new FileStream("hhhh.bmp", FileMode.Open, FileAccess.Read);
           fs1.Flush();
           BinaryReader br = new BinaryReader(fs1);
           byte[] imgbyte = new byte[fs1.Length + 1];
           imgbyte = br.ReadBytes(Convert.ToInt32((fs1.Length)));
           acode.BarcodeSt = imgbyte;
           for (int i = 0; i <9; i++)
           {
               code.Add(acode);
           }
           fs1.Close();
           return code;
       }

Create "hhhh.bmp" Code:
( Add " Neodynamic.WPF.Barcode.dll" in refarence)

private void UpdateBarcodeOnHeader()
        {
            try
            {
                if (productobj.Barcode.Length != 12)
                {
                    Microsoft.Windows.Controls.MessageBox.Show("Please enter a value to encode, thanks!");
                    return;
                }

                barcodeimage.SetValue(System.Windows.Controls.Image.SourceProperty, null);
                //Create an instance of Barcode Professional
                BarcodeProfessional bcp = new BarcodeProfessional();
                 //Barcode settings
                bcp.Symbology = Symbology.Code128;

                bcp.BarcodeUnit = BarcodeUnit.Inch;
                bcp.BarWidth = 1f / 96f;
                bcp.BarHeight = 0.5f;

                //Set the value to encode
                bcp.Code = productobj.Barcode;
                imgGet = bcp.GetBarcodeImageBinary();

                if (imgGet.Length > 0)
                {
                    FileStream fs1 = new FileStream("hhhh.bmp", FileMode.Create, FileAccess.Write);
                    fs1.Write(imgGet, 0, imgGet.Length);
                    fs1.Flush();
                    fs1.Close();
                    Bitmap bmp = new Bitmap("hhhh.bmp");
                    BitmapImage bi = new BitmapImage();
                    MemoryStream ms = new MemoryStream();
                    bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                    bi.BeginInit();
                    ms.Seek(0, SeekOrigin.Begin);
                    bi.CacheOption = BitmapCacheOption.OnLoad;
                    bi.StreamSource = ms;
                    bi.EndInit();
                    bi.StreamSource.Dispose();
                    bmp.Dispose();
                    bmp = null;
                    ms.Dispose();
                    ms = null;
                    this.barcodeimage.Source = bi;
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
            }           
        }    

Autosuggest Textbox for WPF

In this post i will share to everyone how to make an auto suggest TextBox in wpf like GOOGLE Textbox.
For this i create a grid whose first row contains a textbox and the second row contains a Listbox. Now, when the textbox gets the focus and its text changes due to the user’s input then fill up the listbox.
Here is the XAML code

1<Grid>
2        <TextBox Height="23" HorizontalAlignment="Left" Margin="111,46,0,0" Name="txtAutoSuggestName" VerticalAlignment="Top" Width="161" PreviewKeyDown="txtAutoSuggestName_PreviewKeyDown" TextChanged="txtAutoSuggestName_TextChanged" />
3        <ListBox Height="Auto" HorizontalAlignment="Left" Margin="111,69,0,0" Name="listBoxSuggestion" VerticalAlignment="Top" Width="161" Visibility="Hidden" PreviewKeyDown="listBoxSuggestion_PreviewKeyDown" KeyDown="listBoxSuggestion_KeyDown" />
4    </Grid>

Now this is CS code for glorious effect in textbox
01private void txtAutoSuggestName_TextChanged(object sender, TextChangedEventArgs e)
02       {
03           listBoxSuggestion.Items.Clear();
04           if (txtAutoSuggestName.Text != "")
05           {
06               List<Customer> namelist = CustomerGatewayObj.listShow(txtAutoSuggestName.Text);
07               if (namelist.Count > 0)
08               {
09                   listBoxSuggestion.Visibility = Visibility.Visible;
10                   foreach (var obj in namelist)
11                   {
12                       listBoxSuggestion.Items.Add(obj.id);
13                   }
14               }
15           }
16           else
17           {
18               listBoxSuggestion.Visibility = Visibility.Hidden;
19           }
20       }
21 
22       private void txtAutoSuggestName_PreviewKeyDown(object sender, KeyEventArgs e)
23       {
24           if (e.Key == Key.Down)
25           {
26               listBoxSuggestion.Focus();
27           }
28       }
29 
30 
31       private void listBoxSuggestion_PreviewKeyDown(object sender, KeyEventArgs e)
32       {
33           if (listBoxSuggestion.SelectedIndex == 0 && e.Key == Key.Up)
34           {
35               txtAutoSuggestName.Focus();
36           }
37       }
38 
39       private void listBoxSuggestion_KeyDown(object sender, KeyEventArgs e)
40       {
41           if (listBoxSuggestion.SelectedIndex > -1)
42           {
43               if (e.Key == Key.Enter)
44               {
45                   txtAutoSuggestName.Text = listBoxSuggestion.SelectedItem.ToString();
46               }
47           }
48       }

Multi Column ComboBox in C# WPF

For my working purpose I wanna try to make a multiple column Combox Box.So i prepare myself to make this.But i fetched too many problem for this and Finally i have gotten this solution.
At first write this code in XAML file to create MultiColumn ComboBox.


<ComboBox Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="1,0,0,232" Name="cmbWHTypeId" Width="175" TabIndex="0" VerticalAlignment="Bottom">

    <ComboBox.ItemTemplate>

        <DataTemplate>

            <StackPanel Orientation="Horizontal">

                <TextBlock Text="{Binding WhtID}" Width="60"/>

                <TextBlock Text="|" Width="10"/>

                <TextBlock Text="{Binding WhtName}" Width="100" />

            </StackPanel>

        </DataTemplate>

    </ComboBox.ItemTemplate>

</ComboBox>

Then Write the following Code in CS file of  UI


cmbWHTypeId.ItemsSource = objBWhTypes.GetAllWhTypes();

Here GetAllWhTypes() is a method which returns a list collection.

public List<EWareHouseTypes>GetAllWhTypes()

{

List<EWareHouseTypes> liWhTypes = new List<EWareHouseTypes>();

 

try

{

IEBSDataContext objDataContext = new IEBSDataContext();

 

var b = from WAREHOUSE_TYPES in objDataContext.SP_WarehouseType_GetAll() select WAREHOUSE_TYPES;

foreach (var item in b)

{

EWareHouseTypes objEWhTypes = new EWareHouseTypes();

objEWhTypes.WhtID = item.WT_ID;

objEWhTypes.WhtName = item.WT_NAME;

liWhTypes.Add(objEWhTypes);

}

}

catch (Exception e)

{

 

throw e;

}

return liWhTypes;

}

EWareHouseTypes is a class and it contains two property id and name.

GET VALUE FROM COMBO BOX


EWareHouseTypes aWHType = (EWareHouseTypes)cmbWHTypeId.SelectedItem;

MessageBox.Show(aWHType.WhtID);
Manually SET INDEX for a Particular ID


string typeID="WHT01"

 

for (int i = 0; i < cmbWHTypeId.Items.Count; i++)

{

EWareHouseTypes aWHType = (EWareHouseTypes)cmbWHTypeId.Items[i];

if (aWHType.WhtID == typeID)

{

cmbWHTypeId.SelectedIndex = i;

break;
}
}

If you have any query about this topic please leave a comment.

Run Multiprogram in one click by bat file

Code:
@echo off
rem IE
cd C:\Program Files\Internet Explorer
start iexplore.exe
timeout 5
rem Notepad
cd C:\Windows
start notepad.exe
exit
...........
and save it "file_name.bat"