HNDIT1012 Visual Application Programming Week 2 PDF
Document Details
Uploaded by Deleted User
Tags
Summary
These are lecture notes for a Visual Application Programming course, specifically for HNDIT1012 Week 2. The notes cover various aspects of creating Windows applications using C#, including how to add controls, use events, and implement different operators like +,-,*,/ and their importance within the context of C#. Explanations and examples are included.
Full Transcript
HNDIT1012 Visual Application Programming Week 2 Windows Application - Step 1 Step 2 3 Designing Form – Step 3 4 Adding Controls Design the form as shown above. The controls used in the form are: – TextBox :Text bo...
HNDIT1012 Visual Application Programming Week 2 Windows Application - Step 1 Step 2 3 Designing Form – Step 3 4 Adding Controls Design the form as shown above. The controls used in the form are: – TextBox :Text box controls allow entering text on a form at runtime. By default, it takes a single line of text, however, you can make it accept multiple texts and even add scroll bars to it. Let's create a text box by dragging a Text Box control from the Toolbox and dropping it on the form. – Button : Button control is used to perform a click event in Windows Forms, and it can be clicked by a mouse or by pressing Enter keys. – Label : The Label control represents a standard Windows label. It is generally used to display some informative text on the GUI which is not changed during runtime. 5 Property Window You can find Properties Window on the View menu. You can also open it by pressing F4 or by typing Properties in the search box. The Properties window displays different types of editing fields, depending on the needs of a particular property. 6 Setting the properties – Step 4 Select the first textbox and set the name as txtNum1 on the property window. Similarly set the names of other two textboxes as txtNum2 and txtResult respectively. For Labels, Set the text property to Number 1, Number 2 and Result respectively For Buttons, set the properties as shown in the given table. Name Text btnPlus + btnMinus - btnMul X btnDiv / 7 Events An event is a signal that informs an application that something important has occurred. For example, when a user clicks a control on a form, the form can raise a Click event and call a procedure that handles the event. Events also allow separate tasks to communicate. 8 List of Events of a Event Button Filter You can filter List of events on property window by clicking the event filter 9 Adding Click events – Step 5 Double click on plus button to open code editor for click event and type the following code. 10 Running the application Input values for Number1 and Number 2 and check the Result by clicking the plus button. For eg: Number 1 Number 2 Result Com puter Computer 12 6 126 I am Kumar I amKumar 11 Addition Vs Concatenation In the above example ‘+’ operator works as a string Concatenation operator, not an addition operator. Because both operands are string. If you need addition operation, you have to convert both operands to numeric values such as integer, double etc.. 12 Change the code of the click event as shown below to do arithmetic operations like +, -, * and / Run the application and input integer values to Number 1 and Number 2 13 Int32.Parse Method Converts the string representation of a number to its 32-bit signed integer equivalent. toString Method It converts an object to its string representation so that it is suitable for display. 14 C# Data types C# type keyword.NET type bool System.Boolean byte System.Byte sbyte System.SByte char System.Char decimal System.Decimal double System.Double float System.Single int System.Int32 uint System.UInt32 nint System.IntPtr nuint System.UIntPtr long System.Int64 ulong System.UInt64 short System.Int16 string System.String 15 Add click events to other buttons and type the code as given below: 16 / operator performs an integer division if both operands are integers. Eg: 3/4 = 0 4/4 =1 5/4=1 17 C# Arithmetic Operators Operator Name Description Example + Addition Adds together two values x+y - Subtraction Subtracts one value from x-y another * Multiplicatio Multiplies two values x*y n / Division Divides one value by x/y another % Modulus Returns the division x%y remainder ++ Increment Increases the value of a x++ variable by 1 -- Decrement Decreases the value of a x-- variable by 1 18 C# assignment Operators Operator Example Same As = x=5 x=5 += x += 3 x=x+3 -= x -= 3 x=x-3 *= x *= 3 x=x*3 /= x /= 3 x=x/3 %= x %= 3 x=x%3 &= x &= 3 x=x&3 |= x |= 3 x=x|3 ^= x ^= 3 x=x^3 >>= x >>= 3 x = x >> 3