C# Codes
C# Codes
http://msdn.microsoft.com/express/vcsharp
http://www.developer.com/net/cplus/article.php/627891
http://www.codeproject.com/cs/system/cdrom.asp?
df=100&forumid=208854&exp=0&select=1427141
http://forums.microsoft.com/msdn/showforum.aspx?forumid=31&siteid=1
http://www.samspublishing.com/library/content.asp?
b=STY_Csharp_24hours&seqNum=169&rl=1
http://msdn.microsoft.com/vstudio/express/visualcsharp/learning/default.aspx
http://www.devasp.net/net/articles/display/679.html
textBox1.Text = "write text"; //FOR SHOWING THE TEXT IN THE TEXT BOX BY CLICKING
THE BUTTON!
mediaSoundPlayer.Stream = Properties.Resources.NameOfYourMusic;
mediaSoundPlayer.PlayLooping(); //FOR START PLAYING ANY
MUSIC WITH REPEATING!
mediaSoundPlayer.Stream = Properties.Resources.NameOfYourMusic;
mediaSoundPlayer.Play(); //FOR START PLAYING ANY
MUSIC WITHOUT REPEATING!
/*
* Name: HowdyPartner.cs
* Autor: Joe Mayo
*/
using System;
class STARS
{
static void Main()
{
string name;
name = Console.ReadLine();
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string STARS;
Console.WriteLine("Yo man!!!");
STARS = Console.ReadLine();
}
}
} //FOR SHOWING THE TEXT YOU WROTE IN THE
CONSOLE!(The console
won't turn off until you don't do it).
[This code shows the same thing like the
previous code
but on a little more simple and better way].
NOTE: If you don't want the console turns off immediately after you start debuging,you
must of course write this:
Console.WriteLine("write the text you want");
And above that you must also write this:
string STARS;
Also don't forget the all this writing must go between this 2 brackets:---> {
:---> }
AFTER YOU HAVE DONE THIS,YOU SHOULD GET THIS:
{
string STARS;
Console.WriteLine("write the text you want");
STARS = Console.ReadLine();
}
NOTE: The word doesn't need to be "STARS".It can also be any other word you want.For
example MARK,book,Computer...blabla
But then that word must be written on that two places.I will take for example word
"Computer" :
{
string Computer;
Console.WriteLine("write the text you want");
Computer = Console.ReadLine();
}
string test;
test = "Hello all!!!";
MessageBox.Show(test);
MessageBox.Show("What's up?!!"); //CODE FOR SHOWING THE TWO TEXT
BOXES WITH DIFFERENT TEXTS YOU WROTE!
(Note that the word "test" is written on 3
places.Without that word,
the program won't work.The word doesn't need to be
"test".It can be
also any other word you want,but be sure the that
word is written on
all this 3 places where the word "test" is written).
float firstTextBox = 0;
float secondTextBox = 0;
float result = 0;
firstTextBox = float.Parse(textBox1.Text);
secondTextBox = float.Parse(textBox2.Text);
result = firstTextBox + secondTextBox;
label1.Text = result.ToString(); //WITH THIS CODE YOU CAN COUNT ANY
FULL OR DECIMAL NUMBERS!(If instead
word "float" you put the word "int" ,you won't be able
to count with
decimal numbers.Only with full numbers).
if (button1.Dock == DockStyle.Fill)
{
button1.Dock = DockStyle.None;
}
else
{
button1.Dock = DockStyle.Fill;
} //FOR STRETCHING THE BUTTON BY CLICKING
ON IT!(The button will
become bigger after you click on it.For this
option,table
layout panel is needed).
if (fontDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
richTextBox1.SelectionFont = fontDialog1.Font; //WITH THIS
CODE,AFTER YOU CLICK ON THE
BUTTON,THE FONT WINDOW WILL
APPEAR!(For this
option,rich text box and font dialog is
needed).
if (colorDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
richTextBox1.SelectionColor = colorDialog1.Color; //WITH THIS
CODE,AFTER YOU CLICK ON THE BUTTON
THE COLOR WINDOW WILL APPEAR!
(For this option,
rich text box and color dialog is
needed).
pageSetupDialog1.Document = printDocument1;
pageSetupDialog1.PageSettings = printDocument1.DefaultPageSettings;
pageSetupDialog1.AllowMargins = true;
pageSetupDialog1.AllowOrientation = true;
pageSetupDialog1.AllowPaper = true;
pageSetupDialog1.AllowPrinter = true;
pageSetupDialog1.ShowNetwork = true;
pageSetupDialog1.ShowHelp = true;
pageSetupDialog1.EnableMetric = true;
if (pageSetupDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
printDocument1.DefaultPageSettings = pageSetupDialog1.PageSettings; //FOR
OPENING THE PAGE SETUP!(For this
option,page setup dialog and print
document is needed).
printDialog1.Document = printDocument1;
if (printDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
printDocument1.Print();
} //FOR PRINT!(For this option,print
dialog and
print document is needed).
saveFileDialog1.DefaultExt = ".rtf";
saveFileDialog1.OverwritePrompt = true;
saveFileDialog1.Title = "Save File";
if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);
} //FOR SAVING THE TEXT
FILES ON
ANY PLACE ON YOUR
COMPUTER!
(For this option,save file
dialog and rich text box is
needed).
NOTE:The message box which will say us the loading text has failed is made on this
way.Above this:
{
richTextBox1.LoadFile(openFileDialog1.FileName);
}
switch (comboBox1.Text)
{
case "BottomUp":
flowLayoutPanel1.FlowDirection = FlowDirection.BottomUp;
break;
case "LeftToRight":
flowLayoutPanel1.FlowDirection = FlowDirection.LeftToRight;
break;
case "TopDown":
flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
break;
case "RightToLeft":
flowLayoutPanel1.FlowDirection = FlowDirection.RightToLeft;
break;
} //THIS CODE IS FOR 4 DIFFERENT
SEQUENCES FOR THE BUTTONS
WHICH YOU ADDED THANKS TO THE
PREVIOUS CODE!(With this
code you can put the buttons,[which you
added],on 4
different sequences.And that are:bottom
up,left to
right,top down and right to left.For this
option,
combo box and flow layout panel is
needed).
if (checkBox1.Checked == true)
flowLayoutPanel1.WrapContents = true;
else
flowLayoutPanel1.WrapContents = false; //WITH THIS CODE,ALL
BUTTONS YOU ADDED ON THE FORM,THEY
WILL ALL BE SHOWED ON THE FORM,IF
THERE IS ENOUGH PLACE
FOR THEM IN FLOW LAYOUT PANEL!(That will
happen if you
put the mark in that check box.For this
option,check box
and flow layout panel is needed).
if (checkBox2.Checked == true)
flowLayoutPanel1.AutoScroll = true;
else
flowLayoutPanel1.AutoScroll = false; //WITH THIS CODE,ALL BUTTONS
YOU ADDED ON THE FORM,THEY WILL
ALL BE SHOWED ON THE FORM,BUT YOU WILL
SEE THEM ALL THANKS
TO THE ARROWS WHICH ARE AUTOMATICALLY
CREATED IF YOU CREATED
MORE BUTTONS THEN THERE IS A SPACE ON
FLOW LAYOUT PANEL!(For
this option,check box and flow layout panel is
needed).
NOTE: For more options in our icon,we must add the context menu strip on the form and
then load that context menu strip in
the notify icon.And the place in notify icon where we want to load the context menu
strip is called
"ContextMenuStrip" !!!And then in that context menu strip we can put many buttons
and write the code for them.I am
usually using 2 buttons and that are SHOW method and HIDE method.Also if we want
the form be showed by clicking on
the icon 2 times with our left mouse click,we must do that in the events of that notify
icon!
process1.EnableRaisingEvents = true;
process1.Start(); //FOR OPENING THE NOTEPAD BY CLICKNG ON THE
BUTTON!(For this option,process
is needed.Also this code won't work if you do not do the
following.Choose
process then go in his "Properties",then go in the
"StartInfo" then in the
"FileName" write "notepad.exe" and in the
"WorkingDirectory" write
"%system%\system32".But if you want to use something
else instead the
notepad,in the "FileName" just simply write
"wordpad.exe").
NOTE: If you want the message box,(with the text you want),be showed if you click the
closing button or X/close button on
the notepad,bellow this:
{
process1.Kill();
}
Write this:
private void notepadProcess_Exited(object sender, EventArgs e)
{
MessageBox.Show("NOTEPAD CLOSED!", "Notepad closed", MessageBoxButtons.OK);
}
And after you have done all this,again choose the process and go in his "Properties",then go
in the events then choose the
"Exited".There write or even better choose "notepadProcess_Exited".
string programFilesPath =
System.Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
string iePath = System.IO.Path.Combine(programFilesPath, @"Internet
Explorer\IExplore.exe");
process2.EnableRaisingEvents = true;
process2.StartInfo.FileName = iePath;
process2.Start(); //FOR OPENING THE INTERNET EXPLORER
BY CLICKING ON THE BUTTON!
(For this option,process is needed.This time,do
not change
anything in the "Properties" of this process if
you are
using this code).
NOTE:If this time you again you want the there is a message box which will say something
after you clicked the close button
to close the internet explorer or if you clicked the X/close on the internet explorer,bellow
this:
{
process2.Kill();
}
Write this:
private void internetExplorerProcess_Exited(object sender, EventArgs e)
{
MessageBox.Show("INTERNET EXPLORER CLOSED!", "Internet Explorer closed",
MessageBoxButtons.OK);
}
And after you have done all this,do the same thing like before on the previous
example.Choose the process and go in his
"Properties",then go in the events then choose the "Exited".There write or even better
choose "internetExplorerProcess_Exited".
string MyName;
MyName = "Freeman";
MessageBox.Show(MyName); //STRING THAT SHOWS THE WORD
"Freeman"!
string car;
string bike;
car = "Hello man!!";
bike = "What's up?!";
MessageBox.Show(car + bike); //TWO STRINGS THAT SHOWS "Hello man!!What's
up?!"!
NOTE: This code won't work if above "public Form1()" you don't write this:
string tadaPath = "";
int count = 0;
Bitmap b = (Bitmap)pictureBox1.Image;
SaveFileDialog save = new SaveFileDialog();
save.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (save.ShowDialog() == DialogResult.OK)
{
b.Save(save.FileName);
} //WITH THIS YOU CAN SAVE THE
PICTURE ON ANY PLACE
ON YOUR COMPUTER!(For this
option,picture box is
needed).
Bitmap b = (Bitmap)pictureBox1.Image;
SaveFileDialog save = new SaveFileDialog();
save.Filter = "BMP (*.bmp)|*.bmp|JPEG (*.jpeg)|*.jpeg|JPG (*.jpg)|*.jpg|GIF (*.gif)|*.gif|
PNG (*.png)|*.png|TIFF (*.tiff)|*.tiff|DIB (*.dib)|*.dib|JPE (*.jpe)|*.jpe|JFIF (*.jfif)|*.jfif";
if (save.ShowDialog() == DialogResult.OK)
{
b.Save(save.FileName);
} //WITH THIS CODE YOU CAN ALSO SAVE
THE PICTURE ON ANY
PLACE ON YOUR COMPUTER,BUT WITH
THIS CODE YOU CAN
SAVE THE PICTURE IN ANY FORMAT YOU
WANT!(For this
option,picture box is needed).
string laptop;
string computer;
string word;
string car;
laptop = "Hello.";
computer = "How are you today? ";
word = "I am fine...";
car = "and you?";
MessageBox.Show(laptop + computer + word + car + "","Text is bellow"); //FOUR
STRINGS THAT SHOWS THE NAME OF THE MESSAGE
BOX "Text is bellow" AND THE TEXT IN
THAT
MESSAGE BOX "Hello.How are you
today? I am
fine...and you?"!
tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset;
tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
if (comboBox1.Text == "Emil")
{
MessageBox.Show("You chose Emil!");
comboBox1.Text = "";
} //THIS IS AN EXAMPLE FOR THE COMBO BOX!(Here I
edited the items by
putting two words.And that are "Marin" and
"Emil".Only if you
choose "Emil" then click the button,only for "Emil" will
work
because we didn't write a code for "Marin".So when
you choose
"Emil" and click the button,there will be a message
box which will
contain text "You chose Emil!".For this option,combo
box is needed).
if (comboBox1.Text == "Marin")
MessageBox.Show("Hehehe lol"); //THIS IS ALSO AN EXAMPLE FOR THE
COMBO BOX!(After you choose "Marin"
and then click the button,the message box will be
showed which will
contain text "Hehehe lol".Also note the this and the
previous code
must be written in that button which we are using for
this option,
not in the combo box.For this option,combo box is
needed).
if (comboBox1.Text != "Conrad")
{
if (comboBox1.Text == "Grant")
{
MessageBox.Show("YOU PICKED GRANT!!!");
}
else
{
MessageBox.Show("You didn't choose an option!");
}
}
else
{
MessageBox.Show("CONRAD IS PICKED!!!");
} //THIS IS A HARDER EXAMPLE FOR THE COMBO
BOX!(In here I edited 2
items.And that are "Conrad" and "Grant".In the
first row I
used the "if" statement for the false value for the
"Conrad".
So that value won't be executed because it's
false.After that
I again used the "if" statement for the true value
for the
"Grant".Then of course I wrote the action which I
want to
happen for the "Grant" and in this case it's the
message box.
After that I used the "else" statement if the user
didn't
choose any option,so there will be a message box
which will
tell to the user he didn't choose anything.After all
this I
again used the "else" statement for the
"Conrad".So if the
"Conrad" is chosen,there will be a message box
which will say
that.For this option,combo box is needed).
if (comboBox1.Text != "Marin")
{
if (comboBox1.Text == "Emil")
{
MessageBox.Show("TEST NUMBER 2!!!");
}
if (comboBox1.Text == "Vesna")
{
MessageBox.Show("TEST NUMBER 3!!!");
}
}
else
{
MessageBox.Show("TEST NUMBER 1!!!");
} //ANOTHER EXAMPLE FOR THE COMBO BOX!
switch (listBox1.SelectedItem.ToString())
{
case "Marin":
MessageBox.Show("LOL");
break;
} //EXAMPLE FOR THE LIST BOX!(Here in the list box I
added 4 words.
And that are "Marin","Emil","Vesna" and "Ivan".And
after in the
list box you choose "Marin" and click the button,there
will be a
message box which will say "LOL".But if you choose any
other words of
those 3,nothing won't happen because we didn't write a
code for them.
For this option,list box is needed).
switch (listBox1.SelectedItem.ToString())
{
case "Marin":
MessageBox.Show("LOL");
break;
default:
MessageBox.Show("This one is not working");
break;
} //ANOTHER EXAMPLE FOR THE LIST BOX!(In this
example I didn't
add anything new.Just bellow the prevoius code
I also wrote
the code for "default" and with that,if you
choose something
in the list box and it's not written here in the
code,the
message box will appear with the text "This one
is not
working".For this option,list box is needed).
int i = 0;
while (i < int.Parse(textBox1.Text))
{
i++;
}
MessageBox.Show("The final value is: " + i.ToString()); //IF YOU FOR EXAMPLE
IN THE TEXT BOX WRITE NUMBER
6 AND THEN PRESS THE BUTTON,THE
MESSAGE BOX WILL
BE SHOWED AND THE TEXT WILL BE
"The final value
is: 6"!(If that "i" is number 0 and it's
set to
"++" then in the text box you must
write number
0 or any higher number.After that,by
clicking on
the button there will be a message box
which
will say the value.But if you write the "i"
is
for example number 9,then in the text
box you
must write number 9 or any higher
number because
"i" is set the he is less then the value
which
we are writing in the text box.So if you
write
number 4 and the "i" is 9,message box
will say
the value is 9.Also for example if you
set
"int i = -9;" so then "i" is number
-9,and
if he is set the he is less then the value
in
the text box then in the text box you
must write
number -9 or higher.For example
number -4 or
number 2.For this option,text box is
needed).
NOTE: If in the list box you want to choose more then just one item,in the "Properties" of
that list box,in the
"SelectionMode" you must put "MultiSimple" instead "One".
if (comboBox1.Text == "")
{
MessageBox.Show("CHOOSE THE OPTION!!!");
}
{
if (comboBox1.Text == "Test (1)")
{
MessageBox.Show("TEST 1 IS GOOD!");
}
if (comboBox1.Text == "Test (2)")
{
MessageBox.Show("TEST 2 IS GOOD!");
}
if (comboBox1.Text == "NOT FINISHED")
{
MessageBox.Show("THIS OPTION ISN'T FINISHED!!!");
}
} //THIS IS A GOOD EXAMPLE FOR THE COMBO
BOX!(In this example,if
the user does not choose anything in the combo
box,there will
be a message box which will tell that to him,if of
course the
user clicks the button.For this option,combo box
is needed).
First myFirst;
myFirst = new First();
myFirst.Make = "Oldsmobile";
myFirst.Model = "Cutlas Supreme";
myFirst.ElapsedMileage = 200000;
MessageBox.Show(myFirst.Make); //BY CLICKING ON THE BUTTON,YOU
WILL GET THE MESSAGE BOX WITH THE
TEXT "Oldsmobile"!(One example).[If you
understand the strings,
then you should understand this].
NOTE:This two examples won't work if you don't add new class to your project.Here in my
project I added a new class and I
called it "First.cs".Then your class name in the code view should look like this:
using System;
using System.Collections.Generic;
using System.Text;
namespace WindowsApplication1
{
class First
{ <---
} <---
}
And if you want the one of those two examples works,between that two brackets which I
marked with arrows,you must write
this:
//Examples of fields
private string _make;
private string _model;
private int _elapsedMileage;
//Examples of properties
public string Make
{
get { return _make; }
set { _make = value; }
}
public string Model
{
get { return _model; }
set { _model = value; }
}
public int ElapsedMileage
{
get { return _elapsedMileage; }
set { _elapsedMileage = value; }
}
NOTE:This code of course won't work if you don't do the same thing like in the previous
example.So after you added a new
class and called it "First.cs",in him between that two brackets of course this must be
written:
//Examples of fields
private string _make;
private string _model;
private int _elapsedMileage;
//Examples of properties
public string Make
{
get { return _make; }
set { _make = value; }
}
public string Model
{
get { return _model; }
set { _model = value; }
}
public int ElapsedMileage
{
get { return _elapsedMileage; }
set { _elapsedMileage = value; }
}
api.mciSendString("set CDAudio door open", null, 127, 0); //AFTER YOU CLICK ON THE
BUTTON,THE CD-ROM TRAY WILL OPEN!
api.mciSendString("set CDAudio door closed", null, 127, 0); //AFTER YOU CLICK ON
THE BUTTON,THE CD-ROM TRAY WILL
CLOSE!
NOTE: That two codes won't work if you do not do the following.On the beginning of the
whole code of your project you should
have something like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
Or this:
api.mciSendString("set CDAudio door closed", null, 127, 0);
Write this:
public class api
{
// this api used to send string messege to media control interface decice (mci)
// like The cd rom
[DllImport("winmm.dll", EntryPoint = "mciSendStringA")]
public static extern int mciSendString(string lpstrCommand, string
lpstrReturnString, int uReturnLength, int hwndCallback);
// this api used to get information about a drive ex: its name, seial number
// if this function return zero means that one of the information could not be
retrived
// so if it is a CD ROM drive and we can't obtain its name ---> CD ROM is empty
[DllImport("kernel32.dll", EntryPoint = "GetVolumeInformationA")]
public static extern int GetVolumeInformation(string lpRootPathName, StringBuilder
lpVolumeNameBuffer, int nVolumeNameSize, int lpVolumeSerialNumber, int
lpMaximumComponentLength, int lpFileSystemFlags, string lpFileSystemNameBuffer, int
nFileSystemNameSize);
//this api get the drive type (0:unknown,1:invalid
path,2:removable(floppy,removabledisk),3:fixed(hard disk),
//4:remote(network drive),5:CDROM,6: RAM disk)
[DllImport("kernel32.dll", EntryPoint = "GetDriveTypeA")]
public static extern int GetDriveType(string nDrive);
}
Then in the form code view,(the place where you will get by clicking two times on the
form),write this:
string s ="";
StringBuilder volumeName = new StringBuilder(256);
int srNum = new int();
int comLen = new int();
string sysName = "";
int sysFlags = new int();
int result;
string [] logDrives = System.IO.Directory.GetLogicalDrives();
for (int i = 0; i < logDrives.Length; i++)
{
if (api.GetDriveType(logDrives[i]) == 5)
{
s += "Your CD ROM is on drive : " + logDrives[i].ToString() + "\n";
result = api.GetVolumeInformation(logDrives[i].ToString(), volumeName,
256, srNum, comLen, sysFlags, sysName, 256);
if (result == 0)
s += "there is NO CD in ur CD ROM";
else
{
s += "There is a CD inside ur CD ROM and its name is " + volumeName;
}
}
}
listView1.LargeImageList = null;
listView1.Items.Add("First Item", 0);
listView1.Items.Add("Second Item", 1);
listView1.Items.Add("Third Item", 2);
listView1.Items.Add("Fourth Item", 3);
listView1.Items.Add("Fifth Item", 4);
listView1.Items.Add("Sixth Item", 5);
listView1.Items.Add("Seventh Item", 6);
listView1.Items.Add("Eighth Item", 7);
listView1.Items.Add("Nineth Item", 8);
ListViewItem li = new ListViewItem("Tenth Item", 9);
li.Checked = true;
li.ForeColor = Color.Blue;
listView1.Items.Add(li); //FOR ADDING YOUR OWN ITEMS ON THE LIST
VIEW!(Here I added 10
items.Tenth item will have blue letters because I
wrote a little
different code for that tenth item then for the first
nine items.
For first nine items I just wrote one line of code and
for the
tenth item I needed to write more lines of code if I
wanted to
put his letters in the different color.This code must
be written
on the place where you get by clicking two times on
the form.I am
also calling that form code view.It means the
same.In this code
we won't have any pictures above our text which are
that items we
added on the list view.Why?Because in the first line
of code on
the end we wrote the word "null".If we want to have
a picture
above our item,we will need to put image list on our
form.Then in
that image list we will go in his properties and in
"Images" we
will add our picture which we want to be displayed
above our item.
After that,in the first line of code instead the word
"null" we
will write "imageList1".That "imageList1" is of course
the default
name of that image list.The number of the first
picture which we
added in the image list is number 0.So in this
example that picture
will be displayed above the first item.If for example
we want to
display that picture only above our fiveth item,that
number 0 of
that picture must be written in the sixth line of
code.Of course
number 4 must be deleted.And in the second line of
code instead the
number 0 we must write that number 4.For this
option,list view and
image list is needed).
listView1.View = View.Tile; //ITEMS WHICH YOU ADDED ON THE LIST VIEW WILL BE
IN THE TILE VIEW BY CLICKING ON THE
BUTTON!(For this option,list view is needed).
listView1.View = View.List; //ITEMS WHICH YOU ADDED ON THE LIST VIEW WILL BE
IN THE LIST VIEW BY CLICKING ON THE
BUTTON!(For this option,list view is needed).
listView1.BeginUpdate();
listView1.Columns.Add("Name", 200, HorizontalAlignment.Left);
listView1.BeginUpdate();
listView1.LargeImageList = imageList1;
listView1.Items.Add("First Item", 0);
listView1.Items.Add("Second Item", 1);
listView1.Items.Add("Third Item", 2); //WHEN THE USER CLICKS THE
BUTTON,THIS 3 ITEMS TOGETHER WITH PICTURES
WILL BE SHOWED ON THE LIST VIEW!(Here I added
3 items and 3 pictures
for each of them.So when you click the button,this
will be showed.If
I for example write this in the form code view,when
you turn ON the
application,all this 3 items will be immediately showed
without
clicking the button.For this option,list view and image
list is
needed).
listView1.Clear(); //BY CLICKING ON THE BUTTON,ALL ITEMS IN THE LIST VIEW WILL
BE REMOVED!(For this option,list view
is needed).
NOTE:This code won't work if above "public Form1()" you don't write this:
string tadaPath = null;
listView1.BeginUpdate();
listView1.LabelEdit=true;
listView1.AllowColumnReorder=true;
listView1.EndUpdate(); //WITH THIS EXAMPLE AFTER YOU CLICK
ON THE BUTTON,5 COLUMNS AND
2 ROWS WILL BE SHOWED,BUT THIS TIME YOU
WILL ALSO BE ABLE TO
EDIT THE NAME IN THAT 2 ROWS AND ALSO YOU
WILL BE ABLE TO
MOVE THE COLUMNS ON OTHER PLACE!(For this
option,list view and
context menu strip is needed).
NOTE:Editing the names in that two rows will not work if in the list view you don't add the
context menu strip.After
you added the context menu strip in that list view,there create any button,(for example
"Edit"),and for that button
write this code:
ListViewItem li0;
li0=listView1.FocusedItem;
li0.BeginEdit();
if(trackBar1.Value==1)
{
MessageBox.Show("ONE IS CORRECT!");
}
if(trackBar1.Value==2)
{
MessageBox.Show("TWO IS CORRECT!");
} //AFTER YOU CLICK THE BUTTON,YOU CAN GET TWO
MESSAGE BOXES!ONE WILL
SAY "ONE IS CORRECT!" AND THE OTHER ONE WILL
SAY "TWO IS
CORRECT!"!(This is made with track bar.And in it I
added 2 values.
Value "1" and value "2".I can add them as many as
I want in the
properties of that track bar,in row which says
"Maximum".So in
this case I have 3 options free in my track bar
because in the
"Maximum" I wrote "2" and in the "Minimum" I
wrote "0".But of
course you can change that so you can have more
then even 15 free
options in that track bar.So in this example you will
have 3 free
options.If the bar is on the first line and you click
the button,
nothing won't happen.But if you scroll the bar on the
second line
and click the button,you will get a message box with
the text
"ONE IS CORRECT!".And if you scroll the bar on the
third line and
click the button,you will get a message box with the
text "TWO IS
CORRECT!".For second and third line is working
because that second
line is the value "1" and the third line is the value
"2".And we
wrote the code only for that two values.Value "1"
and value "2".
And the value of the first line is of course "0".It
always starts
from zero/0.If we want to make any function for the
first line
also,we will just write the code for it one the same
way like we
wrote it for the second line and third line.For this
option,track
bar is needed).
webBrowser1.Navigate(textBox1.Text); //AFTER YOU WRITE THE LINK IN THE TEXT
BOX AND THEN CLICK THE BUTTON,
THAT PAGE WILL BE OPENED IN THE WEB BROWSER!
(You can also use something
totally else for writing the page links.For example you
can use the rich
text box also.It doesn't need to be the text box.For this
option,web
browser and text box is needed).
NOTE: While turning on the page in the web browser,you also want the there is the
progress bar that shows that page loading,
you must do the following.Take the status strip and put it in the designer surface and in
it add the progress bar.
Then go in the events of the web browser and in the "ProgressChanged" write this code:
toolStripProgressBar1.Maximum=(int)e.MaximumProgress;
toolStripProgressBar1.Value=(int)e.CurrentProgress;
webBrowser1.DocumentText="<html><body><h1>Hello world!</h1><button
id='button1'>Fire an Event</button><button onclick='window.external.sayHello()'>Call a
Method</button></body></html>"; //AFTER YOU CLICK THE BUTTON,
IN THE WEB BROWSER THERE IS
GOING TO BE THE TEXT "Hello
world!"!(There are also going
to be 2 buttons."Fire an
Event" and "Call a Method"
button.First button will
show the message box with
the text "Hello from event
handler!" and the second
button will show the message
box with the text "Hello
from script!".For this option,
web browser is needed).[This
is one line of code.Here it's
in two lines because I didn't
have enough space to write the
whole code in one row].
NOTE:This long one line of code won't work if you don't do the following.Bellow this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
Write this:
using System.Runtime.InteropServices;
Write this:
[ComVisible(true)]
public class ScriptMethods
{
public void sayHello()
{
MessageBox.Show("Hello from script!");
}
}
Then in the events of the web browser,in the "DocumentCompleted" write this:
HtmlElement elem=webBrowser1.Document.GetElementById("button1");
if(elem!=null)
elem.Click+=new HtmlElementEventHandler(elem_Click);
And then somewhere at the end of whole code of your project,(or somewhere else),write
this:
void elem_Click(object sender,HtmlElementEventArgs e)
{
MessageBox.Show("Hello from event handler!");
}
if(e.KeyChar=='\r')
webBrowser1.Navigate(textBox1.Text); //AFTER YOU WRITE THE
PAGE LINK IN THE TEXT BOX,THE PAGE
WILL BE OPENED EVEN IF YOU CLICK THE
ENTER BUTTON ON
YOUR KEYBOARD!IT WON'T BE NECCESSARY
TO CLICK THE BUTTON
FOR NAVIGATING THAT PAGE!(This code
must be written in
the events of that text box,in the
"KeyPress".For this
option,web browser and text box is needed).
this.WindowState=FormWindowState.Minimized;
e.Cancel=true; //IF THE USER CLICKS THE 'X' BUTTON ON THE
FORM,THE FORM WON'T CLOSE,IT WILL
MINIMIZE IN THE TASKBAR!(This code must be
written in the events of that
form,in the "FormClosing").[Also if in the
"FormClosing" you write only
the second line of this code,the 'X' button won't
work].
richTextBox1.Copy(); //FOR COPYING THE TEXT FROM THE RICH TEXT BOX!
(After you click on the button,the selected text from
the rich text box will be copied and you will be able to paste it
on any other text areas.For
example that text area can be wordpad,notepad and paint.For
this option,rich text box is needed).
richTextBox1.Cut(); //FOR CUTTING THE TEXT FROM THE RICH TEXT BOX!
(After you click on the button,the selected text from
the rich text box will be cutted and you will be able to paste it
on any other text areas.For
example that text area can be wordpad,notepad and paint.For
this option,rich text box is needed).
richTextBox1.Undo(); //WITH THIS CODE YOU CAN UNDO THE TEXT IN THE
RICH TEXT BOX!(For this option,rich text box is needed).
richTextBox1.Redo(); //WITH THIS CODE YOU CAN REDO THE TEXT IN THE
RICH TEXT BOX!(For this option,rich text box is needed).
richTextBox1.SelectAll(); //FOR SELECTING THE WHOLE TEXT IN THE RICH
TEXT BOX!(After clicking on the button,whole written text
in the rich text box will be selected and then you can copy or
cut that whole text and then paste
it in any other text area.For example that text area can be
wordpad,notepad and paint.For this
option,rich text box is needed).
string one=String.Empty;
one=richTextBox1.SelectedRtf;
MessageBox.Show(one,"Example text in RTF code..."); //AFTER CLICKING ON
THE BUTTON,THE SELECTED TEXT IN THE RICH TEXT BOX WILL BE
SHOWED AS RTF CODE IN THE MESSAGE
BOX!(For this option,rich text box is
needed).
string two=String.Empty;
two=richTextBox1.SelectedRtf;
richTextBox2.Text=two; //AFTER CLICKING ON THE BUTTON,THE
SELECTED TEXT IN THE FIRST RICH TEXT BOX WILL BE SHOWED
AS RTF CODE IN THE SECOND RICH TEXT BOX!(For
this option,2 rich text boxes are needed).
openFileDialog1.Title="Open file...";
openFileDialog1.Filter="Rich text file (*.rtf)|*.rtf|All files (*.*)|*.*";
openFileDialog1.FileName="";
openFileDialog1.FilterIndex=0;
openFileDialog1.InitialDirectory="My Documents";
openFileDialog1.CheckFileExists=true;
openFileDialog1.CheckPathExists=true;
if(openFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
richTextBox1.LoadFile(openFileDialog1.FileName);
documentTitle=openFileDialog1.FileName;
} //AFTER YOU CLICK ON THE
BUTTON,YOU WILL BE ABLE TO OPEN ANY
TEXT FILE IN THE RICH TEXT
BOX!(For this option,rich text
box and open file dialog is
needed).
NOTE: This code won't work if you do not do the following.Above or bellow this:
public Form1()
{
InitializeComponent();
}
Write this:
private string documentTitle;
saveFileDialog1.DefaultExt=".rtf";
saveFileDialog1.OverwritePrompt=true;
saveFileDialog1.Title="Save file...";
saveFileDialog1.Filter="Rich text file (*.rtf)|*.rtf";
if(saveFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog1.FileName,RichTextBoxStreamType.RichText);
documentTitle=saveFileDialog1.FileName;
} //AFTER YOU CLICK ON THE
BUTTON,YOU WILL BE ABLE TO SAVE THE
TEXT FILE ON ANY PLACE ON
YOUR COMPUTER!(For this option,rich
text box and save file dialog
is needed).
NOTE: This code won't work if you don't do the following.Above or bellow this:
public Form1()
{
InitializeComponent();
}
Write this:
private string documentTitle;
richTextBox1.Rtf="";
documentTitle=""; //AFTER CLICKING ON THE BUTTON,WHOLE
WRITTEN TEXT IN THE RICH TEXT BOX WILL BE DELETED!(This is usually
used when the user wants to start writing the NEW text
file.For this option,rich text box is needed).
NOTE: This code won't work if you don't do the following.Above or bellow this:
public Form1()
{
InitializeComponent();
}
Write this:
private string documentTitle;
e.Cancel=true;
webBrowser1.Navigate(webBrowser1.StatusText); //THIS CODE WILL
PREVENT OPENING LINKS AND OTHER STUFF ON SITES IN THE NEW MICROSOFT
INTERNET EXPLORER WINDOW!(When you
click on the link on some sites you can notice the
that link is mostly opened in the new
Microsoft Internet Explorer window.Thanks to
this two lines of code,links and other stuff
on sites will not be opened in the new
Microsoft Internet Explorer window.It will be
opened inside of your own web browser.
Still most of the times site will not be
displayed correctly.This code must be written
in the events of that web browser,in the
"NewWindow".For this option,web browser is
needed).
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
pictureBox1.Image=Image.FromFile(openFileDialog1.FileName);
} //AFTER CLICKING ON THE
BUTTON,YOU WILL BE ABLE TO OPEN ANY PICTURE IN THE
PICTURE BOX!(For this option,picture
box and open file dialog is needed).
if(openFileDialog1.ShowDialog()==DialogResult.Cancel)
{
MessageBox.Show("You didn't open an image!");
} //AFTER CLICKING ON THE
BUTTON,YOU WILL AGAIN OPEN THE SAME WINDOW JUST LIKE IN
THE PREVIOUS CODE,BUT THIS TIME
AFTER YOU CLICK THE "Cancel" BUTTON,YOU WILL
GET THE MESSAGE BOX THAT SAYS
"You didn't open an image!"!(For this option,
picture box and open file dialog is
needed).
Console.WriteLine("Example...");
Console.ReadLine(); //AFTER DEBUGGING,YOU WILL GET THE
CONSOLE WINDOW WITH THE TEXT "Example..."!(The console won't
turn off immediately after debugging because I
also wrote this second line of code).
Console.WriteLine(DateTime.Now.ToString());
Console.ReadLine(); //AFTER DEBUGGING,YOU WILL GET THE
CONSOLE WINDOW WHICH WILL DISPLAY THE CURRENT DATE AND TIME ON
YOUR COMPUTER!
int sum;
sum=5+20;
Console.WriteLine(sum);
Console.ReadLine(); //AFTER DEBUGGING,YOU WILL GET THE CONSOLE
WINDOW WITH THE RESULT "25"!(That result has been created by
additioning number "5" and number "20").
int sum;
int no1=5;
int no2=20;
sum=no1+no2;
Console.WriteLine(sum);
Console.ReadLine(); //IN THIS EXAMPLE WE WILL AGAIN GET THE RESULT "25"
IN THE CONSOLE WINDOW,BUT ON A LITTLE MORE COMPLICATED WAY!(Here I
used 2 variables.First variable is for number "5" and second
variable is for number "20").
button3.Visible=false;
button1.Visible=true;
label2.Visible=true;
textBox1.Visible=true;
button2.Visible=true;
label3.Visible=true;
Gen_label2(); //AFTER CLICKING ON THE BUTTON,BUTTON WILL
DISAPPEAR AND ALL THIS LINES OF CODE WILL BE EXECUTED!(The button will
disappear because that button we click is called "button3".And
thanks to the first line of code,that will happen.
In short...with this button we start playing the math game).
...write this:
private void Gen_label2()
{
no1=randomNo.Next(100);
no2=randomNo.Next(100);
if(radioButton1.Checked)
{
one="+";
}
else if(radioButton2.Checked)
{
one="-";
}
else if(radioButton3.Checked)
{
one="*";
}
label2.Text=no1+one+no2+"=";
}
int result=0;
if(one=="+")
{
result=no1+no2;
}
else if(one=="-")
{
result=no1-no2;
}
else if(one=="*")
{
result=no1*no2;
}
if(textBox1.Text==result.ToString())
{
label3.Text="Correct";
}
else
{
label3.Text="Incorrect";
} //AFTER CLICKING THE BUTTON,THE TEXT IN THE "label3"
WILL TELL US IS OUR MATH ANSWER CORRECT OR WRONG BY
DISPLAYING THE TEXT "Correct" or "Incorrect" on the
"label3"!
Gen_label2();
textBox1.Text="";
label3.Text=""; //AFTER YOU CLICK ON THE BUTTON,THE NEXT MATH
QUESTION WILL BE DISPLAYED!(The math question will be displayed on the
"label2",text in the "textBox1" will be deleted and the text on the
"label3" will also be deleted).
if(e.Button==MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle,WM_NCLBUTTONDOWN,HT_CAPTION,0);
} //WITH THIS CODE YOU CAN MOVE THE
FORM BY PRESSING AND HOLDING DOWN YOUR LEFT MOUSE
CLICK ANYWHERE ON THE FORM!(This
code must be written in the "MouseDown" event of
that form).
NOTE: This code will not work if you do not do the following.Bellow this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
Write this:
using System.Runtime.InteropServices;
NOTE: Any of this 3 lines of code won't work if you do not do the following.Bellow this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
Write this:
using System.Runtime.InteropServices;
openFileDialog1.Title="Open File";
openFileDialog1.Filter="Rich Text Files (*.rtf)|*.rtf|All Files (*.*)|*.*";
openFileDialog1.FileName="";
openFileDialog1.FilterIndex=0;
openFileDialog1.InitialDirectory="MyDocuments";
openFileDialog1.CheckFileExists=true;
openFileDialog1.CheckPathExists=true;
if(openFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
richTextBox1.LoadFile(openFileDialog1.FileName);
documentTitle=openFileDialog1.FileName;
} //AFTER CLICKING THE
BUTTON,YOU WILL GET THE DIALOG WINDOW WITH WHICH
YOU WILL BE ABLE TO OPEN
ANY TEXT FILE IN YOUR RICH TEXT BOX!(For
this option,rich text box and
open file dialog is needed).
NOTE: This code will not work if you do not do the following.Above this:
public Form1()
{
InitializeComponent();
}
Write this:
private string documentTitle;
saveFileDialog1.DefaultExt=".rtf";
saveFileDialog1.OverwritePrompt=true;
saveFileDialog1.Title="Save File";
if(saveFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog1.FileName,RichTextBoxStreamType.RichText);
documentTitle=saveFileDialog1.FileName;
} //AFTER CLICKING THE
BUTTON,YOU WILL GET THE DIALOG WINDOW WITH
WHICH YOU WILL BE ABLE
TO SAVE THE TEXT FILE WHICH YOU WROTE
IN THE RICH TEXT BOX!
(For this option,rich text box and save
file dialog is needed).
NOTE: This code will not work if you do not do the following.Above this:
public Form1()
{
InitializeComponent();
}
Write this:
private string documentTitle;
pageSetupDialog1.Document=printDocument1;
pageSetupDialog1.PageSettings=printDocument1.DefaultPageSettings;
pageSetupDialog1.AllowMargins=true;
pageSetupDialog1.AllowOrientation=true;
pageSetupDialog1.AllowPaper=true;
pageSetupDialog1.AllowPrinter=true;
pageSetupDialog1.ShowNetwork=true;
pageSetupDialog1.ShowHelp=true;
pageSetupDialog1.EnableMetric=false;
if(pageSetupDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
printDocument1.DefaultPageSettings=pageSetupDialog1.PageSettings;
} //AFTER CLICKING THE
BUTTON,YOU WILL GET THE PAGE SETUP DIALOG WINDOW,
WHERE YOU WILL BE ABLE TO
CONFIGURE THE OPTIONS FOR THE PAPER,PRINTER,
ORIENTATION...!(For this
option,page setup dialog and print document is
needed).
printDialog1.Document=printDocument1;
if(printDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
printDocument1.DocumentName=documentTitle;
printDocument1.Print();
} //AFTER CLICKING THE
BUTTON,YOU WILL GET THE PRINT DIALOG WINDOW WITH WHICH
YOU WILL BE ABLE TO PRINT THE
TEXT WHICH YOU WROTE IN THE RICH TEXT BOX!
(For this option,print dialog and
print document is needed).
[Still does not work properly]...
NOTE: This code will not work if you do not do the following.Above this:
public Form1()
{
InitializeComponent();
}
Write this:
private string documentTitle;
if(fontDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
richTextBox1.SelectionFont=fontDialog1.Font;
} //AFTER CLICKING THE BUTTON,YOU
WILL GET THE FONT DIALOG WINDOW IN WHICH YOU WILL
BE ABLE TO CHANGE THE FONT
OPTIONS FOR THE TEXT IN YOUR RICH TEXT BOX!(For this
option,rich text box and font dialog is
needed).
if(colorDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
richTextBox1.SelectionColor=colorDialog1.Color;
} //AFTER CLICKING THE BUTTON,YOU
WILL GET THE COLOR DIALOG WINDOW IN WHICH YOU WILL
BE ABLE TO CHANGE THE COLOR
OPTIONS FOR THE TEXT IN YOUR RICH TEXT BOX!(For this
option,rich text box and color dialog is
needed).
Image logoImage;
logoImage=Image.FromFile(String.Concat(Environment.GetFolderPath(Environment.Special
Folder.MyDocuments),@"\1.bmp"));
Clipboard.SetImage(logoImage);
richTextBox1.Paste();
Clipboard.Clear(); //
AFTER CLICKING THE BUTTON,THE
IMAGE
FROM THE WRITTEN LOCATION
WILL
BE DISPLAYED IN THE RICH
TEXT
BOX!(For this option,rich
text
box is needed).
string one=String.Empty;
one=System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
one+="\\4.jpg";
pictureBox1.ImageLocation=one; //THIS CODE WILL
SIMPLY LOAD THE PICTURE FROM YOUR "MyDocumets" FOLDER!
(The picture called "4.jpg" which
I put in "MyDocuments" folder will
be displayed in the picture box
after you click the button.For this
option,picture box is needed).
pictureBox1.ImageLocation="http://www.learnvisualstudio.net/images/lvs_logo_small.gif";
//AFTER YOU CLICK THE BUTTON,THE PICTURE FROM THE INTERNET
WEBSITE WILL BE
LOADED AND THEN DISPLAYED IN YOUR PICTURE
BOX!(For this
option,picture box is needed).[Be sure that
the picture exists on the
selected website].
switch(comboBox1.Text)
{
case "Normal":
pictureBox1.SizeMode=PictureBoxSizeMode.Normal;
break;
case "Stretch image":
pictureBox1.SizeMode=PictureBoxSizeMode.StretchImage;
break;
case "Autosize":
pictureBox1.SizeMode=PictureBoxSizeMode.AutoSize;
break;
case "Center image":
pictureBox1.SizeMode=PictureBoxSizeMode.CenterImage;
break;
case "Zoom":
pictureBox1.SizeMode=PictureBoxSizeMode.Zoom;
break;
} //WITH THIS CODE AFTER YOU
CHOOSE THE ITEM IN THE COMBO BOX AND THEN CLICK IT,
THE PICTURE IN THE PICTURE BOX
WILL BE DISPLAYED JUST ON THE WAY YOU SELECTED
IT!(Here I added 5 items and that are
"Normal","Stretch image","Autosize",
"Center image" and "Zoom".So for
example...if in the combo box you choose the
second item which is that "Stretch
image" and then click it,the picture in the
picture box will be stretched.For this
option,picture box and combo box is
needed).
pictureBox1.ErrorImage=Properties.Resources._1;
pictureBox1.InitialImage=Properties.Resources._2; //AFTER CLICKING THE
BUTTON,THE PICTURE BOX WILL BE INITIALIZED!(First line of code will
display the picture for error from our project
resources if the picture which we are
trying to load doesn't exist on the specified
location.Second line of code will simply
display the picture for loading from our
project resources.That picture for loading is
displayed in the picture box while the main
picture is loading.For this option,picture
box is needed).
if(tabControl1.SelectedIndex==0)
{
tabControl1.SelectTab(tabControl1.TabCount-1);
}
else
{
tabControl1.SelectTab(tabControl1.SelectedIndex-1);
} //AFTER CLICKING THE BUTTON,YOU WILL GO
ON THE PREVIOUS TAB!(For this option,tab control is
needed).
if(tabControl1.SelectedIndex==tabControl1.TabCount-1)
{
tabControl1.SelectTab(0);
}
else
{
tabControl1.SelectTab(tabControl1.SelectedIndex+1);
} //AFTER CLICKING THE BUTTON,YOU WILL
GO ON THE NEXT TAB!(For this option,tab control is
needed).
bool exclusive;
Mutex m=new Mutex(true,"INSTANCE",out exclusive);
if(exclusive) //THIS CODE WILL ALLOW YOUR
APPLICATION TO RUN ONLY ONCE!(If the application is already
running and you try to turn it ON again,the
application won't turn ON).[This code must
be written in the "Program.cs" file].
else
{
MessageBox.Show("Application is already running!","RUNNING...");
} //THIS CODE WILL GIVE US THE
MESSAGE BOX WHICH WILL TELL US THAT THE
APPLICATION IS ALREADY
RUNNING!(This code is connected with the
previous code).[It also must be
written in the "Program.cs" file].
To explain this 3 codes that allows your application to run only once,here is the simple code
view of the "Program.cs" file:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
namespace WindowsApplication1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
bool exclusive;
Mutex m=new Mutex(true,"INSTANCE",out exclusive);
if(exclusive)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
MessageBox.Show("Application is already
running!","RUNNING...");
}
}
}
}
NOTE: This code will not work if the option "Optimize code" is enabled in your project
properties,in the "Build" section.
NOTE: This two lines of code are very useful if you want to save the changes from the rich
text box and then load that changes back to your application.
if(frm.IsDisposed==false)
{
frm.Show();
}
if(frm.IsDisposed==true)
{
frm=new Form2();
frm.Show();
} //WITH THIS CODE YOU CAN OPEN THE NEW FORM ONLY
ONCE NO MATTER HOW MANY TIMES YOU CLICK THE BUTTON!
this.SetDesktopLocation(Convert.ToInt32(textBox1.Text),Convert.ToInt32(textBox2.Text));
//AFTER CLICKING ON THE BUTTON,THE DESKTOP LOCATION
OF THE FORM IS
GOING TO BE SET ON THE CUSTOM
DESKTOP
LOCATION!(For this option,two text boxes
are needed).
this.SetDesktopLocation((Int32)Convert.ToInt64(richTextBox1.Text),
(Int32)Convert.ToInt64(richTextBox2.Text)); //THIS CODE
HAS THE SAME FUNCTION LIKE THE PREVIOUS ONE!(The only difference here is that this
code is using 64 bit integer instead of
32 bit integer).[In this code I needed to convert Int32 to Int64 in order to make it work for
the SetDesktopLocation
method.I converted Int32 to Int64 using "(Int32)" before using "Convert.ToInt64"].
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
catch(Exception)
{
MessageBox.Show("CATCH has passed...");
}
finally
{
MessageBox.Show("FINALLY has passed...");
} //IN THIS CODE EXAMPLE,IN FIRST 6
LINES OF CODE,THE
PROGRAM WILL TRY TO EXECUTE THE 3.
4. AND 5.
STATEMENT!IF FOR SOME REASON THEY
CANNOT BE
EXECUTED,THE 7. 8. 9. AND 10.
STATEMENT WILL BE
EXECUTED BY THROWING/CATCHING AN
EXCEPTION THAT
WILL SHOW THE MESSAGE BOX THAT
SAYS "CATCH has
passed..."!AFTER THAT,THE 11. 12. 13.
AND 14.
STATEMENT WILL BE EXECUTED BY
THROWING/CATCHING
THE FINAL STEP THAT WILL SHOW THE
MESSAGE BOX THAT
SAYS "FINALLY has passed..."!(The final
step is
executed no matter if the exception was
thrown or
not).[I took this example from
"Program.cs" file].
textBox1.Text="Copying...";
textBox1.Update();
System.IO.File.Copy("C:\\Documents and Settings\\S.T.A.R.S.---UAC\\Desktop\\PCSX2
trailer.avi","C:\\PCSX2 trailer.avi");
textBox1.Text="Done!"; //AFTER CLICKING THE BUTTON,THE
FILE "PCSX2 trailer.avi" WILL
BE COPIED FROM ONE LOCATION TO
ANOTHER!(After you click the
button,the text in the text box will be
"Copying...".After
the copying is finished,the text in the text box
will be
"Done!".In this example I copied the file from
"Desktop" to
disk "C:".The second line of code updates the
text box in
order to show the text "Copying..." while the
copying of
that file is in progress.If you do not write that
second
line of code,the text "Copying..." will not
appear.For this
option,text box is needed).[Be sure that you
use two
slashes for separation instead of one slash].
textBox1.Text="Moving...";
textBox1.Update();
System.IO.File.Move("C:\\Documents and Settings\\S.T.A.R.S.---UAC\\Desktop\\PCSX2
trailer.avi","C:\\PCSX2 trailer.avi");
textBox1.Text="Done!"; //AFTER CLICKING THE BUTTON,THE
FILE "PCSX2 trailer.avi" WILL
BE MOVED FROM ONE LOCATION TO ANOTHER!
(This code executes
everything the same like the previous one,only
here we are
moving the file from "Desktop" to disk "C:"
instead of
copying the file.For this option,text box is
needed).
textBox1.Text="Deleting...";
textBox1.Update();
System.IO.File.Delete("C:\\Documents and Settings\\S.T.A.R.S.---UAC\\Desktop\\PCSX2
trailer.avi");
textBox1.Text="Done!"; //AFTER CLICKING THE BUTTON,THE
FILE "PCSX2 trailer.avi" WILL
BE DELETED FROM YOUR HARD DISK DRIVE!
(This code is even
easier then the previous two examples.Here I
am simply
deleting the file from the "Desktop".For this
option,text
box is needed).
textBox1.Text="Copying...";
textBox1.Update();
try
{
System.IO.File.Copy("C:\\Documents and Settings\\S.T.A.R.S.---UAC\\Desktop\\PCSX2
trailer.avi","C:\\PCSX2 trailer.avi");
}
catch(Exception)
{
MessageBox.Show("The file already exists.It will now be replaced...");
System.IO.File.Copy("C:\\Documents and Settings\\S.T.A.R.S.---UAC\\Desktop\\PCSX2
trailer.avi","C:\\PCSX2 trailer.avi",true);
}
textBox1.Text="Done!"; //AFTER CLICKING THE BUTTON,THE
FILE "PCSX2 trailer.avi" WILL
BE COPIED FROM ONE LOCATION TO
ANOTHER!(In this example,
everything is the same like in the prevoius
example for
"File.Copy" method,only here I also added an
exception.That
means the that exception will be thrown if you
try to copy
the file that already exists.If for example you
have
already copied the file and want to copy it again
with the
same name on the same location,you will get
the message box
which will say "The file already exists.It will
now be
replaced..." and immediately after you click the
OK button,
the entire file will be replaced.For this
option,text box
is needed).[The 10. line of code is same like 5.
line of
code.The only difference is the 10. line of code
on the end
has the word "true".That word "true" is the bool
value
which tells the program to replace the file.If
instead of
"true" I put "false",the bool value will be
oposite and the
program will not replace the file.Thanks to this
exception,
you can replace/recopy the file as many times
as you want].
textBox1.Text="Moving folder...";
textBox1.Update();
System.IO.Directory.Move("C:\\Documents and Settings\\S.T.A.R.S.---
UAC\\Desktop\\Example","C:\\Example");
textBox1.Text="Done!"; //AFTER CLICKING THE BUTTON,THE
FOLDER "Example" WILL BE
MOVED FROM ONE LOCATION TO ANOTHER!
(Here I am simply moving
the folder from "Desktop" to my disk "C:".When
the folder
is moved,all the subfolders and files inside of
that folder
are also moved.For this option,text box is
needed).
textBox1.Text="Deleting folder...";
textBox1.Update();
System.IO.Directory.Delete("C:\\Example",true);
textBox1.Text="Done!"; //AFTER CLICKING THE BUTTON,THE
FOLDER "Example" WILL BE
DELETED FROM YOUR HARD DISK DRIVE!(Here
I am just deleting
the folder from disk "C:".You can notice that on
the end of
the third line of code there is a word
"true".That word
"true" tells the program to delete the entire
folder,
including all the subfolders and files inside of
that
folder.If we do not use the word "true" or
instead of it we
use the word "false",the program will be able to
delete
only the empty folder.For this option,text box is
needed).
textBox1.Text="Copying file...";
progressBar1.Minimum=0;
progressBar1.Step=10;
progressBar1.Maximum=100;
progressBar1.Value=0;
timer1.Start();
backgroundWorker1.RunWorkerAsync(); //AFTER YOU CLICK THE
BUTTON,THE FILE "Video.avi" WILL BE
COPIED FROM ONE LOCATION TO ANOTHER!
(Here I am copying the
file from my "Desktop" to my disk "C:".After
clicking the
button,the file "Video.avi" will start copying.While
the file
is copying,the text in the text box will be
"Copying file..."
and there is also going to be the progress bar
which will be
showing the progress of that file copying.The
progress bar
will be repeating the progress all until copying is
finished.
When the copying is finished,the text in the text
box will be
"Copying finished!" and the progress in the
progress bar will
be at his maximum value which is in this case
"100".This way
of copying the files is good because the COPYING
process will
not pause the rest execution.That's why I have
put the code
for COPYING files in the background.For this
option,text box,
timer and background worker is needed).
NOTE: This code will not work if you do not do the following.In the timer event called
"Elapsed" write this code:
if(progressBar1.Value!=100)
{
progressBar1.PerformStep();
}
if(progressBar1.Value==100)
{
progressBar1.Value=0;
progressBar1.PerformStep();
}
Also in the background worker event called "DoWork" write this code:
System.IO.File.Copy("C:\\Documents and Settings\\S.T.A.R.S.---
UAC\\Desktop\\Video.avi","C:\\Video.avi",true);
And at the end,in the background worker event called "RunWorkerCompleted" write this
code:
timer1.Stop();
textBox1.Text="Copying finished!";
progressBar1.Value=100;
"/n" //THIS IS NOT THE CODE!(This is very useful when writing the text for
the message box.With the "/n" function,you can write
the text in the new line as many times as you want).
System.Object nullObject = 0;
string str = "";
System.Object nullObjStr = str;
axWebBrowser1.Refresh2(ref nullObjStr); //AFTER CLICKING THE
BUTTON,THE PAGE IN THE WEB BROWSER WILL BE
REFRESHED!(For this option,web browser is
needed).
System.Object nullObject = 0;
string str = "";
System.Object nullObjStr = str;
axWebBrowser1.Navigate(textBox1.Text,ref nullObject,ref nullObjStr,ref nullObjStr,ref
nullObjStr); //AFTER YOU WRITE THE
BUTTON,THAT PAGE
WILL BE OPENED IN
browser is needed).
Process[] procs=Process.GetProcesses(Environment.MachineName.ToString());
foreach(Process process in procs)
{
if(process.MainWindowTitle.Length>0)
{
listBox1.Items.Add(process.MainModule.ModuleName);
}
} //AFTER CLICKING
THE BUTTON,THE LIST BOX WILL DISPLAY ALL THE
PROCESSES THAT
ARE CURRENTLY ACTIVE!(For this option,list box is
needed).
Process[] procs=Process.GetProcesses(Environment.MachineName.ToString());
foreach(Process process in procs)
{
try
{
if(process.MainModule.ModuleName==textBox1.Text)
{
process.Kill();
}
}
catch(Exception)
{
//No function to execute...
}
} //AFTER THE BUTTON
IS CLICKED,THE PROCESS NAME WHICH THE USER
WROTE IN THE TEXT
BOX WILL END!(This is better example for ending
the specified process
because you must also include the extension
together with the
process name.For example:"notepad.exe".If instead
of the "notepad.exe"
you write just "notepad",the process won't end.
For this option,text
box is needed).
Image testImage=Image.FromFile("TEST_IMAGE.bmp");
testImage.Save("C:\\SAVED_IMAGE.bmp"); //AFTER CLICKING
THE BUTTON,THE SAME IMAGE WILL BE SAVED TO
ANOTHER LOCATION
ON YOUR HARD DISK DRIVE!
ImageFormat format1=ImageFormat.Jpeg;
Image image1=Image.FromFile("C:\\FIRST.bmp");
image1.Save("C:\\SECOND.jpg",format1); //WITH THIS CODE
YOU CAN CONVERT AND SAVE ANY IMAGE FORMAT TO
ANY OTHER IMAGE
FORMAT!(For example...you can convert and save
BMP image to JPEG
image or to any other image format such as PNG,GIF
and so on...).
...write this:
using System.Drawing.Imaging;