Monday, July 30, 2012

How to use FileSystemWatcher class ?

Here is the sample code ,,just try it,,,

working properly


code:


namespace FileMonitor  
{ 
    public interface EventsList 
    { 
        void OnRenamed(object source, RenamedEventArgs e); 
        void OnChanged(object source, FileSystemEventArgs e); 
    } 
      
    public  class MonitorEngine  
    { 
        private String path; 
        private EventsList events = null; 
     
        public MonitorEngine(String path,EventsList ev) 
        { 
            this.events=ev; 
            this.path=path; 
        } 
        public void WatcherFiles() 
        { 
            FileSystemWatcher watcher = new FileSystemWatcher(); 
            try 
            { 
                watcher.Path = path; 
                watcher.IncludeSubdirectories=true; 
            } 
            catch(ArgumentException ex)  
            { 
                MessageBox.Show(ex.Message); 
                return; 
            } 
             
            // setup what we are looking for 
            watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite| 
                                   NotifyFilters.FileName  | NotifyFilters.DirectoryName; 
             
            watcher.Filter = "*.*"; 
             
            // Add event handlers. 
            watcher.Changed += new FileSystemEventHandler(events.OnChanged); 
            watcher.Created += new FileSystemEventHandler(events.OnChanged); 
            watcher.Deleted += new FileSystemEventHandler(events.OnChanged); 
            watcher.Renamed += new RenamedEventHandler(events.OnRenamed); 
         
            // Begin watching the directory. 
            watcher.EnableRaisingEvents = true;  
             
        } 
    } 
} 

Friday, July 27, 2012

How to show a message box in windows application?



just wirte down

System.windows.forms.messagebox.show("string");

Tuesday, July 24, 2012

How to make a textbox multiline in WPF?

the only think u have to do is add the property as follows


   1. TextWrapping="Wrap"
   2. VerticalScrollBarVisibility="Visible"
   3. AcceptsReturn="True"

Monday, July 23, 2012

How to make serial communication in .NET?


The first think, just kept in your mind dnt forget to include the namespace.
using System.IO.Ports;

using System.Data;
using System.Threading;


then next step ,go to your mainwindow.xaml.cs page

add the corresponding code to the related controls.

Sample code:


public partial class MainWindow : Window
    {
        #region variables
        //Richtextbox
        FlowDocument mcFlowDoc = new FlowDocument();
        Paragraph para = new Paragraph();
        //Serial
        SerialPort serial = new SerialPort();
        string recieved_data;
        #endregion


        public MainWindow()
        {
            InitializeComponent();
            InitializeComponent();
            Connect_btn.Content = "Connect";
        }

        private void Connect_Comms(object sender, RoutedEventArgs e)
        {
            if (Connect_btn.Content == "Connect")
            {
                serial.PortName = Comm_Port_Names.Text;
                serial.BaudRate = Convert.ToInt32(Baud_Rates.Text);
                serial.Handshake = System.IO.Ports.Handshake.None;
                serial.Parity = Parity.None;
                serial.DataBits = 8;
                serial.StopBits = StopBits.Two;
                serial.ReadTimeout = 200;
                serial.WriteTimeout = 50;
                serial.Open();

                Connect_btn.Content = "Disconnect";
                serial.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Recieve);

            }
            else
            {
                try // just in case serial port is not open could also be acheved using if(serial.IsOpen)
                {
                    serial.Close();
                    Connect_btn.Content = "Connect";
                }
                catch
                {
                }
            }
        }

        #region Recieving

        private delegate void UpdateUiTextDelegate(string text);
        private void Recieve(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            // Collecting the characters received to our 'buffer' (string).
            recieved_data = serial.ReadExisting();
            Dispatcher.Invoke(DispatcherPriority.Send, new UpdateUiTextDelegate(WriteData), recieved_data);
        }
        private void WriteData(string text)
        {
            // Assign the value of the recieved_data to the RichTextBox.
            para.Inlines.Add(text);
            mcFlowDoc.Blocks.Add(para);
            Commdata.Document = mcFlowDoc;
        }

        #endregion


        #region Sending       
       
        private void Send_Data(object sender, RoutedEventArgs e)
        {
            SerialCmdSend(SerialData.Text);
            SerialData.Text = "";
        }
        public void SerialCmdSend(string data)
        {
            if (serial.IsOpen)
            {
                try
                {
                    // Send the binary data out the port
                    byte[] hexstring = Encoding.ASCII.GetBytes(data);
                    //There is a intermitant problem that I came across
                    //If I write more than one byte in succesion without a
                    //delay the PIC i'm communicating with will Crash
                    //I expect this id due to PC timing issues ad they are
                    //not directley connected to the COM port the solution
                    //Is a ver small 1 millisecound delay between chracters
                    foreach (byte hexval in hexstring)
                    {
                        byte[] _hexval = new byte[] { hexval }; // need to convert byte to byte[] to write
                        serial.Write(_hexval, 0, 1);
                        Thread.Sleep(1);
                    }
                }
                catch (Exception ex)
                {
                    para.Inlines.Add("Failed to SEND" + data + "\n" + ex + "\n");
                    mcFlowDoc.Blocks.Add(para);
                    Commdata.Document = mcFlowDoc;
                }
            }
            else
            {
            }
        }

        #endregion


        #region Form Controls

        private void Close_Form(object sender, RoutedEventArgs e)
        {
            if (serial.IsOpen) serial.Close();
            this.Close();
        }
        private void Max_size(object sender, RoutedEventArgs e)
        {
            if (this.WindowState != WindowState.Maximized) this.WindowState = WindowState.Maximized;
            else this.WindowState = WindowState.Normal;
        }
        private void Min_size(object sender, RoutedEventArgs e)
        {
            if (this.WindowState != WindowState.Minimized) this.WindowState = WindowState.Minimized;
            else this.WindowState = WindowState.Normal;
        }
        private void Move_Window(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        #endregion
    }


