C # Network application programming basic exercises and Answers (v)

Source: Internet
Author: User
Tags foreach format bool tostring
Programming | network

  1. Fill in the blanks

1 attributes that enable the control to respond to user interaction are enabled.

2 The property that controls whether the control displays is Visible.

3 If you want to enter a password in the text box, you often specify the PasswordChar property.

4 If the value of a CheckState property is indeterminate at a certain time, the value of its property checked is Unchecked.

5 using a Panel or GroupBox control, you can divide multiple RadioButton controls into two single select groups.

6 If you do not want users to edit text in the ComboBox control, set the property DropDownStyle property value to DropDownList.

7 The property used to set the shortcut key for a menu item in the MenuStrip control is Shortcutkeys.

8 The property used to control how the items are displayed in the ListView control is View.

  2. The Judgment question

1 A control is a collection of properties, methods, and events. To

2 The TextBox control can only be used for the input of single-line text. Wrong

3 through the RichTextBox control can only interoperate with RTF files. Wrong

4 The Checked property of the CheckBox control is always the same as the value of the CheckState property. Wrong

5) The ToolTip component is used to display the custom hint information for a specified control. To

  3. Distinguish the use of TextBox controls, MaskedTextBox controls, and RichTextBox controls.

Troubleshooting

A TextBox control is generally used for editing a single piece of text, can be set to one or more lines of input mode, or can be used as a password input; The MaskedTextBox control is primarily used in a specific format for Tan Jiwen, which will trigger its Maskinputrejected event if the input text does not conform to the format requirement; In addition to the general text editing features of a TextBox, the RichTextBox control can also perform advanced editing of multiple pieces of text, such as changing text, display formatting of paragraphs, finding specific characters and strings in text, and interacting with RTF files.

  4. Briefly describe the difference between a CheckBox control and a RadioButton control.

Troubleshooting

The CheckBox control provides a selection function for the user, typically a choice of two choices, such as "true/false" or "Yes/no", but the control can also be selected by the setting of the property as a choice of three choices. Each checkbox represents a separate selection, and if multiple checkbox controls make up a set of options, you can select multiple simultaneous selections without affecting each other, that is, check. The RadioButton control, which is similar to the CheckBox control feature, is also used to receive a user's choice, but it appears as a single selection, that is, only one group of RadioButton buttons is selected. Once an item is selected, the selected state of the other RadioButton buttons in the same group is automatically cleared.

  5. Design a Windows application with a TextBox control and a button control on the form. Requires that whenever a user clicks a button, the text box adds one line of text to reflect the number of clicks, such as the 3rd click button.

Troubleshooting

1 The form interface as shown in Figure ex5-5-1;

2 The main control properties of the form are set as table ex5-5-1;

Table Ex5-5-1 Primary control properties in a form

Control

Name property

Function

Other properties

TextBox control

TextBox1

Display information

scrollbars=vertical; Multiline=true

Button control

Button1

Trigger Add Informational Event

Button2

Trigger End Add Event

3 Main Event code.

......
int i = 1;
bool Add = true;
... private void button1_click (object sender, EventArgs e)
{
if (ADD) TextBox1.Text + + + i + "click button \ r \ n";
i++;
}
private void Button2_Click (object sender, EventArgs e)
{
Add = false;
}

  6. Write a program, to the ListBox control ListBox1, automatically add 10 number, each account for one item.

Troubleshooting

The main code is as follows:

public partial class Form1:form
{
int m = 1;
......
private void Button1_Click (object sender, EventArgs e)
{
for (int i = m; i < m+10 i++)
{
LISTBOX1.ITEMS.ADD (i);
}
m = m + 10;
}
}

  7. Refer to the "calculator" in the Windows System "accessories" to write a simple calculator on your own. Requirements: the addition and subtraction of integers composed of 0~4 can be implemented.

Troubleshooting

1 The form interface as shown in Figure ex5-5-2;

2) Inputnumber event as Button0, Button1, Button2, Button3, Button4 click event.

