Wednesday, March 28, 2012

How To Use Delegates In C# Easy Tutorial

While trying to access TextBox, RichTextBox etc you might encounter a error like


This error indicate that you cannot use TextBox or RichTextBox from any other tread except the default one.

To Access TextBox in your own thread you will need to use delegates for that.

Below is a simple code, in which I made 2 threads and 2 buttons (You will need to add 2 buttons in your project manually)

* DelegateWrite (Which write in the TextBox)
* DelegateRead (Which read from the TextBox)

Download .exe of this project from here (you will need Framework 4.0 installed)

1:  using System;  
2:  using System.Collections.Generic;  
3:  using System.ComponentModel;  
4:  using System.Data;  
5:  using System.Drawing;  
6:  using System.Linq;  
7:  using System.Text;  
8:  using System.Windows.Forms;  
9:  using System.Threading;  
10:  namespace Delegates  
11:  {  
12:    public partial class Form1 : Form  
13:    {  
14:      public Form1()  
15:      {  
16:        InitializeComponent();  
17:      }  
18:      void DelegateWrite()  
19:      {  
20:        if (textBox1.InvokeRequired)  
21:        {  
22:          textBox1.Invoke(new MethodInvoker(DelegateWrite));  
23:        }  
24:        else  
25:          textBox1.Text="String Generated from Delegate";  
26:      }  
27:      void DelegateRead()  
28:      {  
29:        if (textBox1.InvokeRequired)  
30:        {  
31:          textBox1.Invoke(new MethodInvoker(DelegateRead));  
32:        }  
33:        else  
34:          MessageBox.Show(textBox1.Text);  
35:      }  
36:      private void button1_Click(object sender, EventArgs e)  
37:      {  
38:        Thread objthread = new Thread(new ThreadStart(DelegateWrite));  
39:        objthread.Start();  
40:      }  
41:      private void button2_Click(object sender, EventArgs e)  
42:      {  
43:        Thread objthread = new Thread(new ThreadStart(DelegateRead));  
44:        objthread.Start();  
45:      }  
46:    }  
47:  }  

Friday, March 23, 2012

HOW TO CREATE BOOTABLE ISO OF WINDOWS 7 100% WORKING

Hello everyone, today I am gonna teach you how you can make a bootable ISO of windows 7,2008 R2 Server without any software like nero,magic ISO, alcohol etc. The procedure is quite simple,

For this you will require :

- Bootable CD of windows 7
- cdimage Download from here
(file is password protected, password is taqi)

1. Make a folder in C drive name it win7

2. Copy all the file and folder from windows 7 CD in win7 folder in c drive i.e. c:\win7\ (MAKE SURE YOU DO THIS RIGHT)

2. Download the cdimage from the link provided above

3. Extract the cdimage.exe in root portion of C drive i.e. c:\cdimage.exe

4. Now open up your command prompt from run by typing cmd

5. In command prompt goto C drive and press enter

6. Now copy the command listed below as it is, and press enter

cdimage.exe -lWIN_EN_DVD -m -u2 -bc:\win7\boot\etfsboot.com c:\win7\ c:\win7.iso"


cdimage will start making the bootable iso file in your c drive as "win7.iso", this method is tested and found 100% working

If you have any problem, than please comment here

Thanks

Wednesday, March 21, 2012

Compare two textboxes in C#

Comparing TextBoxes is quite easy in c#, below is a simple code that compare two textbox

:  namespace Comparing  
2:  {  
3:    public partial class Form1 : Form  
4:    {  
5:      public Form1()  
6:      {  
7:        InitializeComponent();  
8:      }  
9:      private void button1_Click(object sender, EventArgs e)  
10:      {  
11:        if (textBox1.Text == textBox2.Text)  
12:          MessageBox.Show("String matched");  
13:        else  
14:          MessageBox.Show("String does not match","Result");  
15:      }      
16:    }  
17:  }  

I added 1 Button and 2 TextBoxes, when we press the button the program will tell us weather the the string in TextBox matches or not.

you will need to add two textbox and a button in your project for the above code to work

Download project .exe from here

Wednesday, March 14, 2012

CREATE A TEXT FILE AND WRITE IN IT C# EXAMPLE

In C# creating and writing on a Text file is really easy as compared to C, C++. Below is the code written is Visual Studio 2010 which creates a file called "abc.txt" in D drive, and than write some string in it finally we save that string in the file.

You will need to add a System.IO namespace on the top of your program in order to successfully run this code

1:   StreamWriter objstreamwriter = new StreamWriter(@"D:\abc.txt", true); //specify path for the file, true indicate weather to overwrite the file if it already exist  
2:   objstreamwriter.WriteLine("Write this string in the Text file located at D drive");  
3:    objstreamwriter.Flush();  


Points to note:
  • StreamWriter is the class which we are using to create File
  • objstreamwriter is the object of class StreamWriter
  • WriteLine is the method of StreamWriter class which write text and at the end automatically escape a line i.e. Enter
  • Flush() is a method that we used to save the text in the file
If you have any queries please post comment.

Thanks