Sunday, 19 February 2012

Console Output from a WinForms Application.

Console Output from a WinForms Application.

You may wish to enable your WinForms application to run from a console window or command line. And when it does, you probably want to send output messages to the console window that launched your WinForms application.

Unfortunately Console.WriteLine()–the standard method of writing to the console window–by default will not work from a WinForms application. That’s because the console window that launched your WinForms application belongs to the cmd.exe process, which is separate from your WinForms application process.

So to redirect output from a WinForms application to the console window that launched it, use the AttachConsole Win32 method introduced in Windows XP. AttachConsole attaches the current process to the console window of another process. The special parameter ATTACH_PARENT_PROCESS attaches to the parent process, which in this case is the console window that launched the WinForms application.

Simple Example

Following is a simple WinForms application that redirects its output to the console window that launched it:

using System; using System.Runtime.InteropServices; using System.Windows.Forms;  namespace MyWinFormsApp {     static class Program     {         [DllImport( "kernel32.dll" )]         static extern bool AttachConsole( int dwProcessId );         private const int ATTACH_PARENT_PROCESS = -1;          [STAThread]         static void Main( string[] args )         {             // redirect console output to parent process;             // must be before any calls to Console.WriteLine()             AttachConsole( ATTACH_PARENT_PROCESS );              // to demonstrate where the console output is going             int argCount = args == null ? 0 : args.Length;             Console.WriteLine( "nYou specified {0} arguments:", argCount );             for (int i = 0; i < argCount; i++)             {                 Console.WriteLine( "  {0}", args[i] );             }              // launch the WinForms application like normal             Application.EnableVisualStyles();             Application.SetCompatibleTextRenderingDefault( false );             Application.Run( new Form1() );         }     } }

One Gotcha

There is one problem with this approach. If you redirect the console window output to a text file, for example using the redirect arrow:

MyWinFormsApp.exe >output.txt arg1 arg2

In this case, output will not redirect to the “output.txt” text file as expected, but instead will continue to appear in the console window. Please comment if you have a solution to this issue.

Show Continuous Progress with .NET ProgressBar and MarqueeAnimationSpeed

For some operations such as logging on to a web site or downloading a web page, you may not know how long it will take the operation to finish. So instead of showing a progress bar with a specified percent complete, you can set the .NET ProgressBar to cycle continuously.

To make a ProgressBar cycle continuously, set the MarqueeAnimationSpeed property to a positive value (by default, it is set to 0). The value specifies the time period, in milliseconds, that it takes the progress block to scroll across the progress bar. A higher value results in a slower speed, and a lower value results in a faster speed. I’ve found that a value of 30 works pretty well. It’s also important to set the Style property to ProgressBarStyle.Marquee.

this.ProgressBar_Download.MarqueeAnimationSpeed = 30; this.ProgressBar_Download.Style = ProgressBarStyle.Marquee;

To stop the cycle, set the Marquee Animation Speed property to 0. To resume the cycle, set it to a positive value.

Sunday, 5 February 2012

Jobs for .Net Freshers in Hyderabad.


Company        Magna Infotech Pvt.Ltd
Website          www.magna.in
Eligibility        B.E/B.Tech, MCA

Experience     2-4 Years

Location         Hyderabad
Magna Infotech Pvt.Ltd
Job Role: Software Engineer/ Programmer - .Net Developer With 2-4 years Exp
Job Summary:Ability to come out with good technical solutions

Ability to troubleshoot critical technology issues

Good knowledge of App server, Web server, DB server tuning

Knowledge of at least one Code profiling tools

Ability to guide team on technology


C# 2008 and the NET 3.5 Platform

Pro C# 2008 and the .NET 3.5 Platform, Fourth Edition


This book has existed (in one form or another) since the first edition of C# and the .NET Platform
was published in conjunction with the release of .NET 1.0 Beta 2, circa the summer of 2001. Since
that point, I have been extremely happy and grateful to see that this text was very well received by
the press and, most important, by readers. Over the years it was nominated as a Jolt Award finalist
(I lost . . . crap!) and for the 2003 Referenceware Excellence Award in the programming book category
(I won? Cool!).

Since that point, I have worked to keep the book current with each release of the .NET platform,
including a limited printing of a Special Edition, which introduced the technologies of .NET 3.0
(Windows Presentation Foundation, Windows Communication Foundation, and Windows Workflow
Foundation) as well as offered previews of several forthcoming technologies, which we now know as
LINQ.

The fourth edition of this text, which you hold in your hands, is a massive retelling of the previous
manuscript to account for all of the major changes that are found within .NET 3.5. Not only will
you find numerous brand-new chapters, you will find many of the previous chapters have been
expanded in great detail.

As with the earlier editions, this edition presents the C# programming language and .NET base
class libraries using a friendly and approachable tone. I have never understood the need some
technical authors have to spit out prose that reads more like a GRE vocabulary study guide than a
readable book. As well, this new edition remains focused on providing you with the information you
need to build software solutions today, rather than spending too much time examining esoteric
details that few individuals will ever actually care about.


Download this book here.

Fibonacci Numbers


Program to print Fibonacci Numbers below 100.

using System;
class myclass
{
    static void Main()
    {
        int fn = 0;
        int sn = 1;
        int tn = 1;

        Console.WriteLine(fn);
        Console.WriteLine(sn);
        while (true)
        {

            tn = fn + sn;
            if (tn >= 100)
            {
                break;
            }
            Console.WriteLine(tn);
            fn = sn;
            sn = tn;

        }
        Console.Read();

    }
}

Output

0
1
1
2
3
5
8
13
21
34
55
89

Random Password Genrater


This Program takes input a string and generate random passwords using that string. 

using System;

namespace randomPassword
{
   
    class Program
    {
       
        static void Main(string[] args)
        {
            // Gets the length  of the password.
            int len;

            // Gets the type of the password
            int sel;

            // Gets a random value.
            int rand;

            // The string that contains the password.
            string pass = "";

            // Random number.
            Random num = new Random();

            // Get the password length (user input).
            Console.WriteLine("Password length:");
            len = Convert.ToInt32(Console.ReadLine());

            // Get the password type (user input).
            Console.WriteLine("Select password type:");
            Console.WriteLine("1 - Letters, Symbols, Numbers");
            Console.WriteLine("2 - Letters, Symbols");
            Console.WriteLine("3 - Letters, Numbers");
            Console.WriteLine("4 - Letters");
            Console.WriteLine("5 - Numbers");
            sel = Convert.ToInt32(Console.ReadLine());
           
            // Based on the password type, generate a password.
            switch (sel)
            {
                    // Complex password (letters, numbers, symbols).
                case 1:
                    for (int c1 = 0; c1 < len; c1++)
                        pass += char.ConvertFromUtf32(num.Next(33, 126));
                    break;

                    // Password composed of letters and symbols.
                case 2:
                    for (int c1 = 0; c1 < len; c1++)
                    {
                    genNew:rand = num.Next(33, 126);
                        if ((rand < 48) || (rand > 57))
                        {
                            pass += char.ConvertFromUtf32(rand);
                        }
                        else
                        {
                            goto genNew;
                        }
                    }
                    break;

                    // Password composed of letters and numbers.
                case 3:
                    for (int c1 = 0; c1 < len; c1++)
                    {
                    genNew2:
                        rand = num.Next(33, 126);
                        if (((rand > 47) && (rand < 58)) || ((rand < 122) && (rand > 96)) || ((rand < 89) && (rand > 64)))
                        {
                            pass += char.ConvertFromUtf32(rand);
                        }
                        else
                        {
                            goto genNew2;
                        }
                    }
                    break;

                    // Password composed of letters.
                case 4:
                    for (int c1 = 0; c1 < len; c1++)
                    {
                    genNew3:
                        rand = num.Next(33, 126);
                        if (((rand > 65) && (rand < 90)) || ((rand < 122) && (rand > 96)))
                        {
                            pass += char.ConvertFromUtf32(rand);
                        }
                        else
                        {
                            goto genNew3;
                        }
                    }
                    break;

                    // Password composed of numbers.
                case 5:
                    for (int c1 = 0; c1 < len; c1++)
                    {
                        pass += char.ConvertFromUtf32(num.Next(48, 57));
                    }
                    break;
            }

            // Show the generated password.
            Console.WriteLine("Your password is: {0}", pass);
            Console.ReadLine();

        }
    }
}

WinApp - Click Counter

This program is a simple Windows Form Application for counting the number of clicks done don on that form and as the user clicks the number counter will increase its value by 1.

Code for the program is as follows:-


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace clickcount
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int clickcount = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            clickcount++;
            label1.Text = "number of clicks: " + clickcount + ".";
        }
    }
}

Write a C-Sharp .NET console application program for finding factorial of the given number.

Write a C-Sharp .NET console application program for finding factorial of the given number.


Program source code for finding factorial of the given number in C#.NET.

using System;
using System.Collections.Generic;
using System.Text;

namespace factorial
{
class Program
{

static void Main(string[] args)
{
int i, fact = 1, n;
String no;
Console.WriteLine("Enter a number:");
no = Console.ReadLine();
n = Convert.ToInt32(no);
for (i = 1; i <= n; i++)
{
fact = fact * i;
}
Console.WriteLine("factorial is:" + fact);
Console.ReadLine();
}
}
}

Write a C-Sharp .NET Console program to generate the following number pattern.


Write a C-Sharp .NET Console program to generate the following number pattern.
 1
22
333
4444
55555

Program Code:-

public class  ForLoop{
  public static void main(String[] args){
    for(int i = 1;i <= 5;i++){
      for(int j = 1;j <= i;j++){
        System.out.print(i);
      }
      System.out.println();
    }
  }
}

Output of the program :

1
22
333
4444
55555