c#课堂作业对话窗口,列表框

如下图所示,随机生成多个 不重复 的随机数(整数,100以下),个数可以通过对话框窗口获取;生成数据后,按 降序 将其显示在ListBox控件中(每次只能选择一项),按钮Up和Down分别上调和下调选择项;

c#课堂作业对话窗口,列表框c#课堂作业对话窗口,列表框


//Author:June
//Form1.cs  
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace class2
{
    public partial class Form1 : Form
    {
        private int num;//个数
        public Form1()
        {
            InitializeComponent();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            if (form2.ShowDialog() == DialogResult.OK)
            {
                this.textBox1.Text = form2.Number.ToString();
                this.num = form2.Number;
            }
        }
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Hashtable ht = new Hashtable();
            Random rm = new Random();
            for (int i = 0; ht.Count < this.num; i++)
            {
                int nValue = rm.Next(100);
                if (!ht.ContainsValue(nValue) && nValue != 0)
                {
                    ht.Add(nValue, nValue);
                    Console.WriteLine(nValue.ToString());
                }
            }
            ArrayList akeys = new ArrayList(ht.Keys);
            akeys.Sort();
            foreach (int skey in akeys)
            {
                Console.Write(skey.ToString() + " ");
           Console.WriteLine(ht[skey]);
           this.listBox1.Items.Add ( skey.ToString() +"\n");
            }

        }
        private void button3_Click(object sender, EventArgs e)
        {
            string selectedItem = listBox1.SelectedItem.ToString();//被选择的项的具体内容
            if (this.listBox1.Items.Contains(selectedItem))
            {
                int selectedNum = listBox1.SelectedIndex;//返回被选择的项号
                listBox1.SetSelected(selectedNum, true);
                if (this.listBox1.GetSelected(selectedNum))
                {
                    listBox1.Items.Insert(selectedNum - 1, selectedItem);//在被选择项的前一项之前插入
                    listBox1.Items.RemoveAt(selectedNum + 1);
                    listBox1.SetSelected(selectedNum, false);
                }
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            string selectedItem = listBox1.SelectedItem.ToString();//被选择的项的具体内容
            if (this.listBox1.Items.Contains(selectedItem))
            {
                int selectedNum = listBox1.SelectedIndex;//返回被选择的项号
                listBox1.SetSelected(selectedNum, true);
                if (this.listBox1.GetSelected(selectedNum))
                {
                    listBox1.Items.Insert(selectedNum +2, selectedItem);//在被选择项的后一项之前插入
                    listBox1.Items.RemoveAt(selectedNum );
                    listBox1.SetSelected(selectedNum, false);
                }
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }     
    }
    }

界面:

c#课堂作业对话窗口,列表框

c#课堂作业对话窗口,列表框

c#课堂作业对话窗口,列表框

//Author:June
//Form2.cs  
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace class2
{
    public partial class Form2 : Form
    {
        //读、写数值个数
        private int number;
        public int Number
        {
            get { return number; }
            set
            {
                if (value > 0)
                    number = value;
                else
                    number = 0;
            }
        }
     
        public Form2()
        {
            InitializeComponent();
        }
        private void Form2_Load(object sender, EventArgs e)
        {
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.Number = int.Parse(this.textBox1.Text);
            this.DialogResult = DialogResult.OK;
        }
    }
}

你可能感兴趣的:(c#课堂作业对话窗口,列表框)