Posts

Showing posts with the label Windows Forms

Donate

Windows Forms CRUD (Create/Update/Delete) Application In .NET Core And Visual Basic

Image
Hello, Here's a Visual Basic tutorial on how to create a CRUD (Create/Update/Delete) application using Windows Forms In .NET Core. The same concept is applied to a Windows Forms application in .NET Framework. Most of the concepts and logic were borrowed from the C# version here except that a different programming language is used. First is to create a Students table on your SQL Server Database using the script below. USE [TestDatabase] GO /****** Object: Table [dbo].[Students] Script Date: 03/31/2014 14:11:36 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Students]( [ID] [int] IDENTITY (1,1) NOT NULL , [Name] [varchar](50) NULL , [Age] [int] NULL , [Address] [varchar](50) NULL , [Contact] [varchar](50) NULL , CONSTRAINT [PK_Students] PRIMARY KEY CLUSTERED ( [ID] ASC ) WITH (PAD_INDEX = OFF , STATISTICS_NORECOMPUTE = OFF , IGNORE_DUP_KEY = OFF , ALLOW_ROW_LOCKS = ON , ALLOW_PAGE_LOCKS =

Windows Forms CRUD (Create/Update/Delete) Application In .NET Core And C#

Image
Hello, Here's a simple tutorial on how to create a CRUD (Create/Update/Delete) application using Windows Forms In .NET Core. The same concept is applied to a Windows Forms application in .NET Framework. First is to create a Students table on your SQL Server Database using the script below. USE [TestDatabase] GO /****** Object: Table [dbo].[Students] Script Date: 03/31/2014 14:11:36 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Students]( [ID] [int] IDENTITY (1,1) NOT NULL , [Name] [varchar](50) NULL , [Age] [int] NULL , [Address] [varchar](50) NULL , [Contact] [varchar](50) NULL , CONSTRAINT [PK_Students] PRIMARY KEY CLUSTERED ( [ID] ASC ) WITH (PAD_INDEX = OFF , STATISTICS_NORECOMPUTE = OFF , IGNORE_DUP_KEY = OFF , ALLOW_ROW_LOCKS = ON , ALLOW_PAGE_LOCKS = ON ) ON [ PRIMARY ] ) ON [ PRIMARY ] GO SET ANSI_PADDING OFF GO Create a Windows Forms App called "CRUDApplicationWi

Activate Or Show Windows Forms Missing Controls Including BindingNavigator In .NET 5 or .NET Core Winforms Application

Image
Gents Good Day! There was a question on forums on why the BindingNavigator control was either missing or grayed out in Visual Studio Toolbox of which the project's target framework is .NET 5. I decided to create a C# Windows Forms application using Visual Studio 2019 that targets the .NET 5 framework and infact, the BindingNavigator control is missing. After searching the net, I found an interesting link in StackOverflow Activate missing Winforms controls in .Net Core 3.1 which is applicable to .NET Core 3.1. I applied the steps stated in the post's answer to the Windows Forms project that I created and it works. Below are the steps to do that. 1. Comment Application.SetHighDpiMode(HighDpiMode.SystemAware); code in Program.cs file. static void Main() { //Application.SetHighDpiMode(HighDpiMode.SystemAware); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault( false ); Application.Run( new Form1()); }

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

AllowUsersToAddRows In DataGridView Not Working If DataSource is List<T>

Greetings! Going back to a previous project of mine, I found out that some DataGridView control's DataSource where set using List<T> and as a result, prevented the users to add new data to the DataGridView. private void List() { List<Item> list = new List<Item>(); for ( int i = 0; i < 100; i++) { list.Add( new Item() { ID = i, Name = String.Format( "{0}:{1}" , "Test" , i.ToString()) }); } DataGridView1.DataSource = list; } The common solution is to use DataTable as the DataSource but if we want to use a List type object, we could use BindingList<T> or BindingSource. Both of these have AllowNew property which indicates that you can add items to the list using the AddNew() method. It is stated in DataGridView.AllowUsersToAddRows that " If the DataGridView is bound to data, the user is allowed to add rows if both this property and the data source's IBindingList.AllowNew property are set to true. ".Well, Lis

DataGridView ComboBox Cascade In Windows Forms

Image
Good day to all! Here's an example of how to perform a combobox lookup or cascade using two DataGridViewComboBoxColumns inside a DataGridView control Combo Lookup in DGV . The solution presented is in VB.NET, so I decided to create a C# equivalent for this. In form load event, populate two DataTables for Roles and Employees. Each employee is assigned to a specific role. private void Form1_Load( object sender, EventArgs e) { DataGridView1.Rows.Add(); dtRole.Columns.Add( "RoleID" ); dtRole.Columns.Add( "RoleName" ); dtRole.Rows.Add(1, "Admin" ); dtRole.Rows.Add(2, "Instructor" ); dtRole.Rows.Add(3, "Utility" ); dtEmployee.Columns.Add( "RoleID" ); dtEmployee.Columns.Add( "EmployeeID" ); dtEmployee.Columns.Add( "EmployeeName" ); dtEmployee.Rows.Add(1, 1, "Sam" ); dtEmployee.Rows.Add(1, 2, "Nicole" ); dtEmployee.Rows.Add(2, 3, "Donald" ); dtEmployee.Rows.Add(

DataGridViewComboBoxColumn Show Dropdown In Single Click Instead Of Double Click

Hi, When adding DataGridViewComboBoxColumn control to a DataGridView control, the dropdown shows when you double click instead of single click. The common fix to this is to set the EditMode property to EditOnEnter . Given that you don't want to alter the default settings of the DataGrid control and you want to handle it through code,I found the solution in this website DATAGRIDVIEWCOMBOBOXCOLUMN REQUIRES MULTIPLE CLICKS TO SELECT AN ITEM from a comment made by a developer. However, there's a slight issue in the code provided since a column index returned might have a -1 index and this will cause an Unhandled Exception . The revision made is to add a condition that will check if the ColumnIndex of the DataGridView cell is greater than or equal to 0. private void DataGridView1_CellClick( object sender, DataGridViewCellEventArgs e) { DataGridView grid = (DataGridView)sender; if (e.ColumnIndex >= 0) { if (grid.Columns[e.ColumnIndex].Name == "Role" || grid.

Change DataGridViewRow FontStyle To Bold In CellFormatting Event

Hello, There was a question raised in the forums on how to change the FontStyle of a DataGridViewRow to Bold in the CellFormatting event. A solution is to set the Font property of a row with a Font object setting it's second parameter to FontStyle.Bold. private void EmployeeDTRDataGridView_CellFormatting( object sender, DataGridViewCellFormattingEventArgs e) { if (EmployeeDTRDataGridView.Columns[e.ColumnIndex].Name == "OnLeave" ) { if (e.Value != System.DBNull.Value) { if (Convert.ToBoolean(e.Value) == true ) { EmployeeDTRDataGridView.Rows[e.RowIndex].DefaultCellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Bold); } } } }

Donate