Tag Archives: C#

Microsoft Visual C# is simple, modern, general-purpose, object-oriented programming language, included in the Microsoft Visual Studio. Visual C# is also mostly used in ASP.NET web sites and Windows applications.

Search for an element in array with Array.Find method in c#

 
Array.Find method searches for an element that matches the conditions defined by the specified predicate(The Predicate is a delegate to a method that returns true if the object passed to it matches the conditions defined in the delegate.), and returns the first occurrence within the entire specified Array.
Continue reading Search for an element in array with Array.Find method in c#

Dynamic CheckedListBox in c#

 
The following example shows how to create checkedlistbox control at run time with event handling using c# programming language. it also demonstrate how to bind checklistbox control at run time with different methods.

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class Form2
{
	CheckedListBox chkListBox;
 
	private void Form2_Load(System.Object sender, System.EventArgs e)
	{
	}
	private void CreateCheckedlistBox()
	{
		chkListBox = new CheckedListBox();
		chkListBox.FormattingEnabled = true;
		chkListBox.Location = new System.Drawing.Point(12, 34);
		chkListBox.Name = "CheckedListBox1";
		chkListBox.Size = new System.Drawing.Size(120, 94);
		chkListBox.CheckOnClick = true;
		chkListBox.ItemCheck += chkListBox_ItemCheck;
		this.Controls.Add(chkListBox);
	}
 
	private void chkListBox_ItemCheck(System.Object sender, System.Windows.Forms.ItemCheckEventArgs e)
	{
		MessageBox.Show(chkListBox.Items(e.Index).ToString);
	}
	private void BindCheckedlistBox()
	{
		chkListBox.Items.Add("Londan");
		chkListBox.Items.Add("New Delhi");
		chkListBox.Items.Add("NewYork");
	}
	public Form2()
	{
		Load += Form2_Load;
	}
 
	//Private Sub BindCheckedlistBox()
	//    Dim cities() As String = {"Londan", "New Delhi", "NewYork"}
	//    chkListBox.Items.AddRange(cities)
	//End Sub
}

Dock property of control in c#

 
You can set the control borders to docked to its parent control through Dock property. Dock property of any control determines how a control is resized with its parent control.means how a control automatically resized as its parent control is resized. for example if you set the dock property of a control to Left, then it automatically align with the left side of its parent and it will automatically resize as the parent control is resized.

Dock property
Dock Locations

There is one point to need consider here that Anchor and Dock properties are mutually exclusive. Only one can be set at a time and the last one set takes precedence.

Example

The following c# example creates a Panel and sets some of its common properties and then set the Dock property of this to Top.
and then set the Dock property of this to Top.

private void CreatePanelAndSetDock()
{
	Panel Pnl = new Panel();
	Pnl.BackColor = Color.Gray;
	Pnl.Height = 50;
	Pnl.BorderStyle = BorderStyle.Fixed3D;
	this.Controls.Add(Pnl);
	Pnl.Dock = DockStyle.Top;
}

Create a new Word document in c#

 
This article describes how to create a new Word document in c#.

follow these steps:
1. Start a new Windows application in c# language.
2. Add a reference to the Microsoft Word Object Library
3. Add a button to Form1 from Toolbox.
4. Double-click Button1, create a Button click event.
5. Write code on the click event of button:

 private void CreateWordDocument()
        {
            object oMissing = System.Reflection.Missing.Value;
 
            Microsoft.Office.Interop.Word._Application objWord;
            Microsoft.Office.Interop.Word._Document objDoc;
            objWord = new Microsoft.Office.Interop.Word.Application();
            objWord.Visible = true;
            objDoc = objWord.Documents.Add(ref oMissing, ref oMissing,
                ref oMissing, ref oMissing);
 
            Microsoft.Office.Interop.Word.Paragraph objPara;
            objPara = objDoc.Content.Paragraphs.Add(ref oMissing);
            objPara.Range.Text = "Welcome to Author Code";
            objPara.Range.Font.Bold = 1;
            objPara.Format.SpaceAfter = 30;   
            objPara.Range.InsertParagraphAfter();
        }

Character casing in Textbox control

You can use the CharacterCasing property of the TextBox control in .net to change the case of characters as required by your application. For example,
you could change the case of all characters entered in a TextBox control used for username and password. When user can enter some textbox in both textbox, these textbox automatically change the text into upper case.
This example requires two textbox (txtUsername, txtPassword) and one button(see in the picture).
we can set the CharacterCasing property of the TextBox control through designer window as well as code.

 

 

[vb]

txtUsername.CharacterCasing = CharacterCasing.Upper
txtPassword.CharacterCasing = CharacterCasing.Upper

[c#]

txtUsername.CharacterCasing = CharacterCasing.Upper;
txtPassword.CharacterCasing = CharacterCasing.Upper;