Posts

Donate

Custom DateTimePicker Control With Background Color And Icon In Windows Forms

Image
Hello Here's a custom DateTimePicker with background color and image icon instead of using the ComboBoxRenderer class. The icon is an image that is added to the project as part of it's resource. The adding of icon is processed through the WndProc method while the setting of background color is handled in the OnPaint() event. Notice that in the constructor, the SetStyle()'s parameters are ControlStyles.UserPaint so that the control paints itself and true to apply the specified style to the control. public class DateTimePickerWithBackground : DateTimePicker { private Bitmap _bmp; enum BorderSize { One = 1, Two = 2 }; public DateTimePickerWithBackground() { _bmp = new Bitmap(ClientRectangle.Height, ClientRectangle.Width); this .SetStyle(ControlStyles.UserPaint, true ); } protected override void WndProc( ref Message m) { base .WndProc( ref m); if (m.Msg == 0xf) //WM_PAINT message { Graphics g = Graphics.FromHwnd(m.HWnd); g.Dra

Load Images in a Windows Form Custom Control

Image
Good day! Often times when developing custom controls you will add images or icons to enhance the UI through the Bitmap class. But when getting the image from file using Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + @"\calendar.png") you may encounter issues such as "path is null" since the path pointed by the BaseDirectory isn't the executable path. A simple workaround is to set the path of the image using hardcoded like Image.FromFile(@"C:\Images\ProjectX\calendar.png") . While this is acceptable, it is ugly to look at and may cause potential issues once the image has been transferred to another location. An accepted solution is to add the image as a project resource then reference it in the custom control code. To accomplish that, here are the steps. * Right Click on the Project -> Properties * In Resources, select Add Resource Dropdown -> Add Existing File - Choose the image or icon to be used as resource. Once done, it wil

MVVM Basics With TextBlock Control

Hello, This post is based on the article Understanding the basics of MVVM design pattern . The author demonstrated the basics of MVVM using TextBlock controls. However, the code samples have several issues and in order for the sample application to work, I revise them with the following changes. BindableBase.cs - since SetProperty method uses T in it's parameter, you also need to reference T in your classname. public class BindableBase <T> : INotifyPropertyChanged { ... } MainPageViewModel.cs - update the code in the constructor to bind a single object to the TextBlock controls. public class MainPageViewModel : BindableBase<Book> { private Book _book; public Book Book { get { return _book; } set { SetProperty( ref _book, value ); } } public MainPageViewModel() { Book = new Book() { Title = "Harry Potter" , Author = "J. K. Rowling" , Category = "Young-adult fiction"

Export ASP.NET GridView To Excel And Preserve Leading Zeros

Image
Hi, There was a question raised on how to export data from ASP.NET Gridview to Microsoft Excel and preserve leading zeros of numeric values. A workaround that we have is to insert an   character before the cell value if that value starts with zero (0). So if the value starts with zero such as 0001, the result would be " 0001". Thus when exported to Excel, the leading zero is kept. cell.Text = string .Format( "&nbsp;{0}" , cell.Text); A suggested solution is to insert a Tab character ( ) but this fails on some occasions. After doing some research, replacing the tab character with &ensp; or &emsp; would also fix the issue. cell.Text = string .Format( "&ensp;{0}" , cell.Text); //or cell.Text = string .Format( "&emsp;{0}" , cell.Text); ASP.NET GridView Export Excel Report Cheers!

WPF DataGrid Data Binding Using MVVM Pattern

Image
Hello, I've been developing WPF applications before but not having applied the MVVM design pattern and have been wanting to create a simple program that loads data into the DataGrid control. As a result of free time, here's a simple demonstration on using MVVM Pattern for Data Binding a WPF DataGrid control. According to Wikipedia, the components of an MVVM pattern are: Model Model refers either to a domain model, which represents real state content (an object-oriented approach), or to the data access layer, which represents content (a data-centric approach). View As in the MVC and MVP patterns, the view is the structure, layout, and appearance of what a user sees on the screen. View model The view model is an abstraction of the view exposing public properties and commands. Instead of the controller of the MVC pattern, or the presenter of the MVP pattern, MVVM has a binder. In the view model, the binder mediates communication between the view and the data binder.The view

