Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

​Q1. Describe the anatomy of Form.

 
In C#, a form is an object that represents a
graphical user interface (GUI) window or dialog
box. It contains controls such as buttons, text boxes,
and labels, and is used to interact with the user.

The anatomy of a form in C# includes:

Title bar: The title bar is located at the top of the


form and displays the title of the form.

Menu bar: The menu bar is a container for menus


that contain commands or options for the user.

Control box: The control box is a collection of


buttons that allow the user to minimize, maximize,
or close the form.

Client area: The client area is the region of the form


where controls and other visual elements are
placed.

Status bar: The status bar is located at the bottom of


the form and displays information about the
current state of the application.
Border: The border surrounds the form and
separates it from other windows or the desktop. 

Q2. What are controls? 


In C#, controls refer to user interface elements that
allow users to interact with an application. 
Some examples of controls in C# include: 
Button: A control that users can click to trigger an
action.
TextBox: A control that allows users to enter and
edit text.
Label: A control that displays text or an image.
ComboBox: A control that allows users to select an
option from a dropdown list.
RadioButton: A control that allows users to select a
single option from a group of options.
CheckBox: A control that allows users to select one
or more options from a group of options. 

Q3. Build an application in C# that monitors all


mouse events. Display a message in a label when
each event occurs. 
using System;
using System.Windows.Forms;
namespace MouseEvents
{
    public partial class Form1 : Form
  {
        public Form1()
    {
            InitializeComponent();
            // Subscribe to mouse event handlers
            this.MouseClick += new
MouseEventHandler(Form1_MouseClick);
            this.MouseDown += new
MouseEventHandler(Form1_MouseDown);
            this.MouseUp += new
MouseEventHandler(Form1_MouseUp);
            this.MouseMove += new
MouseEventHandler(Form1_MouseMove);
    }

        private void Form1_MouseClick(object sender,


MouseEventArgs e)
    {
            // Display message in label
            label1.Text = "Mouse Click: X=" + e.X + ", Y=" +
e.Y;
    }
        private void Form1_MouseDown(object sender,
MouseEventArgs e)
    {
            // Display message in label
            label1.Text = "Mouse Down: X=" + e.X + ", Y="
+ e.Y;
    }

        private void Form1_MouseUp(object sender,


MouseEventArgs e)
    {
            // Display message in label
            label1.Text = "Mouse Up: X=" + e.X + ", Y=" +
e.Y;
    }

        private void Form1_MouseMove(object sender,


MouseEventArgs e)
    {
            // Display message in label
            label1.Text = "Mouse Move: X=" + e.X + ", Y="
+ e.Y;
    }
  }

Q4. Write a windows application in C#, having two
textbox controls and a button control, validate that
the textbox values are not blank. Clicking the
button will interchange the textbox values and the
button disappears. 
using System;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
  {
        public Form1()
    {
            InitializeComponent();
    }

        private void button1_Click(object sender,


EventArgs e)
    {
            string temp = textBox1.Text;
            textBox1.Text = textBox2.Text;
            textBox2.Text = temp;
            button1.Visible = false;
    }

        private void textBox1_TextChanged(object


sender, EventArgs e)
    {
            button1.Enabled =
!string.IsNullOrWhiteSpace(textBox1.Text) &&
!string.IsNullOrWhiteSpace(textBox2.Text);
    }

        private void textBox2_TextChanged(object


sender, EventArgs e)
    {
            button1.Enabled =
!string.IsNullOrWhiteSpace(textBox1.Text) &&
!string.IsNullOrWhiteSpace(textBox2.Text);
    }
  }
}
Q5. Write an C# application with two textbox
controls (name and Telephone) and a button
control. After submitting the form, the application
redirects to another form and displays the Name
and Telephone in it Blue Color. 
using System;
using System.Windows.Forms;
using System.Drawing;

namespace TextBoxExample
{
    public partial class Form1 : Form
  {
        public Form1()
    {
            InitializeComponent();
    }

        private void submitButton_Click(object sender,


EventArgs e)
    {
            string name = nameTextBox.Text;
            string telephone = telephoneTextBox.Text;

            Form2 form2 = new Form2(name,


telephone);
            form2.Show();

            nameTextBox.Text = "";
            telephoneTextBox.Text = "";
    }
  }

    public partial class Form2 : Form


