Home > Net >  Refer to use c # Winform write provinces to choose small programs
Refer to use c # Winform write provinces to choose small programs

Time:09-19

? Write a provincial selection interface, and test, the requirements as follows,

(1) define the drop-down box to store called cmbProvince provinces, define a drop-down box to store called cmbCity city,

(2) when the form loads, is added in the cmbProvince two options, and selected by default in the first paragraph,

(3) when the selected item changes in cmbProvince cmbCity drop-down box displays the corresponding city,



Winform won't write to you have just learnt a great god to write a reference for my



CodePudding user response:

https://www.google.com/search? Ei=HVNuXuvBFsz6 - gTsoof4Bw & amp; Q=Winform E7 + % % 9 c % 81% E5 B8 E9%80% % 82% % 89% % E6 8 b % A9

You are just learning programming? Or just enter the civilized world, primitive man?
Because we assume that a man of the civilized world, at least should be using the search engines,
Look at this issue results available, and the degree of your laziness has come to claim,

CodePudding user response:

Friend, it is suggested that there was a problem first baidu, really can't find the BBS asked, for newcomers baidu questions about c # are full, because sometimes you may ask the question, simple

 
Public partial class Form1: Form
{
String [] province=new string [] {" fujian province ", "zhejiang",};
String [] fujianCity=new string [] {" fuzhou ", "quanzhou", "zhangzhou", "xiamen"};
String [] zhejiangCity=new string [] {" hangzhou ", "jiaxing", "wenzhou", "jinhua"};
Int pro_select=0;
Public _click ()
{
InitializeComponent();
Cmbload ();

}
Void cmbload ()
{
CmbProvince. Items. The Clear ();
CmbProvince. Items. AddRange (province);
CmbProvince. SelectedIndex=0;
CmbCity. Items. The Clear ();
CmbCity. Items. AddRange (fujianCity);
CmbCity. SelectedIndex=0;
}

Private void cmbProvince_SelectedIndexChanged (object sender, EventArgs e)
{
If (cmbProvince. SelectedIndex==0)
{
If (pro_select!=0)
{
CmbCity. Items. The Clear ();
CmbCity. Items. AddRange (fujianCity);
CmbCity. SelectedIndex=0;
Pro_select=0;
}
}
Else if (cmbProvince SelectedIndex==1)
{
If (pro_select!
=1){
CmbCity. Items. The Clear ();
CmbCity. Items. AddRange (zhejiangCity);
CmbCity. SelectedIndex=0;
Pro_select=1;
}
}


CodePudding user response:

Create a new Windows form application, drag the comboBox control,
This is a two-stage linkage drop-down list demo, you can take to consult,
 
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.Data.SqlClient;

The namespace provinces
{
Publicpartialclass Form1: Form
{
Public _click ()
{
InitializeComponent();
}
Private void Form1_Load (object sender, EventArgs e)
{
Using (SqlConnection conn=new SqlConnection (" server=. The database=DBPromary; user id=sa; Password=123456 "))
{
conn.Open();
Using (SqlCommand CMD=conn. CreateCommand ())
{
Cmd.Com mandText="select proName from promary";

Using (SqlDataReader reader=CMD. ExecuteReader ())
{

While (reader. The Read ())
{
String name=reader. Get string (reader. GetOrdinal (" proName "));

Cb1. Items. The Add (name);
}
}
}
}

}



Privatevoid comboBox1_SelectedIndexChanged (object sender, EventArgs e)
{
Cb2. Items. The Clear ();
Using (SqlConnection conn=new SqlConnection (" server=. The database=DBPromary; user id=sa; Password=123456 "))
{
conn.Open();

Using (SqlCommand CMD=conn. CreateCommand ())
{
Cmd.Com mandText="select cityName from city, promary where city. The proID.=promary proID and promary. ProName=@ pname";
CMD. The Parameters. The Add (new SqlParameter (" pname ", cb1. SelectedItem));

Using (SqlDataReader reader=CMD. ExecuteReader ())
{

While (reader. The Read ())
{
String name=reader. Get string (reader. GetOrdinal (" cityName "));

Cb2. Items. The Add (name);
}
}
}
}

}


}
}

CodePudding user response:

Use a dictionary or cache memory, rapid traverse, because provinces, little change, initialized again
  •  Tags:  
  • C#
  • Related