Showing posts with label WPF. Show all posts
Showing posts with label WPF. Show all posts

Thursday, May 15, 2014

Simple Communication between two WPF Application in same PC

I have created a simple communication between two WPF windows application, by which you can send any number of data as string. Right now i am closing TCP Connection just after successful result. You can do this multiple time.

So Here is the code

Client Side Code: (Sender)
        //Publish this location to other opend application
     void sendData(string data)
     {
            try
            {
                TcpClient tcpclnt = new TcpClient();
                tcpclnt.Connect("127.0.0.1", 8001);
                // use the ipaddress as in the server program

                String str = data;
                Stream stm = tcpclnt.GetStream();

                ASCIIEncoding asen = new ASCIIEncoding();
                byte[] ba = asen.GetBytes(str);
                
                stm.Write(ba, 0, ba.Length);
                tcpclnt.Close();
            }

            catch (Exception em)
            {
                Console.WriteLine("Error..... " + em.StackTrace);
            }
     }


Server Side Code: (Receiver)
       //To Listen the Connection type code for an event, As i write here for a button
     private void Button_Click(object sender, RoutedEventArgs e)
     {
            try
            {
                IPAddress ipAd = IPAddress.Parse("127.0.0.1");
                TcpListener myList = new TcpListener(ipAd, 8001);
                myList.Start();

                Socket s = myList.AcceptSocket();
                Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);

                byte[] b = new byte[100];
                int k = s.Receive(b);
                Console.WriteLine("Recieved...");
                for (int i = 0; i < k; i++)
                    Console.Write(Convert.ToChar(b[i]));

                ASCIIEncoding asen = new ASCIIEncoding();
                s.Send(asen.GetBytes("The string was recieved by the server."));
                Console.WriteLine("\nSent Acknowledgement");
                /* clean up */
                s.Close();
                myList.Stop();

            }
            catch (Exception em)
            {
                Console.WriteLine("Error..... " + em.StackTrace);
            }

     }

Reference Site : http://www.codeproject.com/Articles/1415/Introduction-to-TCP-client-server-in-C

Tuesday, May 13, 2014

Sort ListView Item in WPF

Its Simple and easy to sort ListView Item as below

GameFilesListView.Items.SortDescriptions.Add(new SortDescription("headerName", ListSortDirection.Descending));

headerName is the ListView Header Name

Friday, May 2, 2014

Inbuilt Dialogue for Win Application+WPF C#


To Show above progress dialogue in Windows WPF Application, you need to install MahApps.Metro theme then include

   using MahApps.Metro.Controls;
   using MahApps.Metro.Controls.Dialogs;

Also declare a global variable

                  private ProgressDialogController progress;

After this just call this below method to show or hide the progress dialogue

                  public async Task enableOrDisableWaitPopup(bool enable, string message="")
        {
            if(enable)
                progress = await this.ShowProgressAsync("Please wait...", message);
            else
                await progress.CloseAsync();
        }



Tuesday, April 15, 2014

Syntax Formatting for any Language in WPF C# Textbox

Its really Simple and Easy to format any languge
  1. First You need to install AvalonEdit from Manage NuGet Packages. Use following link to install instructions http://www.nuget.org/packages/AvalonEdit
or Just right click on Project in Solution Explorer and Click Manage NuGet Packages, now click Online and Search Avalon Editor. Then Install AvalonEditor
  1. Just add following code in xaml file.
    xmlns:avalonedit="http://icsharpcode.net/sharpdevelop/avalonedit"
  2. Now add your TextEditor as follow
    <avalonedit:TextEditor SyntaxHighlighting="XML" x:Name="gameListXMLText" Height="200">
  3. Just Change SyntaxHighlighting as you want it could be C#
  4. Now Just load xml file as follow
gameListXMLText.Text = File.ReadAllText("sample.xml");