Posts

Showing posts with the label C#

Applying the 2-opt algorithm to travelling salesman problems in C# / WPF

Image
For the Java equivalent see this link: https://www.technical-recipes.com/2017/applying-the-2-opt-algorithm-to-traveling-salesman-problems-in-java/ For the C++ equivalent see this link: https://www.technical-recipes.com/2012/applying-c-implementations-of-2-opt-to-travelling-salesman-problems/ This post demonstrates how to apply the 2-opt algorithm to a number of standard test problems in C# while displaying the results in a WPF style window, while using the MVVM design pattern. See this link for an overview of the two opt algorithm. http://en.wikipedia.org/wiki/2-opt But essentially the 2-opt link swapping heuristic can be summarised by the following steps: The actual 2-opt heuristic can be summarised by the following pseudocode steps, repeating for all feasible combinations of I and k: [code language="text"] 1. take route[1] to route[i-1] and add them in order to new_route 2. take route[i] to route[k] and add them in reverse order to new_route 3. take r...

Using the HTML Agility Package to modify web pages

Image
Step 1: Create a new Visual Studio project Create a new Console application: Step 2: Use the Package Manager console to install the HTML Agility package. Select Tools > NuGet Package Manager > Package Manager Consol Enter: Install-Package HtmlAgilityPack Step 3: Add the reference to the HtmlAgilityPack.dll Right click you References folder, and browse to the HtmlAgilityPack.dll file that gets installed to the 'packages' folder in your Visual Studio project. Select and add this file: Step 4: Try it on some examples 1. Insert the 'DOCTYPE' comment before the main tag and insert a "

Using the WiX Toolset to create installers in Visual Studio C# projects

Image
Here is some of their online documentation on how to use the WiX Toolset to create installers: https://www.firegiant.com/wix/tutorial/getting-started Here is a quick and concise guide to set up using the WiX Toolset in a Visual Studio project, in order to create installation packages, from start to finish. Step 1: Install WiX Toolset First make sure you have installed the WiX toolset from the following site: http://wixtoolset.org/releases/ WiX Toolset when run looks like this: [caption id="attachment_7571" align="alignnone" width="355"] WiX Toolset to create installers[/caption] Step 2: Create a new Visual Studio project In this example I'm creating a new WPF application: Step 3: Add a WiX installer project to the solution Right click on your solution folder and select Add > New Project... Select 'Windows Installer XML' > 'Setup Project' and give your installer project a name: Step 4: generate GUID...

Embedding PowerPoint documents in WPF

Image
Some instruction on how to embed a PowerPoint presentation within a WPF application by converting the file to a XPS format and displaying this using DocumentViewer. Step 1: Create a WPF Application Step 2. Include the necessary references Microsoft.Office.Interop.PowerPoint Office ReachFramework Step 3: Insert DocumentViewer control into MainWindow.xaml [code language="xml"] <Window x:Class="DocumentViewer.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" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <DocumentViewer Name="Documentv...

Using the Mediator pattern in MVVM / WPF

Image
Some instructions on how to use the Mediator design pattern as a means of allowing communication between ViewModel classes in your MVVM / WPF application. In this example I use the Mediator as a means of communicating to the main window which view to display when the user clicks a button on either of the child views, View1 and View2. Step 1: Create a new WPF application Step 2: Add event handling classes EventArgs.cs [code language="csharp"] using System; namespace MvvmSwitchViews { public class EventArgs<T> : EventArgs { public EventArgs(T value) { Value = value; } public T Value { get; private set; } } } [/code] EventRaiser.cs [code language="csharp"] using System; namespace MvvmSwitchViews { public static class EventRaiser { public static void Raise(this EventHandler handler, object sender) { if (handler != null) { ...

Using the CefSharp Chromium Web Browser in WPF / XAML

Image
Some brief instructions on getting started with using the Chromium Web Browser in WPF. Step 1: Create a new WPF project Step 2: Obtain the CefSharp packages using NuGet Link for the NuGet packages is here: https://www.nuget.org/packages/CefSharp.WPF/ Select Tool > NuGet Package manager > Package Manager Console. Use the [code language="txt"] PM> install-package CefSharp.Wpf [/code] Step 3: add the CefSharp dll references Right-click on References, select 'Add reference' When the dialog appears, select the Browse button. Navigate to the 'packages' folder that NuGet has installed to your Visual Studio project. For this project I'm choosing the x86 versions... Add CefSharp.Wpf.dll: Add the CefSharp.dll, CefSharp.Core.dll, CefSharp.BrowserSubprocessCore.dll: If you get the error similar to the following: [code language="text"] Error 1 CefSharp.Common will work out of the box if you specify platform (x86 / ...

How to run processes and obtain the output in C#

Image
Example process: fsutil. Very useful link on obtaining and setting fsutil behaviour and properties at this following link: http://blogs.microsoft.co.il/skepper/2014/07/16/symbolic_link/ Typically contained in the System32 folder: C:\Windows\System32 It is quite simple to do and consists of two main steps: Step 1: Create Process object and set its StartInfo object accordingly [code language="csharp"] var process = new Process { StartInfo = new ProcessStartInfo { FileName = "C:\\Windows\\System32\\fsutil.exe", Arguments = "behavior query SymlinkEvaluation", UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true } }; [/code] Step 2: Start the process and read each line obtained from it: [code language="csharp"] process.Start(); while (!process.StandardOutput.EndOfStream) { var line = process.StandardOutput.ReadLine(); Console.WriteLine(line); } [/code] Full Code listing: [code language=...

Using the Gecko Web Browser in a C# Winforms Project

Image
Some particularly useful links: http://stackoverflow.com/questions/8778320/how-to-use-gecko-in-c-sharp https://xinyustudio.wordpress.com/2015/07/04/embedding-web-browsers-in-winform-applications/#more-3695 See this link if you're interested in using Gecko in a WPF project: https://www.technical-recipes.com/2017/using-the-gecko-web-browser-in-wpf/ Installing and using the latest GeckoFX-45.0 1. Create a new WinForms application: 2. Install the GeckoFX package via NuGet Select Tools > NuGet Package Manager > Package Manager Console At the Package Manager Console type 'install-package geckofx45': 3. Insert the Gecko control into your form. Select View > ToolBox and the new control should now be available: If this tool does not appear then right click on the 'General' tab of the Toolbox and select 'Choose items...'. The go to the COM Components tab and select the Browse button and locate and select the Geck winforms-r...