Drag And Drop in WPF


“This demo will demonstrate how to drag and drop a txt file in the WPF textbox and it show the content of that files”.

 Download: http://www.4shared.com/file/GWB4a3bQ/drg.html

1.     “select the textbox and change it “Allow Drop” property true.”

2.     “write the following code in the textbox “Preview Drag Enter” event. ”

Code:

private void textBox1_PreviewDragEnter_1(object sender, DragEventArgs e)
        {
            bool isCrt = true;</pre>
            if (e.Data.GetDataPresent(DataFormats.FileDrop, true) == true)
            {

                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop, true);
                foreach (string file in files)
                {
                    if (File.Exists(file) == false)
                    {
                        isCrt = false;
                        break;
                    }
                    FileInfo info = new FileInfo(file);
                    if (info.Extension != ".txt")
                    {
                        isCrt = false;
                        break;
                    }

                }
            }
            if (isCrt == true)
            {
                e.Effects = DragDropEffects.All;

            }
            else
                e.Effects = DragDropEffects.None;
            e.Handled = true;
        }

3. “write the following code in the textbox preview drop event function.”

Code:

private void textBox1_PreviewDrop(object sender, DragEventArgs e)
        {

            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop, true);
            foreach (string file in files)
                textBox1.Text += File.ReadAllText(file);
            e.Handled = true;
          }

Timer in WPF


  In this demo, Demonstrate you how to use timer in WPF(Window Presentation Foundation). For using the timer first we need to use this namespace “using System.Windows.Threading;” .Create one “DispatcherTimer()” object called “Timer”.

Download:   “http://www.4shared.com/file/-1Z0xy1_/timer.html ”

       

Write the following code :

Code :

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;
using System.Windows.Threading;

namespace timer

{

    /// <summary>

    /// Interaction logic for MainWindow.xaml

    /// </summary>

    ///

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

            Timer = new DispatcherTimer();

        }

        private DispatcherTimer Timer;

       // public event Action DoSomething;

        private int _timesCalled = 0;

        public void timer_Start(int PeriodInSeconds)

        {

            Timer.Interval = TimeSpan.FromSeconds(PeriodInSeconds);

            Timer.Tick += timer_Task;

            _timesCalled = 0;

            Timer.Start();

        }

        public void timer_Stop()

        {

            Timer.Stop();

        }

        int i;

        private void timer_Task(object sender, EventArgs e)

        {

            listBox1.Items.Add(Convert.ToString(DateTime.Now));

        }

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            if (textBox1.Text.Length > 0)

            {

                listBox1.Items.Clear();

                timer_Start(Convert.ToInt32(textBox1.Text));

            }

            else

                MessageBox.Show("set time");

        }

        private void button2_Click(object sender, RoutedEventArgs e)

        {

            Timer.Stop();

        }

    }

}

Windows Presentation Foundation (WPF)


WPF

WPF (window presentation foundation) is the high level of user interface; provide the 2D, and 3D graphics environment. It’s fully loaded with multimedia feature, in single framework. There is no limit of your design in short, what you want to design in WPF it allows you. Developer can customize the buttons or create its own style button which is suitable to its application with high resolution. Create your own component and use in WPF.

For WPF FEATURE chick here: http://blogs.msdn.com/b/xtof/archive/2007/08/17/top-10-wpf-features.aspx