using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace _034_使用goto语句在数组中搜索指定图书 { public partial class Frm_Main : Form { public Frm_Main() { InitializeComponent();//初始化窗体控件 listBox1.Items.AddRange(G_str_array);//将数组元素罗列在listbox空间中,将现有的项添加到listbox1中; } string[] G_str_array = new string[]{"C#范例宝典","C#编程宝典","C#视频学","C#项目开发全程实录","C#项目开发实例自学手册","C#编程词典","C#实战宝典","C#经验技巧宝典","C#入门模式"}; //初始化一个数组,声明一个数组并不代表着会初始化一个数组 private void button1_Click(object sender, EventArgs e) { int i = 0;// define a counter label1: if (G_str_array[i].Contains(textBox1.Text)) { listBox1.SelectedIndex = i; //找到并选中 MessageBox.Show(textBox1.Text+"书已找到","提示"); return; } i++; if (i < G_str_array.Length) goto label1; MessageBox.Show(textBox1.Text+"书未找到","提示"); } private void Frm_Main_Load(object sender, EventArgs e) { listBox1.Items.AddRange(G_str_array); } } }