230 likes | 343 Vues
This comprehensive guide is designed for beginners learning C#. It covers essential programming concepts, including data types, naming conventions, assignments, comments, operators, and mathematical functions. The second part delves into graphical user interfaces (GUI) and controls, such as buttons, text boxes, and list boxes. You'll learn how to manage properties and events, making your applications user-friendly. By the end, you will understand how to implement various controls and use dialog boxes effectively to enhance user interaction.
E N D
Learning OutcomesPart I We will learn about : • Data types • Naming convention • Assignments and comments • Converting data types • Math functions • Operators and comparisons
Naming convention • C# is case sensitive Naming convention • Variables start with lowercase letter. • Methods and Classes start with Capital letter • Separate words with Uppercase letter : upperCase’ would be a variable. • Controls should start with three letters indicating the control: lblTitle, btnStop, txtName
Data types Basic data types: Integer : byte, int, long Floating point: float, double Characters: char, string Logical: bool Declaration: Datatype name ( = initValue);
Declarations Examples: const double PI =3.14159; const int MAXSIZE = 10, MINSIZE = 1; int Number; double Numberf = 34.5; char c1, c2; bool test = true; string Message = “Cancel”;
Assignment and Comments Assignments: ‘=‘ var= expression; sum = one + two; Comments: /* Slash-star is a multi-line comment- end with a star-slash */ // a double slash which ignores the rest of the line
Operators Operators: Usual operators and order of operation (precedence): +, -, *, /, % ==, !=, >, <, <=, >= Relational Logic operators: AND a = a & b; OR a= a | b; NOT a= !b; XOR a= a ^ b;
Shortcuts Shortcuts X = X + 3; X+=3; div = div / 4; div /= 4; mult = mult * 5; mult *= 5; sub = sub - 2; sub -= 2; rem = rem%4; rem %= 4; Increment i++; ++i : decrement --i ; i--;
Math functions Some Math functions: Sin, Cos, Tan, Exp Log - Returns the logarithm of a specified number. Log10 - Returns the base 10 logarithm. Pow - Returns a number raised to a specified power. Round - Rounds to the nearest integer or decimal places. Sqrt - Returns the square root of a specified number. Truncate - Calculates the integral part of a number. ‘
Comparison == equal to != not equal to < less than <= less than or equal to > greater than >= greater than or equal to && AND || OR
Summary Covered: • Variables constants and data types • Naming convention • Assignments and comments • Operators • Math functions • Logic Operations • Comparison
Learning Outcomes Part II • You will learn about : • GUI • Controls • Button, TextBox, ListBox • Properties and Events • Dialog boxes
GUI and Controls • GUI - Graphical User Interface • How your user interfaces with your program • You can make it really easy (or difficult) for the user! • Lots of controls • e.g. buttons, textboxes, labels, menus • You place them on the form • IDE groups similar controls together • You write code for them
Common controls • Common Controls – in Toolbox • Button • TextBox • ListBox • Label • Checkbox • Radio buttons • Picture box • Menu • ..... • All have properties and events
Button control • Main properties: • Appearance – Color, Image, Text • Behavior – Enabled, Visible • Design – Name • Layout – Location, Size • ........ • Main events: • Click • Double-click
Text Box Control • Properties – Similar to button, but also text entry • Text (single line) • Lines (multiline) • MultiLine is a property • ReadOnly • Events • Keyboard events • TextChanged • Focus – Enter
Text Box control • Reading text • Text property – all text • Individual lines: • textBox1.Lines[0]; • Add text using AppendText • textBox1.AppendText("new text"); • To add on new line: • textBox1.AppendText("\r\nnew last line"); • Backslash indicates non-printing characters • Number of Lines? • textBox1.Lines.Length
TextBox Control • Conversion • String to int • int N; • N = Convert.ToInt(textBox1.Text); • or • N = int.Parse(textBox1.Text); • int to string: • int i = 20; • textBox1.AppendText(i.ToString())
ListBox Control List of selectable items Properties: Items (collection) ScrollBars (Horizontal & Vertical) MultiColumn Events Click SelectedIndexChanged
TextBox and ListBox TextBox and ListBox text properties Text – all the text in the text or list box. Lines[0] – first line of text or list box – read only AppendText(“new text”); – adds text to textbox Lines.Length – number of lines in textbox ListBox Items.Add(“new list item”); Items.Insert(lineNo, “inserted text”); Clear(); SelectedItem Data is a string – need to convert for numbers.
Dialog Boxes Easy way to display text information. A dialog box is not a control it is displayed when program runs MessageBox.Show(Message);
Dialog Boxes You Can add Caption, buttons and icon: MessageBox.Show("This will format your disk!", "Format caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation); Check response with DialogResult.OK DialogResult.Cancel DialogResult.Yes …..
Summary Covered • GUI • Controls • Button, Textbox etc • Properties and Events • Dialog boxes