1 / 23

第 7 章 列表、循环和打印

第 7 章 列表、循环和打印. 列表框和组合框. 提供一列表项,用户做出选择. C# 自动添加滚动条. DropDownStyle 属性,供用户输入. 组合框 text 属性,设计可以删除; 列表框 text 属性, 只能 运行时访问. 简单 列表框、简单组合框、下拉组合框、下拉列表框. Combox 的 DropDownStyle 属性. Items 集合. Items.Add Items.Insert. 显示在列表框中得表项集合, Items[1] 索引从 0 开始。 填充 列表:设计或运行期间. Using the Items.Insert Method.

hani
Download Presentation

第 7 章 列表、循环和打印

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第7章 列表、循环和打印

  2. 列表框和组合框 提供一列表项,用户做出选择 C#自动添加滚动条 DropDownStyle属性,供用户输入 组合框text属性,设计可以删除; 列表框text属性,只能运行时访问 简单列表框、简单组合框、下拉组合框、下拉列表框

  3. Combox的DropDownStyle属性

  4. Items集合 Items.Add Items.Insert 显示在列表框中得表项集合,Items[1]索引从0开始。 填充列表:设计或运行期间

  5. Using the Items.Insert Method • Choose location for new item added to list • Items.Insert method • Specify index position for new item • An existing position or end of list • Index position is zero based • First position, use index position = 0 • Inserting an item beyond end of list throws an exception • Do not set list control’s Sorted property to true

  6. Items.Add方法 • 通用方式 • 使用Items.Add方法示例 • 添加文本框内容 Object.Items.Add(ItemValue); schoolsListBox.Items.Add("Harvard"); schoolsListBox.Items.Add("Stanford"); schoolsListBox.Items.Add(schoolsTextBox.Text); majorsComboBox.Items.Add(majorsComboBox.Text); majorsComboBox.Items.Add(majorString); coffeeComboBox.Items.Add(coffeeComboBox.Text); schoolsListBox.Items.Add(schoolTextBox.Text);

  7. Items.Insert方法 索引位置 Object.Items.Insert(IndexPosition, ItemValue); schoolsListBox.Items.Insert(0, "Harvard"); majorsComboBox.Items.Insert(1, majorsComboBox.Text); 要使用Insert方法选择索引位置,不能把列表框的Sorted属性设置为true • 通式 • 方法实例

  8. SelectedIndex属性 突出显示 -1没有任何选项被选定 • 该表项索引编号被存储在SelectedIndex属性中 // Select the fourth item in list. coffeeTypesListBox.SelectedIndex = 3; // Deselect all items in list. coffeeTypesListBox.SelectedIndex = -1;

  9. Items.Count属性 表项数量 示例 totalItemsInteger= itemsListBox.Items.Count; MessageBox.Show(“The number of items in the list is ” + itemsListBox.Items.Count.ToString()); • 总是比最高SelectedIndex大1,下标从0开始 • 例如: 5个表项 • Items.Count = 5 • Highest index = 4 • 可能的SelectedIndex= 0, 1, 2, 3, 4

  10. Referencing the Items Collection - 2 引用当前列表元素,结合使用Items属性和the SelectedIndex属性 selectedFlavorString= flavorListBox.Items[flavorListBox.SelectedIndex].ToString(); 通过引用控件 Text 属性获取选定表项selectedMajorLabel.Text = majorsComboBox.Text; 给特定表项赋值替换以前内容schoolsListBox.Items[0] = “My School”;

  11. Removing an Item from a List • Items.RemoveAt method • Removes an item by index • First list element is 0, last element is Items.Count -1 • An invalid index will cause an IndexOutOfRange exception namesListBox.Items.RemoveAt[0]; schoolsComboBox.Items.RemoveAt[indexInteger]; coffeeComboBox.Items.RemoveAt(coffeeComboBox.SelectedIndex); • Items.Remove method • Looks for the specified string, removes item if string found • No exception is generated if the item is not found namesListBox.Items.Remove[“My School”]; schoolsComboBox.Items.Remove(schoolTextBox.Text); coffeeComboBox.Items.Remove(coffeeComboBox.Text);

  12. 从列表中删除表项 • Items.RemoveAt方法 • 通过下标删除 • 首0, 末尾Items.Count-1 namesListBox.Items.RemoveAt[0]; schoolsComboBox.Items.RemoveAt[indexInteger]; coffeeComboBox.Items.RemoveAt(coffeeComboBox.SelectedIndex); • Items.Remove方法 • 集合中寻找制定字符串 • 找不到也不会抛出异常 namesListBox.Items.Remove[“My School”]; schoolsComboBox.Items.Remove(schoolTextBox.Text); coffeeComboBox.Items.Remove(coffeeComboBox.Text);

  13. 清除列表 // Confirm clearing the majors list. DialogResult responseDialogResult; responseDialogResult = MessageBox.Show("Clear the majors list?", "Clear Majors List", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (responseDialogResult == DialogResult.Yes) { majorsComboBox.Items.Clear(); } Items.Clear方法 删除列表中的全部选项

  14. 列表框和组合框的事件 • SelectedIndexChanged事件 • Occurs when the user makes a selection from a list • TextChanged事件 • Each keystroke generates a TextChanged event • Available in combo boxes not list boxes • Enter 事件 • Occurs when a control receives the focus • Use to make existing text appear selected • Leave 事件 • Fires when a control loses focus • Often used for validating input data

  15. While和do/While循环 循环体至少执行1次 do { //Statement in loop } While(condition); While(condition) { //Statement in loop } totalInteger = 0; while (totalInteger != 0) { // Statements in loop (will never be done). } 基于指定的条件终止,当条件为true时,循环语句继续执行,一般事先不知道循环次数,通式

  16. The while and do/while Loops - 3 • do/while loop • Tests for completion at bottom of loop • Also called a posttest or exit test • Statements inside loop will always execute at least once

  17. 使用bool变量3步 bool itemFoundBoolean = false; while (! itemFoundBoolean) // Loop when condition is false (not true). { // Statement(s) in loop. // Must include a statement that sets itemFoundBoolean to true. } 首先声明一个变量,设置初值 当某种情况出现,把这个变量的值设为true 循环条件判断变量是否为true

  18. For循环 for (intindexInteger = 2; indexInteger <= 100; indexInteger += 2) for (countInteger = startInteger; countInteger < endInteger;countInteger += incrementInteger) for (intcountInteger = 0; countInteger <= coffeeTypeComboBox.Items.Count – 1;countInteger++) for (rateDecimal = 0.05M; rateDecimal < 0.25M; rateDecimal += 0.05M) for (intcountDownInteger = 10; countDownInteger > 0; countDownInteger– –) 初始化、条件、迭代后发生的动作

  19. 退出循环 Break Continue:将控制权传递到循环的最后,然后重新测试循环退出条件。将循环跳到下一次迭代上。

  20. drawString方法:向图形页面发送一行文本 private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { e.Graphics.DrawString("沈群力", new Font("Arial", 36), Brushes.Black, 100, 100); }

  21. ①事件处理方法的顶部,定义字体、行高以及X和Y的坐标①事件处理方法的顶部,定义字体、行高以及X和Y的坐标 ②设置打印的行 ③打印行 ④如果想打印另一行,则使Y坐标递增 ⑤如果要打印多行,则把②③④放入循环中

  22. 打印预览 PrintPreviewDialog

More Related