30 March 2018

An improved version of WPF usercontrol drag and drop tutorial

This is based on Microsoft - Walkthrough: Enabling Drag and Drop on a User Control


















The improved circle usercontrol has a CircleID property

Circle.xaml.cs
 using System.Windows;  
 using System.Windows.Controls;  
 using System.Windows.Input;  
 using System.Windows.Media;  
 namespace WpfApp3  
 {  
   /// <summary>  
   /// Interaction logic for Circle.xaml  
   /// </summary>  
   public partial class Circle : UserControl  
   {  
     private Brush _previousFill = null;  
     public Circle()  
     {  
       InitializeComponent();  
     }  
     public Circle(Circle c)  
     {  
       InitializeComponent();  
       this.circleUI.Height = c.circleUI.Height;  
       this.circleUI.Width = c.circleUI.Height;  
       this.circleUI.Fill = c.circleUI.Fill;  
       this.CircleID = c.CircleID;  
     }  
     public int CircleID  
     {  
       get { return (int)GetValue(CircleIDProperty); }  
       set { SetValue(CircleIDProperty, value); }  
     }  
     public static readonly DependencyProperty CircleIDProperty =  
       DependencyProperty.Register("CircleID", typeof(int), typeof(Circle));  
     protected override void OnDragEnter(DragEventArgs e)  
     {  
       base.OnDragEnter(e);  
       _previousFill = circleUI.Fill;  
       if (e.Data.GetDataPresent(DataFormats.StringFormat))  
       {  
         string dataString = (string)e.Data.GetData(DataFormats.StringFormat);  
         BrushConverter converter = new BrushConverter();  
         if (converter.IsValid(dataString))  
         {  
           Brush newFill = (Brush)converter.ConvertFromString(dataString.ToString());  
           circleUI.Fill = newFill;  
         }  
       }  
     }  
     protected override void OnDragLeave(DragEventArgs e)  
     {  
       base.OnDragLeave(e);  
       circleUI.Fill = _previousFill;  
     }  
     protected override void OnDrop(DragEventArgs e)  
     {  
       base.OnDrop(e);  
       if (e.Data.GetDataPresent(DataFormats.StringFormat))  
       {  
         string dataString = (string)e.Data.GetData(DataFormats.StringFormat);  
         BrushConverter converter = new BrushConverter();  
         if (converter.IsValid(dataString))  
         {  
           Brush newFill = (Brush)converter.ConvertFromString(dataString);  
           circleUI.Fill = newFill;  
           if (e.KeyStates.HasFlag(DragDropKeyStates.ControlKey))  
           {  
             e.Effects = DragDropEffects.Copy;  
           }  
           else  
           {  
             e.Effects = DragDropEffects.Move;  
           }  
         }  
       }  
       e.Handled = true;  
     }  
     protected override void OnDragOver(DragEventArgs e)  
     {  
       base.OnDragOver(e);  
       e.Effects = DragDropEffects.None;  
       if (e.Data.GetDataPresent(DataFormats.StringFormat))  
       {  
         string dataString = (string)e.Data.GetData(DataFormats.StringFormat);  
         BrushConverter converter = new BrushConverter();  
         if (converter.IsValid(dataString))  
         {  
           if (e.KeyStates.HasFlag(DragDropKeyStates.ControlKey ))  
           {  
             e.Effects = DragDropEffects.Copy;  
           }  
           else  
           {  
             e.Effects = DragDropEffects.Move;  
           }  
         }  
       }  
       e.Handled = true;  
     }  
     protected override void OnMouseMove(MouseEventArgs e)  
     {  
       base.OnMouseMove(e);  
       if (e.LeftButton == MouseButtonState.Pressed)  
       {  
         DataObject data = new DataObject();  
         data.SetData(DataFormats.StringFormat, circleUI.Fill.ToString());  
         data.SetData("Double", circleUI.Height);  
         data.SetData("Object", this);  
         data.SetData("CircleID", CircleID);  
         DragDrop.DoDragDrop(this, data, DragDropEffects.Copy | DragDropEffects.Move);  
       }  
     }  
     protected override void OnGiveFeedback(GiveFeedbackEventArgs e)  
     {  
       base.OnGiveFeedback(e);  
       // These Effects values are set in the drop target's  
       // DragOver event handler.  
       if (e.Effects.HasFlag(DragDropEffects.Copy))  
       {  
         Mouse.SetCursor(Cursors.Cross);  
       }  
       else if (e.Effects.HasFlag(DragDropEffects.Move))  
       {  
         Mouse.SetCursor(Cursors.Pen);  
       }  
       else  
       {  
         Mouse.SetCursor(Cursors.No);  
       }  
       e.Handled = true;  
     }  
   }  
 }  

Circle.xaml
 <UserControl x:Class="WpfApp3.Circle" x:Name="CircleControl"  
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"   
        xmlns:local="clr-namespace:WpfApp3"  
        mc:Ignorable="d"   
        d:DesignHeight="300" d:DesignWidth="300"  
        AllowDrop="True">  
   <Grid>  
     <Grid.RowDefinitions>  
       <RowDefinition />  
       <RowDefinition />  
     </Grid.RowDefinitions>  
     <Label Content="{Binding Path=CircleID, ElementName=CircleControl}" Grid.Row="0" />  
     <Ellipse x:Name="circleUI" Grid.Row="1"  
      Height="100" Width="100"  
      Fill="Blue" />  
   </Grid>  
 </UserControl>  
This version of MainWindow identify the circle usercontrol by CircleID and parent panel by its Name
MainWindow.xaml.cs
 using System.Windows;  
 using System.Windows.Controls;  
 using System.Windows.Media;  
 namespace WpfApp3  
 {  
   /// <summary>  
   /// Interaction logic for MainWindow.xaml  
   /// </summary>  
   public partial class MainWindow : Window  
   {  
     public MainWindow()  
     {  
       InitializeComponent();  
     }  
     private void panel_DragOver(object sender, DragEventArgs e)  
     {  
       if (e.Data.GetDataPresent("Object"))  
       {  
         if (e.KeyStates == DragDropKeyStates.ControlKey )  
         {  
           e.Effects = DragDropEffects.Copy;  
         }  
         else  
         {  
           e.Effects = DragDropEffects.Move;  
         }  
       }  
     }  
     private void panel_Drop(object sender, DragEventArgs e)  
     {  
       string panelName;  
       if (e.Handled == false)  
       {  
         Panel _panel = (Panel)sender;  
         UIElement _element = (UIElement)e.Data.GetData("Object");  
         if (_panel!= null && _element!=null)  
         {  
           panelName = _panel.Name;  
           Panel _parent = (Panel)VisualTreeHelper.GetParent(_element);  
           if (_parent!=null)  
           {  
             if (e.KeyStates ==DragDropKeyStates.ControlKey &&  
               e.AllowedEffects.HasFlag(DragDropEffects.Copy))  
             {  
               Circle _circle = new Circle((Circle)_element);  
               int cid = _circle.CircleID;  
               string s = cid.ToString() + " is in " + panelName;  
               MessageBox.Show(s);  
               _panel.Children.Add(_circle);  
               e.Effects = DragDropEffects.Copy;  
             }  
             else if (e.AllowedEffects.HasFlag(DragDropEffects.Move))  
             {  
               Circle _circle = new Circle((Circle)_element);  
               int cid = _circle.CircleID;  
               string s = cid.ToString() + " is in " + panelName;  
               MessageBox.Show(s);  
               _parent.Children.Remove(_element);  
               _panel.Children.Add(_element);  
               e.Effects = DragDropEffects.Move;  
             }  
           }  
         }  
       }  
     }  
   }  
 }  

MainWindow.xaml
 <Window x:Class="WpfApp3.MainWindow"  
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
     xmlns:local="clr-namespace:WpfApp3"  
     mc:Ignorable="d"  
     Title="MainWindow" Height="350" Width="525">  
   <Grid>  
     <Grid.ColumnDefinitions>  
       <ColumnDefinition />  
       <ColumnDefinition />  
     </Grid.ColumnDefinitions>  
     <StackPanel Grid.Column="0"  
           x:Name="LeftPanel"  
       Background="Beige"  
           AllowDrop="True"  
       DragOver="panel_DragOver"  
       Drop="panel_Drop"  
           >  
       <TextBox Width="Auto" Margin="2"  
        Text="green"/>  
       <local:Circle Margin="2" CircleID="1" />  
       <local:Circle Margin="2" CircleID="2"/>  
     </StackPanel>  
     <StackPanel Grid.Column="1"  
            x:Name="RightPanel"  
       Background="Bisque"  
       AllowDrop="True"  
       DragOver="panel_DragOver"  
       Drop="panel_Drop"     
           >  
     </StackPanel>  
   </Grid>  
 </Window>  

26 January 2018

How to find kickass torrent proxy servers?


How to find kickass torrent proxy servers?  As you can see Google is blocking relevant searches.

Easy, use baidu

www.baidu.com

15 January 2018

Old laptop stopped working after Windows 10 upgrade?

If you are like me has a trusty old laptop you bought back in Windows 7 days and it is still working really well, well except it wouldn't install Windows 10 and Microsoft keeps on trying to force you to update, and the latest patch just render it unbootable. There is hope :)

Install Ubuntu and run Windows 10 under vmware.

Ubuntu has come a long way and it is really refine. It is free and will work with older hardwares while supporting modern features (harddrive encryption) with lots of free software

26 November 2017

Example on how to use C++ regex_token_iterator

 #pragma once   
 #pragma warning(disable:4996)   
 #define _SCL_SECURE_NO_WARNINGS   
 #include "stdafx.h"  
 #include <fstream>  
 #include <iostream>  
 #include <regex>  
 #include <iterator>  
 #include <algorithm>  
 using namespace std;  
 int main() {  
      string seq("tatagcagtcccgctgtgtgtacgacactggcaacatgaggtctttgctaatcttggtagctttg");  
      regex e("(tata)([gatc]*)(tag)");  
      int submatches[] = { 1, 2, 3 };  
      sregex_token_iterator rend;  
      sregex_token_iterator a(seq.begin(), seq.end(), e, submatches);  
      while (a != rend) std::cout << " [" << *a++ << "]";  
      getchar();  
      return 0;  
 }  

22 November 2017

Are you being tracked by Facebook?

One way to find out whether the websites you are visiting has facebook tracker is to install Facebook pixel extension on your Google Chrome browser

https://chrome.google.com/webstore/detail/facebook-pixel-helper/fdgfkebogiimcoedlicjlajpkdmockpc?hl=en

You can also see it by examining your network traffic using the developer tool on your browser


02 November 2017

String.Compare is a better method to compare strings than String.Equal


String.Compare is a better method to compare strings than String.Equal

Below example illustrates how String.Compare handles nulls better than String.Equal


    class Program
    {
        static void Main(string[] args)
        {
            string A = "Hello World";
            string B = "Hello World";
            string C = null;

            if (String.Compare(B, A) == 0) { Console.WriteLine("A equals B"); }
            if (String.Compare(C, A) == 0) { Console.WriteLine("A equals C"); }
            if (B.Equals(A)) { Console.WriteLine("A equals B"); }
            try
            {
                if (C.Equals(A)) { Console.WriteLine("A equals C"); }
            }
            catch (Exception e)
            {
                Console.Write(e.Message);
            }

            Console.ReadKey();
        }
    }

22 October 2017

Anonymous function

 class Program  
   {  
     static void Main(string[] args)  
     {  
       Func<int, int> func1 = x => x + 1;  
       Func<int, int> func2 = x => { return x + 1; };  
       Func<int, int> func3 = (int x) => x + 1;  
       Func<int, int> func4 = (int x) => { return x + 1; };  
       Func<int, int, int> func5 = (x, y) => x * y;  
       Action func6 = () => Console.WriteLine();  
       Func<int, int> func7 = delegate (int x) { return x + 1; };  
       Func<int> func8 = delegate { return 1 + 1; };  
       Console.WriteLine(func1.Invoke(1));  
       Console.WriteLine(func2.Invoke(1));  
       Console.WriteLine(func3.Invoke(1));  
       Console.WriteLine(func4.Invoke(1));  
       Console.WriteLine(func5.Invoke(2, 2));  
       func6.Invoke();  
       Console.WriteLine(func7.Invoke(1));  
       Console.WriteLine(func8.Invoke());  
       Console.ReadLine();  
     }