Tag Archives: codebox

C#.codebox 9: WPF – How to restart my application?

0
Filed under C\C++\C#, How to?, Programming, Source code
Tagged as , , , , ,

C#.codebox 9

WPF – How to restart my application?

Unfortunately, to date, WPF does not have a Restart() function line in WinForms so one way to do this is by using…WinForms.

1
      System.Windows.Forms.Application.Restart();

However, after running this code our application will not exit on it’s own, even though a new instance of the application is being created. Therefore, we must close it manually…for example by doing:

1
      Application.Current.Shutdown();

If you don’t want to use WinForms with your application you can do something like:

1
2
3
4
5
     //things to do before closing
     // ...
     //Restart
      System.Diagnostics.Process.Start(Application.ResourceAssembly.Location);
      Application.Current.Shutdown();

Where we create a new instance of our application, by using Process.Start, and immediately close the current one.

C#.codebox 8: WPF – How to compare two List of string?

0
Filed under C\C++\C#, How to?, Programming, Source code
Tagged as , , , , , , ,

C#.codebox 8

WPF – How to compare two List of string?

1
      bool Result = myList1.SequenceEqual(myList2);

C#.codebox 7: WPF – How to detect a mouse double click on an Image?

0
Filed under C\C++\C#, How to?, Programming, Source code
Tagged as , , , , , ,

C#.codebox 7

WPF – How to detect a mouse double click on an Image?

1
2
3
4
5
6
7
8
9
10
        //Left button down
        void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            myImage = (Image)sender;
 
            if (e.ClickCount == 2) //if double clicked
            {
                //my code
            }
        }

C#.codebox 6: WPF – How to get the location of a control?

0
Filed under C\C++\C#, How to?, Programming, Source code
Tagged as , , , , ,

C#.codebox 6

WPF – How to get the location of a control?

1
2
3
4
5
6
7
        private Point GetControlLocation(UIElement myControl)
        {
            Vector vector = VisualTreeHelper.GetOffset(myControl);
            Point currentPoint = new Point(vector.X, vector.Y);
 
            return currentPoint;
        }

C#.codebox 5: WPF – How to drag an Image with the mouse?

2
Filed under C\C++\C#, How to?, Programming, Source code
Tagged as , , , , , ,

C#.codebox 5

WPF – How to drag an Image with the mouse?

The code below was used in ShortcutMan.G 3.0 to drag an image around the window (a TabItem in this case) with the mouse.
The image is contained within a Canvas which is contained within a TabItem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
        Image myImage = null; //selected image
        Boolean MouseIsDown = false; //mouse down state
 
        //Left button down - when the mouse button is down in the image
        void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            myImage = (Image)sender;
 
            if (e.ClickCount == 1) //if one click
            {
                //Drag image
                TabItem Tab = (TabItem)TabControl1.SelectedItem;
                Canvas TabCanvas = (Canvas)Tab.Content;
 
                ////Get mouse coordinates and center the cursor in the image (optional)
                Point mouseCoordinates = new Point(e.GetPosition(TabCanvas).X, e.GetPosition(TabCanvas).Y);
                Canvas.SetLeft(myImage, mouseCoordinates.X - (myImage.Width / 2));
                Canvas.SetTop(myImage, mouseCoordinates.Y - (myImage.Height / 2));
 
                //Update mouse state
                MouseIsDown = true;
            }
        }
 
        //Left button up - when the mouse button goes up
        void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //Update mouse state
            MouseIsDown = false;
        }
 
        //Mouse leave - when the mouse button leaves the image
        void Image_MouseLeave(object sender, MouseEventArgs e)
        {
            //Update mouse state
            MouseIsDown = false;
        }
 
        //Mouse move - when the mouse moves
        void Image_MouseMove(object sender, MouseEventArgs e)
        {
            if (myImage != null && MouseIsDown) //if there's an image selected and if mouse is down
            {
                TabItem Tab = (TabItem)TabControl1.SelectedItem;
                Canvas TabCanvas = (Canvas)Tab.Content;
 
                //Get mouse coordinates
                Point mouseCoordinates = new Point(e.GetPosition(TabCanvas).X, e.GetPosition(TabCanvas).Y);
 
                //Set limits - the limits within the tab (optional)
                if (mouseCoordinates.X < 0)
                    mouseCoordinates.X = 0;
                if (mouseCoordinates.Y < 0)
                    mouseCoordinates.Y = 0;
                if (mouseCoordinates.X > TabCanvas.Width)
                    mouseCoordinates.X = TabCanvas.Width;
                if (mouseCoordinates.Y > TabCanvas.Height)
                    mouseCoordinates.Y = TabCanvas.Height;
 
                //Update image location
                Canvas.SetLeft(myImage, mouseCoordinates.X - (myImage.Width / 2));
                Canvas.SetTop(myImage, mouseCoordinates.Y - (myImage.Height / 2));
            }
        }

C#.codebox 4: WPF – How to get the owner of a context menu?

0
Filed under C\C++\C#, How to?, Programming, Source code
Tagged as , , , , , ,

C#.codebox 4

WPF – How to get the owner of a context menu?

1
        contextmenu1.PlacementTarget;

C#.codebox 3: WPF – How to set the tab order in XAML Designer in VS2008?

0
Filed under C\C++\C#, How to?, Programming, Source code
Tagged as , , , , , ,

C#.codebox 3

WPF – How to set the tab order in XAML Designer in VS2008?

Unfortunately, currently there is no simple way to do this (like in Windows Forms, with View->Tab Order).
An easy way to do it, is to use the properties window search functionality to filter only the TabIndex property and then apply appropriate values for each control. However, when your window is loaded you have to press the TAB key at least once, to set the focus on the first control in the tab order.
To do this automatically though, and to select/set focus on a control at runtime, you can do something like:

1
2
3
4
5
        //Load
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            TextBox1.Focus(); //set focus on a textbox
        }

C#.codebox 2: WPF – How to extract an icon from a file?

0
Filed under C\C++\C#, How to?, Programming, Source code
Tagged as , , , , , ,

C#.codebox 2

WPF – How to extract an icon from a file?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
        using System.Windows.Media;
        using IconImage = System.Drawing.Icon;
        using System.Windows.Media.Imaging;
        using System.Windows.Controls;
 
        //Extracts the icon from a file and returns it as ImageSource.
        public ImageSource ExtractIconFromFile(string Location)
        {
            Image myImage = new Image();
 
            //Get icon
            IconImage myIconImage;
            myIconImage = IconImage.ExtractAssociatedIcon(Location);
            MemoryStream strm = new MemoryStream();
            myIconImage.Save(strm);
            IconBitmapDecoder BMPDec = new IconBitmapDecoder(strm, BitmapCreateOptions.None, BitmapCacheOption.Default);
            myImage.Source = BMPDec.Frames[0];
            strm.Close();
 
            return myImage.Source;
        }

C#.codebox 1: WPF – How to get the parent window of a control?

0
Filed under C\C++\C#, How to?, Programming, Source code
Tagged as , , , , ,

C#.codebox 1

WPF – How to get the parent window of a control?

1
   Window thisControl_Window = Window.GetWindow(this);