Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
lectures.docx
Скачиваний:
10
Добавлен:
01.07.2025
Размер:
8 Мб
Скачать
☆

It is used to limit user to enter a certain type of numbers

Example:

Topic: Multiple forms

Example-1:

Result: when we click the form1 button then form2 window will appear.

Example-2:

Result: After clicking form1 button then the form2 will appear and it will not allow you to go to the form1 window.

Example-3:

Result: if you write a text in form1’s text box, after clicking form1 button that text will appear in the form2 window.

Example-4:

Result: after clicking Form1 button then the form2 will appear inside the form1 window.

Example-5:

Result: You can open many other forms inside one form window.

Example-6:

Result:

Example-7:

Result:

Topic: ComboBox

Example-1:

Topic: ProgressBar

Example-1:

Result: When it reaches to the end of progress bar then the button will be disabled.

Example-2:

Result: It is same as previous example, but in this example the progress will not increase by value 10 in each step.

Example-3:

Result:

Example-4:

Result: You can pause the progress bar in the middle when you click stop button.

Topic: ListView Control

Example-1:

Note: After adding List View Control choose details inside it.

Result: After filling the text boxes and pressing add button, the entered values will be in the list.

Example-2: Add ContextMenuStrip from Toolbox.

Edit menu inside the context menu strip as “ Select a Text”.

Go to the properties panel of ListView and select ContextMenuStrip from none to ContextMenuStrip1

Result:

Example-3: After changing multi select from false to true in the properties of list view, we can select multiple items at once.

To select many texts first of all we change multiselect from false to true in the properties panel of list view.

Result:

Example-4: we can remove selected row from the list.

For that first of all we write some function name inside the context menu strip.

And then double click on that.

Example-5: We can remove all information at once without selecting text.

Example-6: We can use check boxes for every row in the list view by changing checkboxes from false to true in the property of list view.

Example-7:

Result: After checking any information row in the list view and clicking Remove button, we can remove that information row from the list view at all.

Topic: ToolStrip Control

Example:

Note: Choose SplitButton inside the menu of ToolStrip.

Download icons from the following web address:

And put it into your toolstrip, by uploading the image in the properties panel.

No follow the same steps for Save icon as below:

Topic: NotifyIcon Control

Example-1:

Next, upload an icon to this notifyicon as shown below:

Result:

Example-2: In order to hide the running application we add a hide method.

Result: our running application will disappear from the desktop, but will be running at the backend.

Topic: Checking Controls on Leave

Example:

After selecting the first text box then go to the properties panel and choose Events. There you can find Leave command.

Double click on Leave method, and you get the codes as below:

Now type here the codes as below:

Now execute it.

You can see that program will not allow you to leave empty the first text box.

Topic: TrackBar and NumbericUpDown Controls

Example-1: TrackBar

It has maximum and minimum values to be represented.

We write the following code:

Now this program will show you a number which will you select from track bar.

Different tries are shown below:

You can change also its orientation from horizontal to vertical as below:

Now the orientation of track bar will become as below:

Example-2: NumbericUpDown

It is similar to track down control.

Topic: Accessing All Controls

Example-1:

Suppose we have several different buttons as shown in the picture below:

Let double click on the “ClickMe” button, and you will get the following method:

Let’s type there the following codes:

This means when you execute this program and after click the ClickMe button then text on every button will become as “Hello” text.

You can see only button4 text has not been changed. That is because it lies inside the groupbox.

Example-2:

Result: Now in time after clicking the ClickMe button then text on every button will become “Hello” text.

Topic: Operator Overloading

Example-1: operator “+”

private void button1_Click(object sender, EventArgs e)

{

Item a1 = new Item();

a1.Price = 5;

Item a2 = new Item();

a2.Price = 2;

Item a3 = a1 + a2;

MessageBox.Show(a3.Price.ToString());

}

class Item

{

public int Price;

}

Result: Error, it cannot add two objects.

Example-2: Now we use operator +.

private void button1_Click(object sender, EventArgs e)

{

Item a1 = new Item();

a1.Price = 5;

Item a2 = new Item();

a2.Price = 2;

Item a3 = a1 + a2;

MessageBox.Show(a3.Price.ToString());

}

class Item

{

public int Price;

public static Item operator +(Item i1, Item i2)

{

Item i3 = new Item();

i3.Price = i1.Price + i2.Price;

return i3;

}

}

Result: 7

Example-3: “==” and “!=” operators

private void button1_Click(object sender, EventArgs e)

{

Item a1 = new Item();

a1.Price = 5;

Item a2 = new Item();

a2.Price = 2;

if(a1==a2)

MessageBox.Show("Same");

else MessageBox.Show("Different");

}

class Item

{

public int Price;

public static bool operator ==(Item i1, Item i2)

{

return (i1.Price==i2.Price)? true: false;

}

}

Result: Error, because “==” operator cannot be used alone without “!=” operator.

Example-4: Now it works properly.

private void button1_Click(object sender, EventArgs e)

{

Item a1 = new Item();

a1.Price = 5;

Item a2 = new Item();

a2.Price = 2;

if(a1==a2)

MessageBox.Show("Same");

else MessageBox.Show("Different");

}

class Item

{

public int Price;

public static bool operator ==(Item i1, Item i2)

{

return (i1.Price==i2.Price)? true: false;

}

public static bool operator !=(Item i1, Item i2)

{

return (i1.Price != i2.Price) ? true : false;

}

}

Result: Different

Example-5: “<” and “>”, “<=” and “>=” operators

Note: < operator cannot work alone without > operator, they work together.

private void button1_Click(object sender, EventArgs e)

{

Item a1 = new Item();

a1.Price = 5;

Item a2 = new Item();

a2.Price = 2;

if(a1>a2)

MessageBox.Show("a1 > a2");

else MessageBox.Show("a1 < a2");

}

class Item

{

public int Price;

public static bool operator <(Item i1, Item i2)

{

return (i1.Price<i2.Price)? true: false;

}

public static bool operator >(Item i1, Item i2)

{

return (i1.Price > i2.Price) ? true : false;

}

}

Result:

Example-6: <= and >= operators, they also work together only.

private void button1_Click(object sender, EventArgs e)

{

Item a1 = new Item();

a1.Price = 5;

Item a2 = new Item();

a2.Price = 5;

if(a1>=a2)

MessageBox.Show("a1 >= a2");

else MessageBox.Show("a1 < a2");

}

class Item

{

public int Price;

public static bool operator <=(Item i1, Item i2)

{

return (i1.Price<=i2.Price)? true: false;

}

public static bool operator >=(Item i1, Item i2)

{

return (i1.Price >= i2.Price) ? true : false;

}

}

Result:

Example-7: “++” operator

private void button1_Click(object sender, EventArgs e)

{

Item a1 = new Item();

a1.Price = 5;

a1++;

MessageBox.Show(a1.Price.ToString());

}

class Item

{

public int Price;

public static Item operator ++(Item i1)

{

i1.Price++;

return i1;

}

}

Result: 6

Example-8: “- -“ operator

private void button1_Click(object sender, EventArgs e)

{

Item a1 = new Item();

a1.Price = 5;

a1--;

MessageBox.Show(a1.Price.ToString());

}

class Item

{

public int Price;

public static Item operator --(Item i1)

{

i1.Price--;

return i1;

}

}

Result: 4

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]