📄 testform.cs
字号:
this.label16.Text = "Publisher Date:";
//
// txtBookType
//
this.txtBookType.Location = new System.Drawing.Point(88, 88);
this.txtBookType.Name = "txtBookType";
this.txtBookType.Size = new System.Drawing.Size(184, 20);
this.txtBookType.TabIndex = 14;
this.txtBookType.Text = "";
//
// label15
//
this.label15.Location = new System.Drawing.Point(24, 88);
this.label15.Name = "label15";
this.label15.Size = new System.Drawing.Size(48, 23);
this.label15.TabIndex = 13;
this.label15.Text = "Type:";
//
// txtBookTitle
//
this.txtBookTitle.Location = new System.Drawing.Point(88, 56);
this.txtBookTitle.Multiline = true;
this.txtBookTitle.Name = "txtBookTitle";
this.txtBookTitle.Size = new System.Drawing.Size(248, 20);
this.txtBookTitle.TabIndex = 12;
this.txtBookTitle.Text = "";
//
// label14
//
this.label14.Location = new System.Drawing.Point(24, 56);
this.label14.Name = "label14";
this.label14.Size = new System.Drawing.Size(56, 23);
this.label14.TabIndex = 11;
this.label14.Text = "Title:";
//
// label13
//
this.label13.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label13.Location = new System.Drawing.Point(16, 16);
this.label13.Name = "label13";
this.label13.Size = new System.Drawing.Size(168, 23);
this.label13.TabIndex = 10;
this.label13.Text = "Book Detail";
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuPublisher,
this.menuAuthor,
this.menuTitle});
this.menuItem1.Text = "Add/Remove Group";
//
// menuPublisher
//
this.menuPublisher.Checked = true;
this.menuPublisher.Index = 0;
this.menuPublisher.Text = "Publisher";
this.menuPublisher.Click += new System.EventHandler(this.menuPublisher_Click);
//
// menuAuthor
//
this.menuAuthor.Checked = true;
this.menuAuthor.Index = 1;
this.menuAuthor.Text = "Author";
this.menuAuthor.Click += new System.EventHandler(this.menuAuthor_Click);
//
// menuTitle
//
this.menuTitle.Enabled = false;
this.menuTitle.Index = 2;
this.menuTitle.Text = "Title";
this.menuTitle.Click += new System.EventHandler(this.menuTitle_Click);
//
// TestForm
//
this.AcceptButton = this.btnFind;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(584, 430);
this.Controls.Add(this.panelContainer);
this.Controls.Add(this.splitter2);
this.Controls.Add(this.bookTree);
this.Controls.Add(this.panelTop);
this.Menu = this.mainMenu1;
this.Name = "TestForm";
this.Tag = "Author";
this.Text = "Book Browser";
this.panelTop.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dsBooks)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.bookDataView)).EndInit();
this.panelContainer.ResumeLayout(false);
this.panelAuthor.ResumeLayout(false);
this.panelPublisher.ResumeLayout(false);
this.panelTitle.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new TestForm());
}
public void SetUpDataBinding()
{
int nRows = 0;
try
{
this.accessConn.Open();
OleDbDataAdapter daAuthors = new OleDbDataAdapter (this.cmdGetAuthors);
nRows = daAuthors.Fill (this.dsBooks, "Authors");
}
catch (System.Data.OleDb.OleDbException e)
{
MessageBox.Show (String.Format ("Database Error: {0}", e.Message), "TestControl Application");
}
catch ( System.Exception e)
{
MessageBox.Show (String.Format ("Error: {0}", e.Message), "TestControl Application");
}
this.accessConn.Close();
this.bookDataView = dsBooks.Tables ["Authors"].DefaultView;
this.bookTree.AutoBuildTree = false;
this.bookTree.SetLeafData ("Title", "title", "title_id", 2, 2);
this.bookTree.DataSource = this.bookDataView;
this.bookTree.AddGroup ("Publisher", "pub_id", "pub_name", "pub_id", 0, 0);
this.bookTree.AddGroup ("Author", "au_id", "au_name", "au_id", 1, 3);
this.bookTree.BuildTree();
if ( this.bookTree.Nodes.Count <= 0 )
return;
System.Windows.Forms.Binding myBinding;
this.txtPubName.DataBindings.Add ("Text", bookDataView, "pub_name");
this.txtPubCity.DataBindings.Add ("Text", bookDataView, "pub_City");
this.txtPubState.DataBindings.Add ("Text", bookDataView, "pub_State");
this.txtPubContry.DataBindings.Add ("Text", bookDataView, "pub_Country");
this.lblPublisherID.DataBindings.Add ("Text", bookDataView, "pub_id");
this.txtAuthFirstName.DataBindings.Add ("Text", bookDataView, "au_fname");
this.txtAuthLastName.DataBindings.Add ("Text", bookDataView, "au_lname");
this.txtAuthAddress.DataBindings.Add ("Text", bookDataView, "au_address");
this.txtAuthCity.DataBindings.Add ("Text", bookDataView, "au_city");
this.txtAuthZip.DataBindings.Add ("Text", bookDataView, "au_zip");
this.txtAuthState.DataBindings.Add ("Text", bookDataView, "au_state");
this.lblAuthorID.DataBindings.Add ("Text", bookDataView, "au_id");
this.txtBookTitle.DataBindings.Add ("Text", bookDataView, "title");
this.txtBookTitleNodes.DataBindings.Add ("Text", bookDataView, "title_notes");
this.txtBookType.DataBindings.Add ("Text", bookDataView, "title_type");
this.txtBookYTDSales.DataBindings.Add ("Text", bookDataView, "title_ytd_sales");
this.lblTitleID.DataBindings.Add ("Text", bookDataView, "title_id");
this.imgBookCover.DataBindings.Add ("ImagePath", bookDataView, "title_imagePath");
myBinding = this.txtBookPrice.DataBindings.Add ("Text", bookDataView, "title_price");
myBinding.Format += new ConvertEventHandler(CurrencyFormat);
myBinding = this.txtBookPubDate.DataBindings.Add ("Text", bookDataView, "title_pubdate");
myBinding.Format += new ConvertEventHandler(DateFormat);
myBinding = this.txtBookRoylaty.DataBindings.Add ("Text", bookDataView, "title_royalty");
myBinding.Format += new ConvertEventHandler(PercentFormat);
myBinding = this.txtBookAdvPaid.DataBindings.Add ("Text", bookDataView, "title_advance");
myBinding.Format += new ConvertEventHandler(CurrencyFormat);
String sName = this.bookTree.Tag == null ? "Item" : this.bookTree.Tag.ToString();
this.lblLocate.Text = "Locate " + sName + " By " + sName + " ID: ";
}
public void CurrencyFormat(object sender, ConvertEventArgs e)
{
if ( e.Value is System.DBNull )
return;
e.Value = (Convert.ToDouble(e.Value)).ToString ("C");
}
public void PercentFormat(object sender, ConvertEventArgs e)
{
if ( e.Value is System.DBNull )
return;
double percentValue = Convert.ToDouble (e.Value);
percentValue = percentValue / 100;
e.Value = percentValue.ToString("P0");
}
public void DateFormat(object sender, ConvertEventArgs e)
{
if ( e.Value is System.DBNull )
return;
e.Value = Convert.ToDateTime (e.Value).ToShortDateString();
}
private void btnFind_Click(object sender, System.EventArgs e)
{
if ( this.txtBookTitle.Text.Trim() != String.Empty )
{
TreeNode currentNode = this.bookTree.FindNodeByValue (this.txtBookID.Text.ToUpper());
if ( currentNode != null )
bookTree.SelectedNode = currentNode;
else
{
this.txtBookID.Focus();
this.txtBookID.SelectAll();
}
}
}
private void RebuildTree ()
{
String sSort = "";
int nCheckedCount = 0;
this.bookTree.RemoveAllGroups();
// set groups and sorting string
for (int i=0; i<this.menuItem1.MenuItems.Count; i++)
{
MenuItem menu = this.menuItem1.MenuItems[i];
menu.Enabled = true;
if ( menu.Checked)
{
nCheckedCount ++;
switch (menu.Text)
{
case "Publisher" :
sSort += "pub_id, ";
this.bookTree.AddGroup ("Publisher", "pub_id", "pub_name", "pub_id", 0, 0);
break;
case "Author" :
sSort += "au_name, ";
this.bookTree.AddGroup ("Author", "au_id", "au_name", "au_id", 1, 3);
break;
case "Title" :
sSort += "title_id, ";
this.bookTree.AddGroup ("Title", "title_id", "title", "title_id", 2, 2);
break;
}
}
}
// disable the only unchecked menu item to disallow check it.(It, in fact, is a leaf group)
if ( nCheckedCount == this.menuItem1.MenuItems.Count - 1 )
{
for (int i=0; i<this.menuItem1.MenuItems.Count; i++)
if ( !menuItem1.MenuItems[i].Checked )
this.menuItem1.MenuItems[i].Enabled = false;
}
// determine which group is the leaf group and set the leaf data
if ( sSort.IndexOf ("title_id") < 0 )
this.bookTree.SetLeafData ("Title", "title", "title_id", 2, 2);
else if (sSort.IndexOf ("au_name") < 0 )
this.bookTree.SetLeafData ("Author", "au_name", "au_id", 1, 3);
else
this.bookTree.SetLeafData ("Publisher", "pub_name", "pub_id", 0, 0);
// sort and re-build the tree
this.bookDataView.Sort = sSort + "title_id";
this.bookTree.BuildTree();
String sName = this.bookTree.Tag == null ? "Item" : this.bookTree.Tag.ToString();
this.lblLocate.Text = "Locate " + sName + " By " + sName + " ID: ";
}
private void menuPublisher_Click(object sender, System.EventArgs e)
{
this.menuPublisher.Checked = !this.menuPublisher.Checked;
this.RebuildTree();
}
private void menuAuthor_Click(object sender, System.EventArgs e)
{
this.menuAuthor.Checked = !this.menuAuthor.Checked;
this.RebuildTree();
}
private void menuTitle_Click(object sender, System.EventArgs e)
{
this.menuTitle.Checked = !this.menuTitle.Checked;
this.RebuildTree();
}
private void bookTree_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
ControlLib.dbTreeNode node = (ControlLib.dbTreeNode) e.Node;
// determine which page to display
if ( node != null )
{
switch (node.GroupName)
{
case "Publisher" :
this.panelAuthor.Visible = false;
this.panelTitle.Visible = false;
this.panelPublisher.Visible = true;
break;
case "Author" :
this.panelPublisher.Visible = false;
this.panelTitle.Visible = false;
this.panelAuthor.Visible = true;
break;
case "Title" :
this.panelPublisher.Visible = false;
this.panelAuthor.Visible = false;
this.panelTitle.Visible = true;
break;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -