1)编制Windows Form程序,分别用while、do…while、for、foreach求1~100的和。
主要程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
int i = 1;
int number=0;
while (i < 101)
{
number = number + i;
i++;
}
Console.WriteLine("使用while循环从1加到100的总和是:sum={0}", number);
Console.ReadKey();
i=1;
number=0;
do
{
number = number + i;
i++;
} while (i < 101);
Console.WriteLine("使用do while循环从1加到100的总和是:sum={0}", number);
Console.ReadKey();
number = 0;
for (i = 0; i < 101; i++)
{
number = number + i;
// i++;
}
Console.WriteLine("使用for循环从1加到100的总和是:sum={0}", number);
Console.ReadKey();
i = 1;
number = 0;
int[] a = new int[100];
for (i = 0; i < 100; i++)
a[i] = i+1;
i=0;
foreach(int j in a)
number=number+j;
Console.WriteLine("使用 foreach循环从1加到100的总和是:sum={0}", number);
Console.ReadKey();
}
}
}
2.综合运用label,button, treeview控件,设计类似于windows程序管理器界面,并调试其程序。参考图如下.
主要程序:
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button4_Click(object sender, EventArgs e)
{
this.Close();
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
textBox1.Text=treeView1.SelectedNode.Text;
textBox1.ForeColor=Color.Red;
}
private void button3_Click(object sender, EventArgs e)
{
treeView1.ImageList = null;
}
private void button2_Click(object sender, EventArgs e)
{
treeView1.ImageList = imageList1;
treeView1.ImageIndex = 0;
treeView1.SelectedImageIndex = 0;
}
private void button1_Click(object sender, EventArgs e)
{
if (treeView1.SelectedNode != null)
{
treeView1.Nodes.Remove(treeView1.SelectedNode);
}
else
{
MessageBox.Show("无选择节点");
return ;
}
}
}
}
运行结果:
3)利用combox设计一个四则运算器。如下图所示
主要程序:
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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Focus();
textBox4.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = textBox4.Text = textBox3.Text = "";
// comboBox1.SelectedIndex ;
//comboBox1.text = "";
textBox1.Focus();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
double a, b;
try
{
a = double.Parse(textBox1.Text);
}
catch (System.FormatException)
{
MessageBox.Show("您输入的操作数1不是一个有效的数据");
textBox1.Text = "";
textBox1.Focus();
return;
}
catch (System.OverflowException)
{
MessageBox.Show("您输入的操作数1超出范围");
return;
}
try
{
b = double.Parse(textBox3.Text);
}
catch (System.FormatException)
{
MessageBox.Show("您输入的操作数2不是一个有效的数据");
textBox1.Text = "";
textBox1.Focus();
return;
}
catch (System.OverflowException)
{
MessageBox.Show("您输入的操作数2超出范围");
return;
}
string options = comboBox1.Text;
if (options == "+")
{
// MessageBox.Show(options);
textBox4.Text = (a + b).ToString();
}
else if (options == "-")
{
textBox4.Text = (a - b).ToString();
}
else if (options == "*")
{
textBox4.Text = (a * b).ToString();
}
else if (options == "/")
{
textBox4.Text = (a / b).ToString();
}
else return;
}
}
}
运行结果:
4 ) 设计一程序,实现comBox和listbox中内容的相互切换。
主要程序:
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 WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// comboBox1.DropDownStyle = ComboBoxStyle.Simple;
// listBox1.Items.Clear();
comboBox1.Items.Add("Visual c#.NET程序设计");
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.Items.Count > 0)
{
try
{
string options = comboBox1.Text;
listBox1.Items.Add(options);
comboBox1.Items.Remove(options);
}
catch (Exception)
{
MessageBox.Show("请先选择数据");
return;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
string options = listBox1.Text;
comboBox1.Items.Add(options);
listBox1.Items.Remove(options);
}
}
}
运行结果: