As seen with the combo box, to limit the number of strings the user can select, you can use a domain up-down. When the user has made an item selection from a domain up-down, you can get that item pass its Text value to the column. Here is an example: using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SqlClient;
public class Exercise : System.Windows.Forms.Form
{
Button btnCreateDatabase;
DomainUpDown dudFlavors;
Button btnCreateOrder;
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
btnCreateDatabase = new Button();
btnCreateDatabase.Text = "Create Database";
btnCreateDatabase.Width = 120;
btnCreateDatabase.Location = new Point(12, 12);
btnCreateDatabase.Click += new EventHandler(btnCreateDatabaseClick);
btnCreateOrder = new Button();
btnCreateOrder.Text = "Create Order";
btnCreateOrder.Location = new Point(150, 12);
btnCreateOrder.Width = 100;
btnCreateOrder.Click += new EventHandler(btnCreateOrderClick);
dudFlavors = new DomainUpDown();
dudFlavors.Location = new Point(12, 46);
dudFlavors.Items.Add("Chunky Butter");
dudFlavors.Items.Add("Chocolate Chip");
dudFlavors.Items.Add("Caramel Au Lait");
dudFlavors.Items.Add("Cream of Cocoa");
dudFlavors.Items.Add("Chocolate Cookie");
Text = "Clarksville Ice Cream";
Controls.Add(btnCreateOrder);
Controls.Add(btnCreateDatabase);
Controls.Add(dudFlavors);
StartPosition = FormStartPosition.CenterScreen;
dudFlavors.Width = this.Width - 30;
dudFlavors.Height = this.Height - 80;
dudFlavors.Anchor = AnchorStyles.Left | AnchorStyles.Top |
AnchorStyles.Right | AnchorStyles.Bottom;
}
private void btnCreateDatabaseClick(object sender, EventArgs e)
{
using (SqlConnection cntExercise =
new SqlConnection("Data Source='EXPRESSION'; " +
"Integrated Security='SSPI';"))
{
SqlCommand cmdExercise =
new SqlCommand("IF EXISTS (SELECT name " +
"FROM sys.databases WHERE name = N'ClarksvilleIceCream1' " +
") " +
"DROP DATABASE ClarksvilleIceCream1; " +
"CREATE DATABASE ClarksvilleIceCream1", cntExercise);
cntExercise.Open();
cmdExercise.ExecuteNonQuery();
}
using (SqlConnection cntExercise =
new SqlConnection("Data Source='EXPRESSION'; " +
"Database='ClarksvilleIceCream1'; " +
"Integrated Security='SSPI';"))
{
SqlCommand cmdExercise =
new SqlCommand("CREATE TABLE IceCreamOrders " +
"(" +
" OrderNumber int," +
" Flavors nvarchar(40)," +
");", cntExercise);
cntExercise.Open();
cmdExercise.ExecuteNonQuery();
}
}
void btnCreateOrderClick(object sender, EventArgs e)
{
using (SqlConnection cntExercise =
new SqlConnection("Data Source='EXPRESSION';" +
"Database='ClarksvilleIceCream1';" +
"Integrated Security=yes;"))
{
SqlCommand cmdExercise =
new SqlCommand("INSERT INTO IceCreamOrders " +
"VALUES(1, '" + dudFlavors.Text + "');",
cntExercise);
cntExercise.Open();
cmdExercise.ExecuteNonQuery();
MessageBox.Show("The ice cream order has been created.",
"Clarksville Ic eCream",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
public class Program
{
[STAThread]
static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}
A domain up-down is primarily a list-based control like any other, like a combo box. This means that, to populate a domain up-down control, you can use the exact same rules of a combo box, by calling the Add() method of its Items property. Here are examples: using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SqlClient;
public class Exercise : System.Windows.Forms.Form
{
Button btnCreateDatabase;
DomainUpDown dudFlavors;
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
btnCreateDatabase = new Button();
btnCreateDatabase.Text = "Create Database";
btnCreateDatabase.Width = 120;
btnCreateDatabase.Location = new Point(12, 12);
btnCreateDatabase.Click += new EventHandler(btnCreateDatabaseClick);
dudFlavors = new DomainUpDown();
dudFlavors.Location = new Point(12, 46);
Text = "Clarksville Ice Cream";
Controls.Add(btnCreateDatabase);
Controls.Add(dudFlavors);
StartPosition = FormStartPosition.CenterScreen;
dudFlavors.Width = this.Width - 30;
dudFlavors.Height = this.Height - 80;
dudFlavors.Anchor = AnchorStyles.Left | AnchorStyles.Top |
AnchorStyles.Right | AnchorStyles.Bottom;
}
private void btnCreateDatabaseClick(object sender, EventArgs e)
{
using (SqlConnection cntExercise =
new SqlConnection("Data Source=(local); " +
"Integrated Security='SSPI';"))
{
SqlCommand cmdExercise =
new SqlCommand("IF EXISTS (SELECT name " +
"FROM sys.databases WHERE name = N'ClarksvilleIceCream1' " +
") " +
"DROP DATABASE ClarksvilleIceCream1; " +
"CREATE DATABASE ClarksvilleIceCream1", cntExercise);
cntExercise.Open();
cmdExercise.ExecuteNonQuery();
}
using (SqlConnection cntExercise =
new SqlConnection("Data Source=(local); " +
"Database='ClarksvilleIceCream1'; " +
"Integrated Security='SSPI';"))
{
SqlCommand cmdExercise =
new SqlCommand("CREATE TABLE Flavors " +
"(" +
" FlavorCode nvarchar(5) not null," +
" Flavor nvarchar(50) not null," +
" Notes nvarchar(max)," +
");", cntExercise);
cntExercise.Open();
cmdExercise.ExecuteNonQuery();
}
using (SqlConnection cntExercise =
new SqlConnection("Data Source=(local);" +
"Database='ClarksvilleIceCream1';" +
"Integrated Security=yes;"))
{
SqlCommand cmdExercise =
new SqlCommand("INSERT INTO Flavors(FlavorCode, Flavor) " +
"VALUES(N'VNLLA', N'French Vanilla')," +
" (N'CHRCK', N'Cherry Coke')," +
" (N'BTRPC', N'Butter Pecan')," +
" (N'CHBTR', N'Chunky Butter')," +
" (N'CHCCP', N'Chocolate Chip')," +
" (N'CRMAL', N'Caramel Au Lait')," +
" (N'CRMCC', N'Cream of Cocoa')," +
" (N'CHLCK', N'Chocolate Cookie')," +
" (N'ORGSB', N'Organic Strawberry')," +
" (N'CHBRN', N'Chocolate Brownies');",
cntExercise);
cntExercise.Open();
cmdExercise.ExecuteNonQuery();
MessageBox.Show("The database has been created.",
"Clarksville Ice Cream",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
using (SqlConnection cntExercise =
new SqlConnection("Data Source=(local);" +
"Database='ClarksvilleIceCream1';" +
"Integrated Security=SSPI;"))
{
SqlCommand cmdExercise = new SqlCommand("SELECT ALL * FROM Flavors;", cntExercise);
cntExercise.Open();
SqlDataReader rdrExercise = cmdExercise.ExecuteReader();
while (rdrExercise.Read())
{
dudFlavors.Items.Add(rdrExercise[1].ToString());
}
}
}
}
public class Program
{
[STAThread]
static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}
|
|||||||
|
|
||
| Home | Copyright © 2008-2026, FunctionX | |
|
|
||