Disable Button In AngularJS Using ng-disabled On It's First Page Load In ASP.NET MVC

Hello, One might notice that during page load of an ASP.NET MVC application, the button with ng-disabled attribute and have values such as $invalid or $dirty does not disable the button. Thus, this will enable the user to submit the form with empty values to the controller method. In order to disable the button on page load, add $pristine values in the ng-disabled attribute such as the code below. Original Code <input type= "submit" id= "btnAddEmployee" class = "btn btn-primary" value= "Save" ng-click= "Save()" ng-disabled= "AddEmp.fname.$dirty && AddEmp.fname.$invalid || AddEmp.lname.$dirty && AddEmp.lname.$invalid || AddEmp.salary.$dirty && AddEmp.salary.$invalid" /> Modified code with $pristine <input type= "submit" id= "btnAddEmployee" class = "btn btn-primary" value= "Save" ng-click= "Save()" ng-disabled= &qu

$location.path() Method Not Reloading New Data In ASP.NET MVC Web API With AngularJS

Hello, I was trying to integrate AngularJS into a simple ASP.NET MVC Web API application with Save functionality. Basically, when you have finished posting data the next logic will be to redirect the user to the display all page. However, I stumbled into an issue in which the $location service does not reload data. According to the docs,the $location service allows you to change only the URL; it does not allow you to reload the page. So after placing breakpoints in the Api Controller, I noticed that the GetAll() method is called first next is the Save() method. The solution to this dilemma is to transfer the $location.path() method inside the success function so that the Save() method in the Api Controller is executed first. Code With Issue: $location.path() is outside the success function. $scope.Add = function () { $http({ method: "POST" , data: $scope.employee, url: "/api/employees" }) .then( function (response) { $scope.employees = response.data;

Adding ASP.NET 5 Templates To VS 2015

Image
Good day! One might notice that the ASP.NET 5 templates are not visible when creating a new Web Application Project in Visual Studio 2015 given that this is a fresh installation of the software. So to add the ASP.NET 5 Templates, download the ASP.NET 5 run-time tools Microsoft ASP.NET and Web Tools 2015 (RC) – Visual Studio 2015 and proceed with the setup. Restart your machine afterwards. That's it.. :-)

This version of Visual Studio requires the April 2014 update to Windows 8.1 (VS 2015)

Hello, Now that I've got the time to install Visual Studio 2015 Enterprise on my laptop with Windows 8.1 Operating System I decided to give it a go. That's when the moment I opened the installer file, an issue pops out of the screen with the description " This version of Visual Studio requires the April 2014 update to Windows 8.1 and Windows Server 2012 R2 known as KB2919355 ". The solution to this issue can easily be found at stackoverflow. But missing the links of the packages needed to resolve the problem. A step by step example is presented below. 1. Download update package 2919442 and install update to your pc. This is the pre-requisite update of package 2919355     x86     x64 2. Download update package 2919355 and install update to your pc.     x86     x64 3. After those updates have been installed, restart the computer for the updates to take effect. Cheers! :-)

How To Post JSON Data With WebRequest In .NET

Hello, There was a question on how to post a JSON data using the WebRequest class on both C# and VB.NET given that using Ajax, the post data would look like this. data : '{ "age": "78", "weight": "51" }' In .NET, you need to store the JSON data into a string object and escape those double quotes before passing it to the WebRequest object. C#.NET string postData = "{ \"age\": \"78\", \"weight\": \"51\" }" ; VB.NET Dim postData As String = "{ ""age"": ""78"", ""weight"": ""51"" }" Cheers! :-)

Donate