The complete code is as follows:

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;
Namespace Exer2
{
public partial class Formcalculator:form
{
Enum Calculatetype {None, add, sub};
Calculatetype mycal = Calculatetype.none;
int x, y;
BOOL Isy = false;
Public Formcalculator ()
{
InitializeComponent ();
Textbox.textalign = HorizontalAlignment.Right;
}
private void Inputnumber (object sender, EventArgs e)
{
Button num = (button) sender;
if (Isy)
{
Textbox.clear ();
Isy = false;
}
TextBox.Text + = num. Text;
}
private void Buttonequal_click (object sender, EventArgs e)
{
y = Convert.ToInt32 (textbox.text);
if (mycal = = Calculatetype.add)
{
TextBox.Text = convert.tostring (x + y);
mycal = Calculatetype.none;
}
if (mycal = = calculatetype.sub)
{
TextBox.Text = convert.tostring (x-y);
mycal = Calculatetype.none;
}
Isy = true;
}
private void Addbutton_click (object sender, EventArgs e)
{
mycal = Calculatetype.add;
x = Convert.ToInt32 (TextBox.Text);
Isy = true;
}
private void Buttonsub_click (object sender, EventArgs e)
{
mycal = calculatetype.sub;
x = Convert.ToInt32 (TextBox.Text);
Isy = true;
}
private void Buttonclear_click (object sender, EventArgs e)
{
TextBox.Text = "";
mycal = Calculatetype.none;
Isy = false;
}
}
}

  8. Try using the TreeView, ListView, and other controls to implement a document management program similar to "explorer" for viewing files under the C:\Documents and Settings directory.

Troubleshooting

1 Create a new project named Windowscontrolsexercise, rename the file Form1.cs to Explorer.cs in Solution Explorer, and set the Form1 form's Text property to "explorer".

2 Add a SplitContainer control, a ImageList control, a TreeView control, a ListView control to the form, and the page layout and control properties as shown in Figure ex5-5-3.

3 In Solution Explorer, add the two icon files in the ImageList1 control to the application directory, named Folder.ico and Doc.ico, respectively.

4 Add the namespace in the Explorer.cs code file: Using System.IO and add the constructor code as follows:

Public Explorer ()
{
InitializeComponent ();
Populatetreeview ();
}
private void Populatetreeview ()
{
TreeNode RootNode;
DirectoryInfo info = new DirectoryInfo (@ "C:\Documents and Settings");
if (info. Exists)
{
RootNode = new TreeNode (info. Name);
Rootnode.tag = info;
GetDirectories (info. GetDirectories (), rootnode);
TREEVIEW1.NODES.ADD (RootNode);
}
}
private void GetDirectories (directoryinfo[] subdirs, TreeNode nodetoaddto)
{
TreeNode Anode;
Directoryinfo[] Subsubdirs;
foreach (DirectoryInfo subdir in Subdirs)
{
Anode = new TreeNode (subdir.name, 0, 0);
Anode.tag = SubDir;
Anode.imagekey = "folder";
Subsubdirs = Subdir.getdirectories ();
if (subsubdirs.length!= 0)
{
GetDirectories (subsubdirs, anode);
}
NODETOADDTO.NODES.ADD (anode);
}
}
5 Add the TreeView1 NodeMouseClick event so that when you click a node in TreeView1, the contents of that node are used to populate the listView1.
private void Treeview1_nodemouseclick (object sender, TreeNodeMouseClickEventArgs e)
{
TreeNode newselected = E.node;
ListView1.Items.Clear ();
DirectoryInfo nodedirinfo = (directoryinfo) Newselected.tag;
Listviewitem.listviewsubitem[] SubItems;
ListViewItem item = NULL;
foreach (DirectoryInfo dir in Nodedirinfo.getdirectories ())
{
item = new ListViewItem (dir. Name, 0);
SubItems = new listviewitem.listviewsubitem[]
{New Listviewitem.listviewsubitem (item, "Directory"),
New Listviewitem.listviewsubitem (Item,
Dir. Lastaccesstime.toshortdatestring ())};
Item. Subitems.addrange (subitems);
LISTVIEW1.ITEMS.ADD (item);
}
foreach (FileInfo file in Nodedirinfo.getfiles ())
{
item = new ListViewItem (file. Name, 1);
SubItems = new listviewitem.listviewsubitem[]
{New Listviewitem.listviewsubitem (item, "File"),
New Listviewitem.listviewsubitem (Item,
File. Lastaccesstime.toshortdatestring ())};
Item. Subitems.addrange (subitems);
LISTVIEW1.ITEMS.ADD (item);
}
Listview1.autoresizecolumns (columnheaderautoresizestyle.headersize);
}

6) compile and execute by key.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.