  {
        public Form2(string name, string telephone)
    {
            InitializeComponent();

            nameLabel.Text = name;
            telephoneLabel.Text = telephone;

            nameLabel.ForeColor = Color.Blue;
            telephoneLabel.ForeColor = Color.Blue;
    }
  }

Q6. Build a stop watch using Windows Form. 
using System;
using System.Windows.Forms;
using System.Diagnostics;

namespace StopWatchExample
{
    public partial class Form1 : Form
  {
        private Stopwatch stopwatch = new
Stopwatch();

        public Form1()
    {
            InitializeComponent();
    }

        private void startStopButton_Click(object


sender, EventArgs e)
    {
            if (stopwatch.IsRunning)
      {
                stopwatch.Stop();
                startStopButton.Text = "Start";
      }
            else
      {
                stopwatch.Start();
                startStopButton.Text = "Stop";
      }
            TimeSpan elapsed = stopwatch.Elapsed;
            timeLabel.Text = String.Format("{0:00}:
{1:00}:{2:00}", elapsed.Hours, elapsed.Minutes,
elapsed.Seconds);
    }

        private void resetButton_Click(object sender,


EventArgs e)
    {
            stopwatch.Reset();
            timeLabel.Text = "00:00:00";
            startStopButton.Text = "Start";
    }
  }
}

Q7. Write the steps to add a button and picture box


controls to a form. 
Open your Visual Studio and create a new Windows
Forms Application project.
In the Solution Explorer, open the form where you
want to add the button and picture box controls.
From the Toolbox, drag and drop a Button control
onto the form. You can find the Button control in
the Common Controls section of the Toolbox.
Click on the button control and locate the
properties window. Set the Name property to a
meaningful name, such as "btnSubmit", and set the
Text property to the desired label for the button,
such as "Submit".
From the Toolbox, drag and drop a PictureBox
control onto the form. You can find the PictureBox
control in the Common Controls section of the
Toolbox.
Click on the picture box control and locate the
properties window. Set the Name property to a
meaningful name, such as "picImage", and set the
Image property to the desired image for the picture
box. You can also set other properties such as
SizeMode, BorderStyle, etc.
Position the button and picture box controls on the
form using the mouse or by setting the Location
property in the properties window. 

Q8. What are the two different approaches for


event handling? What is the difference between
them? 
Polling: In the polling approach, the program
repeatedly checks for the occurrence of an event by
querying the input devices or other event sources.
The program continuously checks whether an event
has occurred, and when it detects an event, it takes
the appropriate action.
Interrupt-driven: In the interrupt-driven approach,
the program does not continuously poll for events.
Instead, the program registers an interrupt handler
to be executed when an event occurs. The interrupt
handler is a function that is executed by the
operating system or hardware when an event
occurs.  
The main difference between the two approaches is
how they handle events. The polling approach
continuously checks for events, while the interrupt-
driven approach waits for events to occur and only
executes code when an event occurs. The interrupt-
driven approach is generally more efficient, but it
can be more complex to implement. The polling
approach is simpler to implement but can be less
efficient. 

Q9. What property would you use to control the


shape of the mouse pointer when it enters the
Client area of a Windows form? 
To control the shape of the mouse pointer when it
enters the client area of a Windows form, you can
use the Cursor property of the form.
You can set the Cursor property to a specific cursor
shape such as Arrow, Hand, or WaitCursor to
change the shape of the mouse pointer when it
enters the client area of the form. For example, if
you want to change the mouse pointer to a hand
shape when it enters the client area of a form. 

Q10. How can you add a custom property to a


form? 
To add a custom property to a form in C#, you can
follow these steps:
Open the form in the designer.
Open the Properties window by pressing F4 or
selecting View > Properties Window from the
menu.
Click on the lightning bolt icon in the Properties
window to switch to the Events tab.
Click on the arrow next to (Custom) to expand the
custom properties section.
Click on the ellipsis button (...) next to (Custom) to
open the Custom Attributes dialog box.
In the Custom Attributes dialog box, click on the
Add button to add a new custom property.
Enter a name for the property in the Name field,
and select the data type for the property from the
Type dropdown list.
Click OK to close the Custom Attributes dialog box
and save the new property.
Set the value of the custom property in your code
by using the dot notation, like you would with any
other property of the form.

You might also like