1.19. Displaying a Connection Property Dialog Box
Problem
You want to display a dialog box from an application that lets users create database connections using a Properties dialog box similar to that in the Visual Studio IDE.
Solution
Use a PropertyGrid control.
Follow these steps to create the solution:
Create a C# Windows Forms application,
DisplayConnectionPropertyDialog.Add a
PropertyGridcontrol. Accept all default properties including the namepropertyGrid1. Add an event handler namedonPropertyValueChanged()for thePropertyValueChangedevent.Add a
Labelcontrol below thePropertyGridcontrol. Set itsTextproperty toConnection String:.Add a
TextBoxcontrol below theLabelcontrol. Name itconnectString. Set theReadOnlyproperty totrue. Set theMultilineproperty totrue. Size the control to accommodate about four lines of text.The C# code in Form1.cs in the project
DisplayConnectionPropertyDialogis shown in Example 1-18. You need to add the highlighted code.
Example 1-18. File: Program.cs for DisplayConnectionPropertyDialog solution
using System; using System.Windows.Forms;using System.Data.SqlClient;namespace DisplayConnectionPropertyDialog { public partial class Form1 : Form {SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();public Form1() { InitializeComponent();propertyGrid1.SelectedObject = scsb;} private void onPropertyValueChanged( object s, PropertyValueChangedEventArgs e) {connectString.Text = scsb.ConnectionString;} } }
Run the solution and ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access