窗体Test1

我在做东西的时候遇到了一个问题,就是说当点击不同的省份时会在panel里面显示相对应的内容,这样写感觉还可以。就做个备份。

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 Test1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string[] str = {"郑州","信阳","商丘","洛阳","开封" };
        string[] str1 = { "常州","南京","苏州","无锡","泰安"};
        private void button1_Click(object sender, EventArgs e)
        {
         
        }
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            //拆箱,在一个事件里面拆箱控件
            RadioButton rb = (RadioButton)sender;
            //if (rb.Checked)
            //{
            //    MessageBox.Show(rb.Text);
            //}
            //判断按钮的TEXT,应该还有别的好的方法,可惜没找到
            if (rb.Text == "河南省")
            {
                //调用函数
                BangDing(str);
            }
            else if(rb.Text=="江苏省")
            {
                BangDing(str1);
            }
            //弹出提示框
          if(rb.Checked)
          {
              MessageBox.Show(rb.Text);
          }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

         
          
          
        }
        /// <summary>
        /// 添加单选按钮控件
        /// </summary>
        /// <param name="stra"> 数组名,参数</param>
       public void BangDing(string[] stra)
        {
            panel2.Controls.Clear();
           
            for (int i = 0; i < 5; i++)
            {
               
                RadioButton rb = new RadioButton();
                //为新加的控件添加 调用事件,并绑定方法
                rb.CheckedChanged += new EventHandler(radioButton1_CheckedChanged);
                rb.Name = rb + (i + 1).ToString();
                rb.Text = stra[i].ToString();
                rb.Location = new Point(0, 22 * i);
                panel2.Controls.Add(rb);
            }
        }

    
    }
}

posted @ 2011-09-19 16:22  豪迈人生  阅读(186)  评论(4编辑  收藏  举报