Archive

Archive for the ‘C#’ Category

How to Text Wrap a Label in WPF?

January 26th, 2010 joy No comments

WPF does not allow text wrapping for label control.Thus to wrap a label with a fixed width,we have to place the TextBlock control inside the label control and set its TextWrapping property to “Wrap”.

<Label Width=”50″>
<TextBlock TextWrapping=”Wrap”> I am label with width 50</TextBlock>
</Label>

Popularity: 8% [?]

Categories: C#, WPF Tags:

How to set border color of a panel in c#

December 7th, 2009 joy No comments

In c# for panel there no property to set border color directly.We can only set the border style using BorderStyle.

To set the border color of a panel,

Step 1 : Set BorderStyle property to None.

Step 2 : Draw a rectangle and set the pen color in paint event of the panel

private void panelpanelbordercolor1_Paint(object sender, PaintEventArgs e)
{
  e.Graphics.DrawRectangle(Pens.Red,
  e.ClipRectangle.Left,
  e.ClipRectangle.Top,
  e.ClipRectangle.Width - 1,
  e.ClipRectangle.Height -1);
  base.OnPaint(e);
}

Source Link

Popularity: 9% [?]

Categories: C# Tags: ,

How To Call a Button Click From Another Button in c#

November 8th, 2009 joy No comments

If there is a requirement for calling a button click event from another button in c#, there is a easy way to do.We just need to call the PerformClick Method of the button to be fired.

private void button1_Click(object sender, EventArgs e)
{
    button2.PerformClick();
}
 
private void button2_Click(object sender, EventArgs e)
{
    System.Windows.Forms.MessageBox.Show("I am called from button1");
}

Popularity: 5% [?]

Make Textbox Number Only – WPF

September 28th, 2009 joy No comments

Coding :

private void txt_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
e.Handled = !ValidNumeric(e.Text);
base.OnPreviewTextInput(e);
}
 
bool ValidNumeric(string str)
{
bool ret = true;
 
int l = str.Length;
for (int i = 0; i &lt; l; i++)
{
char ch = str[i];
ret &= Char.IsDigit(ch);
}
 
return ret;
}

Thanks dedjo

Popularity: 6% [?]

Categories: C#, WPF Tags: ,

Steps to Create Setup and Deployment Project in Dot Net VS 2008

July 10th, 2009 praveen 23 comments

Step 1
Create your own windows application. Create a new Windows application project in C# and named it as Sample.

d01

Step 2

Design your own application. Here we have a simple login form for example.

d02
Step 3
After completing the design and coding, build the solution of the project in release mode.

d03

Step 4
Check the Release folder for the file “ProjectName.exe”. Here in this example we have the project name as sample so we can find a file with the name Sample.exe. Double click the executable file and check the example.

d04

Step 5
Create a Deployment Project. Select the “Other Project Types” -> “Setup and Deployment” -> “Setup project”. Here we have the setup project for example as “SampleSetup”.

d05

Step 6
Add the Sample.exe project application file inside the “Application Folder”.

d06

Step 7

To make a shortcut for the project right click “File System on Target Machine” and create shortcut of the application. Here in this example the project shortcut is created in program files folder.

d07

Step 8
Create the shortcut of the application.

d08

Step 9
Rename the shortcut of the application.

d09

Step 10
Move the Shortcut file to specified target. Note if you need another shortcut for some other target also create use same steps.

d10

Step 11
Now build the solution in release mode.

d11

Step 12
The setup file created in release folder of the project specified path.

d12

Step 13
Run the setup, step the path to extract.

d13

d14

d15

Step 14
The SampleSetup project is extracted and shortcuts are created. Now run your application.

d16

Popularity: 30% [?]

Categories: Asp.net, C# Tags: ,

Copy-Paste to and from clipboard using c#

February 18th, 2009 admin No comments

To make the operations such as copy paste using c#,we have to use the Clipboard Class which provides methods to place data on and retrieve data from the system Clipboard.

Example :-

Copy the text from txtCopy to clipboard

 System.Windows.Forms.Clipboard.SetDataObject(txtCopy.Text, true);

Paste the text from clipboard to txtPaste

IDataObject clipData = Clipboard.GetDataObject();
if (clipData.GetDataPresent(DataFormats.Text))
{
txtPaste.Text = clipData.GetData(DataFormats.Text).ToString();
}
else
{
txtPaste.Text = "Could not retrieve data from the clipboard.";
}

Download

Popularity: 29% [?]

Categories: C# Tags:

Get Clients IP Address Using ASP.NET

February 18th, 2009 admin 3 comments

We can get the clients IP address using the Request object’s(The Request object retrieves the values that the client browser passed to the server during an HTTP request. ) property Request.ServerVariables Collection.
Request.ServerVariables Collection gets the values of predetermined environment variables.

“REMOTE_ADDR” gets the IP address of the remote host making the request. 

string ipaddr;

ipaddr = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

here the string “ipaddr” stores the clients ip.

List Of other Request.ServerVariables Object

Variable Description
AUTH_TYPE The authentication method that the server uses to validate users when they attempt to access a protected script.
CONTENT_LENGTH The length of the content as given by the client.
CONTENT_TYPE The data type of the content. Used with queries that have attached information, such as the HTTP queries POST and PUT.
GATEWAY_INTERFACE The revision of the CGI specification used by the server. Format: CGI/revision.