Here you can see some objects of each controls, you simply just understand what the controls I put here. If you want to change the name of object means you can do it, it’s ur wish.

Just try it


its 100% working....

Converting text into an image format in c#...

just apply the code in ur application and see the changes and effects...

here is code snippet

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.IO;

namespace wer
{
public partial class WebForm1 : System.Web.UI.Page
{

Bitmap b = new Bitmap(1, 1);
Font f = new Font("Arial", 24);


protected void Page_Load(object sender, EventArgs e)
{

}

protected void btn_create_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "")
{ Response.Write("Please enter some text in the TextBox!!!"); }
else
{
Graphics graphics = Graphics.FromImage(b);
int width = (int)graphics.MeasureString(TextBox1.Text, f).Width;
int height = (int)graphics.MeasureString(TextBox1.Text, f).Height;
b = new Bitmap(b, new Size(width, height));
graphics = Graphics.FromImage(b);
graphics.Clear(Color.DarkBlue);
graphics.DrawString(TextBox1.Text, f, new SolidBrush(Color.White), 0, 0);
graphics.Flush();
b.Save("C:\\test.gif", System.Drawing.Imaging.ImageFormat.Gif);
Image1.Height = height;
Image1.Width = width;
  Image1.ImageUrl = "C:\\test.gif"; 
}
}
}
}
Happy coding ,.,.great day....

Sending email from your asp.net applications using c#??

now  iam giving some idea to send an email from c#

Put the controls in UI of ur .aspx page.

and write the following code in button click event,

SmtpClient smtpServer = new SmtpClient();
MailMessage mail = new MailMessage();
smtpServer.Credentials = new System.Net.NetworkCredential(TextBox1.Text + "@gmail.com", TextBox2.Text);
smtpServer.Port = 587;
smtpServer.Host = "smtp.gmail.com";
smtpServer.EnableSsl = true;
mail.From = new MailAddress(TextBox1.Text + "@gmail.com");

mail.To.Add(TextBox3.Text);
mail.Subject = TextBox4.Text;
mail.Body = TextBox5.Text;
smtpServer.Send(mail);

string message = "Mail successfully sent..";

ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + message + "');", true);

please dont forget to add the namespaces.

using System.Web.UI.WebControls;
using System.Net.Mail;


Happy coding

have a great day....cherrs

How to send Email using VB...?


Today iam explaining you about how to send emails from asp.net using vb

Its really very simple.
Simple simple.

Just create an asp.net web site and put control related to the objects used in the following code.

This is the code. just try it.Its working finely.

Imports System.Net.Mail
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim smtpServer As New SmtpClient()
        Dim mail As New MailMessage
  smtpServer.Credentials = New Net.NetworkCredential(TextBox1.Text &     "@gmail.com", TextBox2.Text)
        smtpServer.Port = 587
        smtpServer.Host = "smtp.gmail.com"
        smtpServer.EnableSsl = True
        mail.From = New MailAddress(TextBox1.Text & "@gmail.com")       
        mail.To.Add(TextBox3.Text)
        mail.Subject = TextBox4.Text
        mail.Body = TextBox5.Text()
        smtpServer.Send(mail)
        MsgBox("mail is sent", MsgBoxStyle.OkOnly, "Report")
    End Sub

        
End Class