This is little a «Questions-Answer» node for my international visitors. Hope you find your answer here…
P.s. Sorry for my English =)
Question: How to stop thread?
Answer: There are two way to stop thread. First is aborting.
Thread t = new Thread(function); t.Start(); t.Abort(); //Stoping the thread
Second way — using some flag. Example:
bool flag = false; void function() { while (!flag) { MessageBox.Show("Message!"); Thread.Sleep(2000); } }
If you set variable ‘flag’ to ‘true’ — the thread will be exit.
Question: How to convert string to int?
Answer:
string s = "123"; int i = Int32.Parse(s);
Question: How to create file or folder?
Answer:
//Directory System.IO.Directory.CreateDirectory("MyDirName"); //File ////Work with bytes System.IO.FileStream fs = System.IO.File.Create("FileName"); ////Work with text ///////Read System.IO.StreamReader sr = System.IO.StreamReader("FileName"); ///////Write System.IO.StreamWriter sw = System.IO.StreamWriter("FileName");
Question: What is ‘get’ and ‘set’ keywords?
Answer: You can use keywords ‘get’ and ‘set’ to protect element of your class. You can disable modification of class element, or disable reading element. Example:
class Test { private int _a = 5; private int _b = 5; private int _c = 5; public int d = 5; public int a { get { return this._a; } } public int b { set { this._b = value; } } public int c { set { this._c = value; } get { return this._c; } } } /// Test t = new Test(); t.a = 1; //error int temp = t.a; //ok t.b = 1; //ok temp = t.b; //error t.c = 1; //ok temp = t.c; //ok t.d = 1; //ok temp = t.d; //ok
Question: How to clone object?
Answer:
Button b = new Button(); Button clone = (Button)b.Clone();
Question: How create event?
Answer: Events — it’s functions. To create event in your class you can use this code:
class T { int A = 1; public delegate void _AChangeEvent(); public event _AChangeEvent AChangeEvent; public void setA(int A) { this.A = A; this.AChangeEvent(); } } /////in main class, onLoad event T t = new T(); t.AChangeEvent += new T._AChangeEvent(t_AChangeEvent); t.setA(2); /////in main class void t_AChangeEvent() { //Show message when function 't.setA' was call MessageBox.Show("'A' was changed!"); }
Question: How to change current directory (location)?
Answer: To change current directory use class ‘Environment’.
Environment.CurrentDirectory = "C:\\";
Question: How to read file line by line?
Answer: To read all file by line you need use ‘while’ loop.
System.IO.StreamReader sr = new System.IO.StreamReader("filename"); string line = String.Empty; while ((line = sr.ReadLine()) != null) { MessageBox.Show("Current line: " + line); }
Question: How show form modal?
Answer: You need create new windows form class to your project (for example with name ‘Form2’) and use this code:
Form2 f2 = new Form2(); f2.Show(); //Show not modal form f2.ShowDialog(); //Show modal form
Question: How do something every second?
Answer: If you need do some code with some interval use this code:
System.Windows.Forms.Timer t = new System.Windows.Forms.Timer(); t.Interval = 2 * 1000; //every two seconds t.Tick += new EventHandler(t_Tick); //t.Enabled = false; //Stop timer t.Enabled = true; //Start timer ////////// void t_Tick(object sender, EventArgs e) { MessageBox.Show("This message show every two seconds!"); }
Also… You can use ‘google tranclate’ to view my site in your language and try find more answers on questions. Same if you have a question, feel free to contact me. I try to help you. Thanks!