My sharing and discussion of topics in C#, WCF, WPF, Winforms, SQL, ASP.Net, Windows Service, Java Script .... with you all.
Showing posts with label Win Forms. Show all posts
Showing posts with label Win Forms. Show all posts
Friday, October 18, 2013
Binding DataSource to a ComboBox with DataView
To explain how to bind data source to a ComboBox with DataView, I wrote an article for the following site:
http://www.c-sharpcorner.com/UploadFile/0f68f2/programmatically-binding-datasource-to-combobox-in-multiple/
Binding DataSource to a ComboBox with DataTable
To explain how to bind data source to a ComboBox with DataTable, I wrote an article for the following site:
http://www.c-sharpcorner.com/UploadFile/0f68f2/programmatically-binding-datasource-to-combobox-in-multiple/
Binding DataSource to a ComboBox with DataSet
To explain how to bind data source to a with DataSet, I wrote an article for the following site:
http://www.c-sharpcorner.com/UploadFile/0f68f2/programmatically-binding-datasource-to-combobox-in-multiple/
Binding DataSource to a ComboBox with List
To explain how to bind data source to a ComboBox with List Values, I wrote an article for the following site:
http://www.c-sharpcorner.com/UploadFile/0f68f2/programmatically-binding-datasource-to-combobox-in-multiple/
Binding DataSource to a ComboBox with Array
To explain how to bind data source to a ComboBox with Array values, I wrote an article for the following site:
http://www.c-sharpcorner.com/UploadFile/0f68f2/programmatically-binding-datasource-to-combobox-in-multiple/
Binding DataSource to a ComboBox with Enumeration Values
To explain how to bind data source to a ComboBox with Enumeration Values, I wrote an article for the following site:
http://www.c-sharpcorner.com/UploadFile/0f68f2/programmatically-binding-datasource-to-combobox-in-multiple/
Tuesday, July 30, 2013
Spellchecker in WPF RichTextBox using Microsoft Word Object Library
Here is my article for spelleing checking in WPF RichTextBox:

