944,015 Members | Top Members by Rank

Ad:
  • C# Code Snippet
  • Views: 3289
  • C# RSS
0

Getting basic with Visual Studio in C#

by on Nov 29th, 2008
Visual Studio is a great tool to use. But for beginners like me a few years back, it was a bit overwhelming and confusing. You got the feeling you lost all the control. VS has it’s special ways of doing things with partial classes and so on.
So lets get back in control (for a while) this has the drawback we have to do a lot of things for ourself. But it’s fun to do it once and in the future we will enjoy the benefits that VS offers even more.
So lets begin with a new project by clicking File->New Project in the dialog that opens click Empty Project and give it a name. Click OK. What you see now is a bit different from the usual Console- or Form Application. You should see practically nothing, emptyness all over!
So lets start filling that in. Rightclick your project in the solutions explorer and select Add New Item…In the dialog that appears select Code File and give it a name if you want to. Click the Add button. Paste the code snippet in or type it in. Or start with a very basic
public class MyWindow : Form
{
}
Run it and see what happens…Build it up line per line from here. Experiment like hell, it’s very instructive! Enjoy the learning. Don’t forget to fill in the References map with the necessary dll’s in the solution explorer by right clicking it and selecting Add Reference…, .
C# Code Snippet (Toggle Plain Text)
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing;
  4.  
  5. public class MyWindow : Form //derive a class MyWindow from a Form class
  6. {
  7. Button MyButton = new Button(); //make a new button
  8. TextBox MyTextBox = new TextBox(); //make a new textbox
  9.  
  10. public MyWindow() //constructor
  11. {
  12. //===================================================================
  13. //set some properties of this MyWindow form
  14. this.Text = "Just testing";
  15. this.BackColor = Color.DeepSkyBlue;
  16.  
  17. //===================================================================
  18. //set location,size and text of the newly created button on the form
  19. MyButton.Location = new Point(100, 100);
  20. MyButton.Size = new Size(50, 50);
  21. MyButton.Text = "Do it";
  22. //hook in a routine to handle click events for MyButton
  23. MyButton.Click += new EventHandler(MyButton_Click);
  24. //add the control to the MyWindows forms control collection
  25. this.Controls.Add(MyButton);
  26.  
  27. //===================================================================
  28. //set location,size and text of the newly created textbox on the form
  29. MyTextBox.Location = new Point(10, 10);
  30. MyTextBox.Size = new Size(150, 20);
  31. MyTextBox.Text = "";
  32. //add the control to the MyWindows forms control collection
  33. this.Controls.Add(MyTextBox);
  34.  
  35. //===================================================================
  36. }
  37.  
  38. private void MyButton_Click(object Obj, EventArgs EvA)
  39. {
  40. //aarghh the ultimate
  41. //by clicking on our button, set a text in a textbox
  42. this.MyTextBox.Text = "Hello world!";
  43. }
  44.  
  45. private static void Main()
  46. {
  47. Application.Run(new MyWindow()); //start the application
  48. }
  49. }
Message:
Previous Thread in C# Forum Timeline: Merge dotnet file and crystal report with C# project
Next Thread in C# Forum Timeline: import from & export to excel





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC