C# Int (Integer) Infinity

Background

I’m implement a data structure which is used to convert a time range to human-readable text, e.g. 0-7 days to “Within 1 Week”, 7-30 days to “1 Week to 1 Month”. Here I use Tuple<int,int> to represent the time range but not two integers, because we can define a Dictionary<Tuple<int, int>, String> to represent a map from time range to text.

But how could we represent “Over 2 Weeks”? Apparently the second parameter of the Tuple should be infinity.

Infinity

To represent infinity for an integer, we can use Int.MaxValue

So we can solve above problem using following code

 

IKVM.NET Convert JAR to .NET (C#, VB.NET) DLL

Why Convert JAR to .NET DLL

Imagine your company builds an online file converter, and its backend programming language is Java because of its position in web development. And in this project you build a file parsing library (jar format certainly) which is used to parse different types of files.

After this project is done, your boss decides to create a Windows desktop version for this software. Now the best choice is .NET, but how can you reuse your java parsing library? That’s why IKVM.NET existed.

IKVM.NET Introduction

IKVM.NET is a .NET implementation of Java which allows us to run Java code in .NET (C#, VB.NET, etc.) easily. It includes following three components:

  • JVM in .NET (which means you can run java byte code in .NET)
  • Java class library implementation
  • A compiler converting java byte code to .NET IL

There are many famous Java projects converted to .NET using IKVM.NET already, such as Tika, Curator, etc. Even Mono has include it as a built-in component.

Start Converting

This will generate a DLL file named libpst.dll from libpst.jar in current directory.

Without -target:library option, it will generate an exe file.

 

WPF Dragablz Tutorial Part 2: Set Custom Tab Host Window

Introduction

Dragablz is a WPF Tab Control which can let us to drag and drop tabs, drag tabs out as floating window and docking support, etc. It’s a pretty good control, but its documentation is too few.

In Dragablz’s official tutorial, we use default InterTabController for TabablzControl, and InterTabClient property is not specified for InterTabController, so tearing a tab out directly will only create a window which is same as the container window of the TabItem.

In this tutorial I will show you how to create a custom Tab Host Window.

 

Step 1 – Implement IInterTabClient

To set a custom Tab Host window, we should implement the IInterTabClient interface, which contains two method signature

Only the first method GetNewHost will be used here, so we can ignore the TabEmptiedHandler method.

Create a new class named MainInterTabClient.cs and make it implement IInterTabClient interface, then modify the GetNewHost method like following

The return value is the new host for dragged out tab, next we will create a new window for it.

 

Step 2 – Create a new Tab Host Window

Add a new window named TabHostWindow

Visual Studio add new window

Visual Studio add new window

In XAML View, add following code

 

Step 3 – Add Window into InterTabClient

Go back to MainInterTabClient.cs, update the GetNewHost method

It will create an instance of our newly created TabHostWindow as the host of dragged out TabItem.

Step 4 – Create a ViewModel for MainWindow

Next we need create a ViewModel class to bind newly created InterTabClient, name this class MainWindowViewModel.

 

Step 5 – Bind ViewModel

Now we need bind the ViewModel we created just now, add following line into your MainWindow constructor:

Then modify XAML of your MainWindow

 

Finally, we finish it! Drag a tab out you will see the tab will be inside a new clean window.

WPF ModernUI Tutorial Part 1

Introduction

ModernUI is a nice WPF theme and a styling framework which let you build a modern, beautiful application easily. Are you poor in designing skill and have problem with designing GUI for your WPF applicaiotn? If so, you have to try ModernUI, it is the best choice for you. In web design, there is Bootstrap for building great UI. And for WPF, it is Modern UI.

 

Screenshots

Mordern UI Screenshots

Mordern UI Screenshots

WPF ModernUI Screenshots

WPF ModernUI Screenshots

 

Installation

The installation is really easy, just search “ModernUI” in Manage NuGet Package Window, and select “ModernUI for WPF” item and click “Install”

Install ModernUI for WPF in NuGet Package Manager

Install ModernUI for WPF in NuGet Package Manager

Or run following command in Package Manager Console

 Install-Package ModernUI.WPF

 

Create a ModernUI Window

First create a new WPF Application Project named “ModernUISample”. After creating project, select MainWindow.xaml, change the XAML as following:

The ModernWindow class is defined under FirstFloor.ModernUI.Windows.Controls, so add following line to MainWindow.xaml.cs to import this namespace

Then let MainWindow inherit from class ModernWindow

 

Click “Debug” button to run the program, we will see a black rectangle with a light blue border is shown. What’s wrong with it? It’s because that we have not applied the theme files to our application.

Open App.xaml, and add following code:

 

 

Run the program again, you will see a modern window is shown

Modern UI blank window

mysql auto backup

First create a shell script which is used to backup database, here we named the script as mysql_backup.sh:

In above command, we use date command to generate current date, and use it as part of filename.

After that we need to make this file executable

 

To make this backup script run automatically, we should create a cronjob. Type following command to open cronjob editor

crontab -e

Write following text and save it

Above command means the cronjob will run at 0:00 everyday

 

 

Check MySQL case-insensitive query or table name

I tried to install Umbrao CMS, it supports MySQL database but it need MySQL to support case-insensitive query and table name.

 

Following is way to check whether case-insensitive query is supported:

show variables where variable_name=’lower_case_table_names';

or check lower_case_table_names option in my.ini (MySQL configuration file)

 

If this option’s value is set to 1, then case-insensitive query and table name is supported.

QWebEngine crash with exception code 0x80000003

QWebEngine is a new web browser engine introduced in Qt 5.4, it’s used to replace the old QWebView. QWebView is based on WebKit, while QWebEngine is based on Chromium browser, so it’s faster and supports more features.

I wrote a very simple program to test QWebEngine, following is the code:

Compiling and running is smooth. But when I tried to run this demo on another machine, it crashed, and an error dialog is shown with the message “An unknown software exception (0x80000003) has occurred at 0x023c5b7e. Click OK to terminate the program.

By chekcing the log file (debug.log) I found some helpful information:

[ERROR:icu_util.cc(154)] Couldn’t mmap D:\Qt\Qt5.4.0\5.4\msvc2013_64\icudtl.dat
[FATAL:content_main_runner.cc(719)] Check failed: base::i18n::InitializeICU()

We can see the icudtl.dat path is not correct, because it doesn’t exist at all.

To fix it we need to create a new file named “qt.conf” under the application directory, and fill it with following content:

 

(This bug is posted at https://bugreports.qt.io/browse/QTBUG-42083)

WebServlet cannot be resolved to a type

WebServlet annotation is introduced in Servlet 3.0, it’s used to simplify the servlet creation (we don’t need to define it and its URL mapping in web.xml)

The WebServlet annotation usage is

e.g.

Then the user can visit /servlet/GetMailFileOwner to access this servlet

 

But today I met a problem with using WebServlet annotation. First I added WebServlet on the Linux machine (Eclipse Luna), and switched to another Windows machine (MyEclipse), then I tried to rebuild the project, I got following error:

On the Windows machine, I use the default servlet.jar which is added when I create the new Dynamic Web Project. But I replaced the default servlet.jar file with Tomcat 8.0 libraries.

So the solution is to add Tomcat 8.0 libraries to Java Build Path (We need first download Tomcat 8.0 and set it up for Eclipse, otherwise Eclipse will not find this Tomcat 8.0 libraries)

HTML set checkbox as readonly

Sometimes we don’t want to make some form fields non-editable to users, like Product ID, Order Created Date, Post Author, etc.

For HTML text input and textarea controls, we can add “readonly” attribute to disallow users to modify their value, but this attribute will not work for checkbox control. So how should we disable the checkbox (or set it as readonly)? The “disabled” attribute will do the job

(It works for both radio box and checkbox)

 

You will see following checkbox and radio button is disabled:


Fabric.js set text color

Fabric.js is a powerful javascript canvas library which wraps html5 canvas operation, and introduces objects (text, shape, path, etc.) to developer.

There is a common method set() for object, it can be used to set property of an object. But if you want to change text color, text.set(‘color’, ‘#0f0′) will not work.

To set color of text, we need modify the fill attribute or use setColor() method (only available for text object)

Or