http://www.c-sharpcorner.com/UploadFile/0f68f2/spelling-checking-using-microsoft-word-object-library-in-wpf/
Wednesday, July 3, 2013
How to set directory path for setup project in visual studio 2010
Here I am going to share how we can set directory path for our installed package.
Suppose you have a setup project already included in your main project. (To know how to add setup project in Visual Studio-2010, you may refer to article at:
http://www.c-sharpcorner.com/Blogs/9817/adding-a-setup-project-in-visual-studio-2010.aspx)
Now you want to set directory path for your install, so that your app get installed in the desired location. In order to do this, you need to follow steps as:
1.Right Click on your Setup project -> View -> File System
2.Then Click Application Folder, open property tab
3.Set 'AlwaysCreate' property to TRUE.
(This property creates a directory structure for your install, if directory structure doesn’t exist.)
4.Then Set 'DeafultLocation' property to your desired location (e.g. C:\MyWinApps\).
(You may choose any directory structure of your choice except some folders/Directories which are protected by Windows Operating System)
Wednesday, June 12, 2013
Automatic Windows Locking using Timer
I wrote an article for the same topic for C-SharpCorner site. Please go through the link below:
http://www.c-sharpcorner.com/UploadFile/0f68f2/automatic-system-locking-using-timer/
Thanks,
Hemant Srivastava
Friday, May 3, 2013
Select All, Clear All, Cut, Copy, Paste on List Box
Have you ever imagined our life without “Cut-Copy-Paste” in today’s computer era… specially a Software developer’s life! These clipboard operations are very common and most frequently used, but here I am going to discuss how to code them (on a List Box control) instead of just using them.
In this article, I am going to show some edit operations like: Select All, Clear All, Cut, Copy and Paste on a List Box control. To demonstrate this, I created a simple Win form application with a Menu Strip and a List Box control. On the menu Strip, all the operations are entered as menu items with short cut keys.
For better understanding, sample code is also attached with this article.
Click to download source code
Here is a screenshot of the GUI.
![]() |
Select All:
To select all the items in a ListBox, we clear all the
selected items firstly and keep on selecting item while iterating the entire
list.
private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
listBox1.SelectedItems.Clear();
for (int i = 0; i < listBox1.Items.Count; i++)
{
listBox1.SetSelected(i, true);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Paste:
To paste the text data into our List
Box, we firstly retrieve the text data from buffer using Clipboard class. Since
it is a list box where each item in the List Box should correspond to each line
in the buffer, so here we parse the Text data by a new line character and then
store each line as a new item in the List Box.
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
// Getting Text from Clip board
string s = Clipboard.GetText();
//Parsing criteria: New Line
string[] lines = s.Split('\n');
foreach (string ln in lines)
{
listBox1.Items.Add(ln.Trim());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Copy:
To
copy the text data into buffer, we iterate entire selected item collection and
keep on appending items into a StringBuilder object. To keep every item in a new row, we are using
here AppendLine()method after
inserting item text into stringbuilder. Then at the end we use Clipboard.SetData() method to copy
the data into buffer.
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
StringBuilder sb = new StringBuilder();
foreach (object row in listBox1.SelectedItems)
{
sb.Append(row.ToString());
sb.AppendLine();
}
sb.Remove(sb.Length - 1, 1); // Just to avoid copying last empty row
Clipboard.SetData(System.Windows.Forms.DataFormats.Text, sb.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Cut:
Basically
cut operation is the combination of copying and removing the selected items. In
order to do this, we start copying all the selected items (as above) into
buffer.
After copying, we remove all the selected items from the
List Box. For this we need another
collection to keep all the selected items. We are creating this separate
collection because we can’t delete item from the collection while iterating the
same collection using foreach loop.
private void cutToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
StringBuilder sb = new StringBuilder();
// we use this collection to keep all the Selected Items
List<object> selectedItemList = new List<object>();
foreach (object row in listBox1.SelectedItems)
{
sb.Append(row.ToString());
sb.AppendLine();
// Keep on adding selected item into a new List of Object
selectedItemList.Add(row);
}
sb.Remove(sb.Length - 1, 1); // Just to avoid copying last empty row
Clipboard.SetData(System.Windows.Forms.DataFormats.Text, sb.ToString());
// Removing selected items from the list box
foreach (object ln in selectedItemList)
{
listBox1.Items.Remove(ln);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Clear All:
This
is the simplest operation. We need to just clear all the items from List Box
using built-in Clear() method.
private void clearAllToolStripMenuItem_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
}
Thursday, May 2, 2013
Multiline TextBox scroll to bottom automatically
If we want to have our vertical scroll bar in a TextBox (obviously Multiline TextBox) pointing always to the bottom as we write data in the TextBox, we could do this using another way instead of using ScrollToCaret option.
When we write data in TextBox using textBox1.text = “This is my data… bla blah blah….”, by default the vertical scroll bar always stays at the top. But if we use AppendText propery of textbox, vertical bar automatically comes to bottom whenever data is written in the text box.
textBox1.AppendText(Environment.NewLine);
textBox1.AppendText(sMessage);
Wednesday, January 9, 2013
System Shutdown and Restart through C# Code
In this article, I am going to show how we can shutdown, restart, Lock, Logoff, Sleep etc. through C# Code.
Suppose your app needs to perform operations such as shutdown, restart, Logoff, Lock, Hibernation, Sleep through code, then you need to call a process that runs specific executable file (already stored in our system) along with their required arguments. Here I made a Utility Application ‘PC Controller’ that allows you to do these kind of such operations. A sample code is attached with this article.
After going through this article, we will get to know following points:
Adding items in ListView Control and showing them as Small Icons
Shutting Down, restarting, Locking , logging off operation through code
When we run this application, we get following Screen where each operation is showing as an icon in the ListView Control.
Figure-1
Let us see the above application in more detail. On this GUI designing, here a ListView Control is being used. Just follow the steps below to design GUI:
• To show item as icons, Click the arrow on the right side of the ListViewControl in designer mode and set the View to “SmallIcon”. (See Figure-2)
• Then drop an ImageList Control and add all your icon images in the ImageList Control by setting the “Images” property to folder path where we store our all icons.
• After associating images with ImageList Control, we will see ‘imageList1’ in combo box of “SmallImageList” property. Now set the Small image List property to ‘imageList1’.
• Now attach event handler for “SelectedIndexChanged” event.
Figure-2
In the “SelectedIndex_Changed” event handler, add the following code. In which we basically call a process by giving the executable file name and its command line arguments.
---------------------------------------------------------------------------------------------------------------
ProcessStartInfo startinfo = new ProcessStartInfo(filename, arguments);
Process.Start(startinfo);
----------------------------------------------------------------------------------------------------------------
Here is the full Code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
DoOperation(listView1.SelectedItems[0].Text);
}
private void DoOperation(string oparation)
{
string filename = string.Empty;
string arguments = string.Empty;
switch (oparation)
{
case "Shut Down":
filename = "shutdown.exe";
arguments = "-s";
break;
case "Restart":
filename = "shutdown.exe";
arguments = "-r";
break;
case "Logoff":
filename = "shutdown.exe";
arguments = "-l";
break;
case "Lock":
filename = "Rundll32.exe";
arguments = "User32.dll, LockWorkStation";
break;
case "Hibernation":
filename = @"%windir%\system32\rundll32.exe";
arguments = "PowrProf.dll, SetSuspendState";
break;
case "Sleep":
filename = "Rundll32.exe";
arguments = "powrprof.dll, SetSuspendState 0,1,0";
break;
}
ProcessStartInfo startinfo = new ProcessStartInfo(filename, arguments);
Process.Start(startinfo);
this.Close();
}
}
Tuesday, January 8, 2013
Free Bingo Ticket Generator Downlaod
Here I made a Bingo Ticket Generator. Feel free to download this user friendly version by clicking following link:
Download Bingo Ticket Generator!!!
Features:
Print: You can easily print a bunch of ticketsCopy-Paste: You may copy the tickets and paste them anywhere e.g. Notepad, MS-Word, MS-Excel
Export: You can directly export a bunch of tickets to a MS-Excel File
Image: You can save them in Image format too and then can print.
On running of this ticket generator app, you would see a screen as below:
Tuesday, November 27, 2012
Hangman Game (Guess the Word) in C#
This word guessing game is very similar to popular word guessing game: HANGMAN
(Only difference is that here computer is not drawing "Hangman" picture on every wrong guess by the guessing player... May be in next version, I might include that diagram...)
This game is developed as a Windows Application in C#. Feel free to download the game... and source code of this game.
Screen Shots:
Rules:
This game is generally played by two persons. One player has to guess a word which is given by another player. Here you have to play this game with Computer. In order to win this game, Guessing player has to guess the word correctly. In the guessing, player can not make more then five wrong guesses.
If he suggests more than five wrong letters, he loses the game.
Initially, the word to guess is represented by a row of dashes, giving the number of letters.
If the guessing player suggests a letter which occurs in the word, the other player (in this case Computer) writes it in all its correct positions.
If the suggested letter does not occur in the word, guessing player loses his guessing attempt.
The game is over when:
(Only difference is that here computer is not drawing "Hangman" picture on every wrong guess by the guessing player... May be in next version, I might include that diagram...)
This game is developed as a Windows Application in C#. Feel free to download the game... and source code of this game.
Screen Shots:
Rules:
This game is generally played by two persons. One player has to guess a word which is given by another player. Here you have to play this game with Computer. In order to win this game, Guessing player has to guess the word correctly. In the guessing, player can not make more then five wrong guesses.
If he suggests more than five wrong letters, he loses the game.
Initially, the word to guess is represented by a row of dashes, giving the number of letters.
If the guessing player suggests a letter which occurs in the word, the other player (in this case Computer) writes it in all its correct positions.
If the suggested letter does not occur in the word, guessing player loses his guessing attempt.
The game is over when:
- The guessing player completes the word, or guesses the whole word correctly
- The guessing player makes more than five wrong guesses and unable to guess the word correctl
Friday, November 23, 2012
Image Color Detector in C#
In this “Color Detector” application, firstly we select an image (jpeg,
bmp, png etc.) and then we pick the color which we want to detect on that
image. If we found the color, we display a confirmation message. The snapshot
for the running application is below:
Source of the image: We can take mage from any source (like Digital
Camera, any stored image in PC, Mobile etc.), and keep the image somewhere in
our system. Image can be in any format (i.e. jpeg, bmp, png etc.)
Image Selection:
For image file selection, we include an “OpenFileDialog” control into our form.
On “Choose Image” button click, we show “OpenFileDialog” to choose any image
file from the system. After image file selection, we get the file path and then
load the image into a picture box. For the file selection and loading into a
picture box, refer to following code:
private void ChooseImageBtn_Click(object sender, EventArgs e)
{
try
{
//Clearing previously selected image from picture box
ImagePathLbl.Text = "";
pictureBox1.Image = null;
//Showing the File Chooser Dialog Box for Image File selection
DialogResult IsFileChosen = openFileDialog1.ShowDialog();
if (IsFileChosen == System.Windows.Forms.DialogResult.OK)
{
//Get the File name
ImagePathLbl.Text = openFileDialog1.FileName;
//Load the image into a picture box
if (openFileDialog1.ValidateNames == true)
{
pictureBox1.Image = Image.FromFile(ImagePathLbl.Text);
}
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
Color Selection:
For color selection which we want to detect in our selected image, we include
an “ColorDialog” control into our form. On “Pick Color” button click, we show
“ColorDialog” to choose any color from the dialog box. After color selection,
we get the color name and then display the picked color into a panel by setting
Panel.BackColor property to picked color. If the picked color is a known color,
we also display the color name in text. For the color selection and displaying
the selected color, refer to following code:
private void PickColorBtn_Click(object sender, EventArgs e)
{
try
{
// Clean previously selected color
SelectedColorNameLbl.Text = "";
panelSelectedColor.BackColor = actualColor;
//Showing color choice
DialogResult IsColorChosen = colorDialog1.ShowDialog();
if (IsColorChosen == System.Windows.Forms.DialogResult.OK)
{
panelSelectedColor.BackColor = colorDialog1.Color;
//If it is know color, display the color name
if (colorDialog1.Color.IsKnownColor == true)
{
SelectedColorNameLbl.Text = colorDialog1.Color.ToKnownColor().ToString();
}
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
Color Detection:
For color detection, firstly we convert the loaded image in picture box to a
“BitMap” class. Refer to following MSDN link for more detail:
http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspxThen we iterate each pixel of image and get the pixel color. We store the each pixel color into a temporary variable named “now_color”. Get the 32-bit ARGB value of this “now_color” (a Color Object) with 32-bit ARGB value of our “Picked Color”. If both values are same, we display a “Color Found!” message and come out from the iteration, otherwise we keep on looking other pixel until we cover whole image. If we don’t get success in color finding, we show “Selected Color Not Found" Message. For the color detection, refer to following code:
private void DetectColorBtn_Click(object sender, EventArgs e)
{
try
{
Boolean IsColorFound = false;
if (pictureBox1.Image != null)
{
//Converting loaded image into bitmap
Bitmap bmp = new Bitmap(pictureBox1.Image);
//Iterate whole bitmap to findout the picked color
for (int i = 0; i < pictureBox1.Image.Height; i++)
{
for (int j = 0; j < pictureBox1.Image.Width; j++)
{
//Get the color at each pixel
Color now_color = bmp.GetPixel(j, i);
//Compare Pixel's Color ARGB property with the picked color's ARGB property
if (now_color.ToArgb() == colorDialog1.Color.ToArgb())
{
IsColorFound = true;
MessageBox.Show("Color Found!");
break;
}
}
if (IsColorFound == true)
{
break;
}
}
if (IsColorFound == false)
{
MessageBox.Show("Selected Color Not Found.");
}
}
else
{
MessageBox.Show("Image is not loaded");
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
Wednesday, November 14, 2012
A Free Download Party Game !!!
I created this party game which contains currently following games:
TELL A NAME:
In this game, some alphabets and one category will be shown on the screen and you have to tell any name from that category in which
the shown alphabets would come in the same order.
For Example: Suppose shown Alphabets are "R" and "S"
and Category is "PLACE"
So correct answer could be like: PARIS (As R comes before S)
(but not SAN FRANCISCO because in this anem S comes before R)
BINGO:
In this game, you are given a Bingo ticket and you have to cross the same number which is displayed here on your ticket.
Generally 5 types of prizes are kept for this game:
FIRST FIVE: Whoever crossed his first five numbers
FIRST ROW: Whoever crossed all the numbers in his first row
SECOND ROW: Whoever crossed all the numbers in his second row
THIRD ROW: Whoever crossed all the numbers in his third row
FULL HOUSE: Whoever crossed all the numbers in his ticket
___________________________
___________________________
Tuesday, November 13, 2012
Installing MonoDevelop IDE and Writing First program with MonoDevelop
For this topic, you may go to my article published in C# corner site:
Thanks...
Friday, October 19, 2012
How to Get the Selected Cells and Rows in the Windows Forms DataGridView Control
Here basically I am showing how to get selected cells/rows in DataGridView Control and use of IEnumerator while accessing them.
DataGridViewSelectedRowCollection m = dataGrid_ZipList.SelectedRows;
DataGridViewRow row;
IEnumerator Enumerator = m.GetEnumerator();
Enumerator.Reset();
while (Enumerator.MoveNext())
{
row = (DataGridViewRow)Enumerator.Current;
lstPostalCodes.Add(row.Cells["POSTAL_CODE"].Value.ToString());
}
Monday, July 30, 2012
How to pass image from one windows form to another window form
Step-1: Add a following reference to your project:
Sytem.Drawing
Step-2: Make a property as following in one of your Form (say Form1) so that this property would be accessible to another form (say Form2)
Image image_Form1;
public Image Image_Form1
{
get { return image_Form1; }
set { image_Form1 = value; }
}
Step-3: Then in your code get the image in a variable (say image_From1)
image_Form1= Image.FromFile("world.bmp");
Step-4: In your another form “Form2”, suppose you want to show the Form1’s image in a picture Box.
Make an object of Form1 in Form2.Form1 Form1Object = new Form1();
Step-5: Then you could assign Form1’s image to a picture Box like:
pictureBox.Image = Form1Object.Image_From1;
Sytem.Drawing
Step-2: Make a property as following in one of your Form (say Form1) so that this property would be accessible to another form (say Form2)
Image image_Form1;
public Image Image_Form1
{
get { return image_Form1; }
set { image_Form1 = value; }
}
Step-3: Then in your code get the image in a variable (say image_From1)
image_Form1= Image.FromFile("world.bmp");
Step-4: In your another form “Form2”, suppose you want to show the Form1’s image in a picture Box.
Make an object of Form1 in Form2.Form1 Form1Object = new Form1();
Step-5: Then you could assign Form1’s image to a picture Box like:
pictureBox.Image = Form1Object.Image_From1;
Subscribe to:
Comments (Atom)