HTTP_<HeaderName> The value stored in the header HeaderName. Any header other than those
listed in this table must be prefixed by “HTTP_” in order for the
ServerVariables collection to retrieve its value.

Note: The server interprets any underscore (_) characters in
HeaderName as dashes in the actual header. For example if you specify
HTTP_MY_HEADER, the server searches for a header sent as MY-HEADER.

LOGON_USER The Windows NT® account that the user is logged into.
PATH_INFO Extra path information as given by the client. You can access scripts
by using their virtual path and the PATH_INFO server variable. If this
information comes from a URL it is decoded by the server before it is
passed to the CGI script.
PATH_TRANSLATED A translated version of PATH_INFO that takes the path and performs any necessary virtual-to-physical mapping.
QUERY_STRING Query information stored in the string following the question mark (?) in the HTTP request.
REMOTE_ADDR The IP address of the remote host making the request.
REMOTE_HOST The name of the host making the request. If the server does not have
this information, it will set REMOTE_ADDR and leave this empty.
REQUEST_METHOD The method used to make the request. For HTTP, this is GET, HEAD, POST, and so on.
SCRIPT_MAP Gives the base portion of the URL.
SCRIPT_NAME A virtual path to the script being executed. This is used for self-referencing URLs.
SERVER_NAME The server’s host name, DNS alias, or IP address as it would appear in self-referencing URLs.
SERVER_PORT The port number to which the request was sent.
SERVER_PORT_SECURE A string that contains either 0 or 1. If the request is being handled
on the secure port, then this will be 1. Otherwise, it will be 0.
SERVER_PROTOCOL The name and revision of the request information protocol. Format: protocol/revision.
SERVER_SOFTWARE The name and version of the server software answering the request (and running the gateway). Format: name/version.
URL Gives the base portion of the URL.

Popularity: 5% [?]

Categories: Asp.net, C# Tags:

Convert Text to Image using c#

February 13th, 2009 admin No comments

We can Convert text to image in c# using System.Drawing namespace.
Here we create a bitmap image and use its Graphics property to convert text passed to image and place the image in a picture box.

private void TextToImage()
{
Color BackColor = Color.White;
String FontName = "Times New Roman";
int FontSize = 25;
int Height = 50;
int Width = 300;
<span style="color: rgb(56, 118, 29);">
//creating bitmap image
<span style="color: rgb(0, 0, 0);">Bitmap </span></span>bitmap = new Bitmap(Width, Height);
 
<span style="color: rgb(56, 118, 29);">//FromImage method creates a new Graphics from the specified Image.</span>
Graphics graphics = Graphics.FromImage(bitmap);
Color color = Color.Gray;
Font font = new Font(FontName, FontSize); 
 
SolidBrush BrushBackColor = new SolidBrush(BackColor);
Pen BorderPen = new Pen(color);
 
Rectangle displayRectangle = new Rectangle(new Point(0, 0), new Size(Width - 1, Height - 1));
<span style="color: rgb(56, 118, 29);">//Drawing a rectangle and filling the background as white
<span style="color: rgb(0, 0, 0);">graphics</span></span>.FillRectangle(BrushBackColor, displayRectangle);
graphics.DrawRectangle(BorderPen, displayRectangle); <span style="color: rgb(56, 118, 29);">
 
//giving the font and color to the string passed</span>
graphics.DrawString(txtEnter.Text,font,Brushes.Red, 0, 0);
pictureBox1.Image = bitmap;
 
}

Download

Popularity: 5% [?]

Categories: C# Tags:

Get available server names of Sql Server within the local network – SqlDataSourceEnumerator

January 7th, 2009 admin No comments

SqlDataSourceEnumerator is a class in .net framework that gets the available instances of SQL Server within the local network.

DataTable dt = SqlDataSourceEnumerator.Instance.GetDataSources();

SqlDataSourceEnumerator.Instance.GetDataSources() returns a datatable with the available servers in the current network.

The DataTable returned as 4 colunms such as :-

  • ServerName
  • InstanceName
  • IsClustered
  • Version

Popularity: 1% [?]

Categories: C# Tags:

Sorting Program using C#

January 2nd, 2009 admin No comments
namespace SortingNos
{
   class Program
   {
       static void Main(string[] args)
       {
           Console.WriteLine("Enter Numbers :");
           int[] arr = new int[5];
           string arr1 = "";
           for (int i = 0; i < 5; i++)
 {
 arr1 = Console.ReadLine();
 arr[i] = Convert.ToInt32(arr1);            
}
 
 int a, b, temp = 0;
 Console.WriteLine();
 for (a = 0; a < arr.Length; a++)
           {
                               for(b=a+1;b< arr.Length;b++)
               {
                   if (arr[a] > arr[b])
                   {
                       temp = arr[a];
                       arr[a] = arr[b];
                       arr[b] = temp;
                   }
               }             
               Console.WriteLine(arr[a]);              
           }          
          Console.ReadLine();
       }
   }
}

Download

Popularity: 2% [?]

Categories: C# Tags: