1using LuceneEngine.Index; 2using LuceneEngine.Search; 3using System; 4using System.Collections; 5using System.ComponentModel; 6using System.Data; 7using System.Diagnostics; 8using System.Drawing; 9using System.IO; 10using System.Windows.Forms; 11 12namespace LuceneUI.frmMain 13{ 14 public class frmMain : System.Windows.Forms.Form 15 { 16 #region Controls ... 17 18 19 private System.Windows.Forms.Label lblFilesLocation; 20 private System.Windows.Forms.TextBox txtFilesLocation; 21 private System.Windows.Forms.Label lblIndexLocation; 22 private System.Windows.Forms.TextBox txtIndexLocation; 23 private System.Windows.Forms.Label lblTotalTime; 24 private System.Windows.Forms.Button cmdStart; 25 private System.Windows.Forms.GroupBox grpIndex; 26 private System.Windows.Forms.GroupBox grpSearch; 27 private System.Windows.Forms.Label lblSearchFor; 28 private System.Windows.Forms.TextBox txtSearchFor; 29 private System.Windows.Forms.Button cmdSearch; 30 private System.Windows.Forms.Label lblNumHits; 31 private System.Windows.Forms.DataGrid dgResults; 32 private System.Windows.Forms.StatusBar statAll; 33 private System.Windows.Forms.Button cmdStop; 34 private System.Windows.Forms.ListBox lstErrors; 35 private System.Windows.Forms.Label lblLastMod; 36 private System.Windows.Forms.TextBox txtLastModFrom; 37 private System.Windows.Forms.Label lblTo; 38 private System.Windows.Forms.TextBox txtLastModTo; 39 private System.Windows.Forms.FolderBrowserDialog fldbrwsdiag; 40 private System.Windows.Forms.Button cmdFilesDir; 41 private System.Windows.Forms.Button cmdIndexDir; 42 private System.Windows.Forms.GroupBox grpErrors; 43 #endregion 44 #region Constructor ... 45 46 47 public frmMain() 48 { 49 // Required for Windows Form Designer support 50 InitializeComponent(); 51 52 // Add any constructor code after InitializeComponent call 53 } 54 55 protected override void Dispose( bool disposing ) 56 { 57 if( disposing ) 58 { 59 //if (components != null) 60 //{ 61 // components.Dispose(); 62 //} 63 } 64 base.Dispose( disposing ); 65 } 66 #endregion 67 #region Windows Form Designer generated code ... 68 69 /// <summary> 70 /// Required method for Designer support - do not modify 71 /// the contents of this method with the code editor. 72 /// </summary> 73 private void InitializeComponent() 74 { 75 this.grpIndex = new System.Windows.Forms.GroupBox(); 76 this.cmdIndexDir = new System.Windows.Forms.Button(); 77 this.cmdFilesDir = new System.Windows.Forms.Button(); 78 this.cmdStop = new System.Windows.Forms.Button(); 79 this.txtFilesLocation = new System.Windows.Forms.TextBox(); 80 this.txtIndexLocation = new System.Windows.Forms.TextBox(); 81 this.cmdStart = new System.Windows.Forms.Button(); 82 this.lblTotalTime = new System.Windows.Forms.Label(); 83 this.lblIndexLocation = new System.Windows.Forms.Label(); 84 this.lblFilesLocation = new System.Windows.Forms.Label(); 85 this.lstErrors = new System.Windows.Forms.ListBox(); 86 this.grpSearch = new System.Windows.Forms.GroupBox(); 87 this.txtLastModTo = new System.Windows.Forms.TextBox(); 88 this.lblTo = new System.Windows.Forms.Label(); 89 this.txtLastModFrom = new System.Windows.Forms.TextBox(); 90 this.lblLastMod = new System.Windows.Forms.Label(); 91 this.dgResults = new System.Windows.Forms.DataGrid(); 92 this.txtSearchFor = new System.Windows.Forms.TextBox(); 93 this.lblSearchFor = new System.Windows.Forms.Label(); 94 this.cmdSearch = new System.Windows.Forms.Button(); 95 this.lblNumHits = new System.Windows.Forms.Label(); 96 this.statAll = new System.Windows.Forms.StatusBar(); 97 this.grpErrors = new System.Windows.Forms.GroupBox(); 98 this.fldbrwsdiag = new System.Windows.Forms.FolderBrowserDialog(); 99 this.grpIndex.SuspendLayout(); 100 this.grpSearch.SuspendLayout(); 101 ((System.ComponentModel.ISupportInitialize)(this.dgResults)).BeginInit(); 102 this.grpErrors.SuspendLayout(); 103 this.SuspendLayout(); 104 // 105 // grpIndex 106 // 107 this.grpIndex.Controls.Add(this.cmdIndexDir); 108 this.grpIndex.Controls.Add(this.cmdFilesDir); 109 this.grpIndex.Controls.Add(this.cmdStop); 110 this.grpIndex.Controls.Add(this.txtFilesLocation); 111 this.grpIndex.Controls.Add(this.txtIndexLocation); 112 this.grpIndex.Controls.Add(this.cmdStart); 113 this.grpIndex.Controls.Add(this.lblTotalTime); 114 this.grpIndex.Controls.Add(this.lblIndexLocation); 115 this.grpIndex.Controls.Add(this.lblFilesLocation); 116 this.grpIndex.Location = new System.Drawing.Point(4, 6); 117 this.grpIndex.Name = "grpIndex"; 118 this.grpIndex.Size = new System.Drawing.Size(680, 96); 119 this.grpIndex.TabIndex = 0; 120 this.grpIndex.TabStop = false; 121 this.grpIndex.Text = "Index Files"; 122 // 123 // cmdIndexDir 124 // 125 this.cmdIndexDir.Location = new System.Drawing.Point(638, 43); 126 this.cmdIndexDir.Name = "cmdIndexDir"; 127 this.cmdIndexDir.Size = new System.Drawing.Size(24, 20); 128 this.cmdIndexDir.TabIndex = 7; 129 this.cmdIndexDir.Text = "..."; 130 this.cmdIndexDir.Click += new System.EventHandler(this.cmdIndexDir_Click); 131 // 132 // cmdFilesDir 133 // 134 this.cmdFilesDir.Location = new System.Drawing.Point(638, 20); 135 this.cmdFilesDir.Name = "cmdFilesDir"; 136 this.cmdFilesDir.Size = new System.Drawing.Size(24, 20); 137 this.cmdFilesDir.TabIndex = 6; 138 this.cmdFilesDir.Text = "..."; 139 this.cmdFilesDir.Click += new System.EventHandler(this.cmdFilesDir_Click); 140 // 141 // cmdStop 142 // 143 this.cmdStop.Enabled = false; 144 this.cmdStop.Location = new System.Drawing.Point(147, 66); 145 this.cmdStop.Name = "cmdStop"; 146 this.cmdStop.Size = new System.Drawing.Size(54, 20); 147 this.cmdStop.TabIndex = 5; 148 this.cmdStop.Text = "Stop"; 149 this.cmdStop.Click += new System.EventHandler(this.cmdStop_Click); 150 // 151 // txtFilesLocation 152 // 153 this.txtFilesLocation.Location = new System.Drawing.Point(88, 20); 154 this.txtFilesLocation.Name = "txtFilesLocation"; 155 this.txtFilesLocation.Size = new System.Drawing.Size(544, 20); 156 this.txtFilesLocation.TabIndex = 0; 157 this.txtFilesLocation.Text = "C:\\"; 158 // 159 // txtIndexLocation 160 // 161 this.txtIndexLocation.Location = new System.Drawing.Point(88, 42); 162 this.txtIndexLocation.Name = "txtIndexLocation"; 163 this.txtIndexLocation.Size = new System.Drawing.Size(544, 20); 164 this.txtIndexLocation.TabIndex = 1; 165 this.txtIndexLocation.Text = "C:\\"; 166 // 167 // cmdStart 168 // 169 this.cmdStart.Location = new System.Drawing.Point(89, 66); 170 this.cmdStart.Name = "cmdStart"; 171 this.cmdStart.Size = new System.Drawing.Size(54, 20); 172 this.cmdStart.TabIndex = 2; 173 this.cmdStart.Text = "Start"; 174 this.cmdStart.Click += new System.EventHandler(this.cmdStart_Click); 175 // 176 // lblTotalTime 177 // 178 this.lblTotalTime.AutoSize = true; 179 this.lblTotalTime.Location = new System.Drawing.Point(206, 68); 180 this.lblTotalTime.Name = "lblTotalTime"; 181 this.lblTotalTime.Size = new System.Drawing.Size(67, 16); 182 this.lblTotalTime.TabIndex = 4; 183 this.lblTotalTime.Text = "Total Time..."; 184 // 185 // lblIndexLocation 186 // 187 this.lblIndexLocation.AutoSize = true; 188 this.lblIndexLocation.Location = new System.Drawing.Point(10, 46); 189 this.lblIndexLocation.Name = "lblIndexLocation"; 190 this.lblIndexLocation.Size = new System.Drawing.Size(81, 16); 191 this.lblIndexLocation.TabIndex = 2; 192 this.lblIndexLocation.Text = "Index Location:"; 193 // 194 // lblFilesLocation 195 // 196 this.lblFilesLocation.AutoSize = true; 197 this.lblFilesLocation.Location = new System.Drawing.Point(14, 22); 198 this.lblFilesLocation.Name = "lblFilesLocation"; 199 this.lblFilesLocation.Size = new System.Drawing.Size(77, 16); 200 this.lblFilesLocation.TabIndex = 0; 201 this.lblFilesLocation.Text = "Files Location:"; 202 // 203 // lstErrors 204 // 205 this.lstErrors.Location = new System.Drawing.Point(10, 22); 206 this.lstErrors.Name = "lstErrors"; 207 this.lstErrors.Size = new System.Drawing.Size(656, 82); 208 this.lstErrors.TabIndex = 3; 209 // 210 // grpSearch 211 // 212 this.grpSearch.Controls.Add(this.txtLastModTo); 213 this.grpSearch.Controls.Add(this.lblTo); 214 this.grpSearch.Controls.Add(this.txtLastModFrom); 215 this.grpSearch.Controls.Add(this.lblLastMod); 216 this.grpSearch.Controls.Add(this.dgResults); 217 this.grpSearch.Controls.Add(this.txtSearchFor); 218 this.grpSearch.Controls.Add(this.lblSearchFor); 219 this.grpSearch.Controls.Add(this.cmdSearch); 220 this.grpSearch.Controls.Add(this.lblNumHits); 221 this.grpSearch.Location = new System.Drawing.Point(6, 106); 222 this.grpSearch.Name = "grpSearch"; 223 this.grpSearch.Size = new System.Drawing.Size(678, 246); 224 this.grpSearch.TabIndex = 1; 225 this.grpSearch.TabStop = false; 226 this.grpSearch.Text = "Search"; 227 // 228 // txtLastModTo 229 // 230 this.txtLastModTo.Location = new System.Drawing.Point(174, 42); 231 this.txtLastModTo.Name = "txtLastModTo"; 232 this.txtLastModTo.Size = new System.Drawing.Size(68, 20); 233 this.txtLastModTo.TabIndex = 12; 234 this.txtLastModTo.Text = ""; 235 // 236 // lblTo 237 // 238 this.lblTo.AutoSize = true; 239 this.lblTo.Location = new System.Drawing.Point(158, 44); 240 this.lblTo.Name = "lblTo"; 241 this.lblTo.Size = new System.Drawing.Size(14, 16); 242 this.lblTo.TabIndex = 11; 243 this.lblTo.Text = "to"; 244 // 245 // txtLastModFrom 246 // 247 this.txtLastModFrom.Location = new System.Drawing.Point(88, 42); 248 this.txtLastModFrom.Name = "txtLastModFrom"; 249 this.txtLastModFrom.Size = new System.Drawing.Size(68, 20); 250 this.txtLastModFrom.TabIndex = 10; 251 this.txtLastModFrom.Text = "1/1/1990"; 252 // 253 // lblLastMod 254 // 255 this.lblLastMod.AutoSize = true; 256 this.lblLastMod.Location = new System.Drawing.Point(16, 44); 257 this.lblLastMod.Name = "lblLastMod"; 258 this.lblLastMod.Size = new System.Drawing.Size(75, 16); 259 this.lblLastMod.TabIndex = 9; 260 this.lblLastMod.Text = "Last Modified:"; 261 // 262 // dgResults 263 // 264 this.dgResults.DataMember = ""; 265 this.dgResults.HeaderForeColor = System.Drawing.SystemColors.ControlText; 266 this.dgResults.Location = new System.Drawing.Point(10, 70); 267 this.dgResults.Name = "dgResults"; 268 this.dgResults.Size = new System.Drawing.Size(660, 168); 269 this.dgResults.TabIndex = 8; 270 this.dgResults.DoubleClick += new System.EventHandler(this.dgResults_DoubleClick); 271 // 272 // txtSearchFor 273 // 274 this.txtSearchFor.Location = new System.Drawing.Point(88, 20); 275 this.txtSearchFor.Name = "txtSearchFor"; 276 this.txtSearchFor.Size = new System.Drawing.Size(292, 20); 277 this.txtSearchFor.TabIndex = 4; 278 this.txtSearchFor.Text = "C#"; 279 // 280 // lblSearchFor 281 // 282 this.lblSearchFor.AutoSize = true; 283 this.lblSearchFor.Location = new System.Drawing.Point(46, 22); 284 this.lblSearchFor.Name = "lblSearchFor"; 285 this.lblSearchFor.Size = new System.Drawing.Size(43, 16); 286 this.lblSearchFor.TabIndex = 1; 287 this.lblSearchFor.Text = "Search:"; 288 // 289 // cmdSearch 290 // 291 this.cmdSearch.Location = new System.Drawing.Point(384, 20); 292 this.cmdSearch.Name = "cmdSearch"; 293 this.cmdSearch.Size = new System.Drawing.Size(54, 20); 294 this.cmdSearch.TabIndex = 5; 295 this.cmdSearch.Text = "Search"; 296 this.cmdSearch.Click += new System.EventHandler(this.cmdSearch_Click); 297 // 298 // lblNumHits 299 // 300 this.lblNumHits.AutoSize = true; 301 this.lblNumHits.Location = new System.Drawing.Point(448, 22); 302 this.lblNumHits.Name = "lblNumHits"; 303 this.lblNumHits.Size = new System.Drawing.Size(60, 16); 304 this.lblNumHits.TabIndex = 7; 305 this.lblNumHits.Text = "Num Hits..."; 306 // 307 // statAll 308 // 309 this.statAll.Location = new System.Drawing.Point(0, 471); 310 this.statAll.Name = "statAll"; 311 this.statAll.Size = new System.Drawing.Size(690, 22); 312 this.statAll.SizingGrip = false; 313 this.statAll.TabIndex = 2; 314 // 315 // grpErrors 316 // 317 this.grpErrors.Controls.Add(this.lstErrors); 318 this.grpErrors.Location = new System.Drawing.Point(8, 356); 319 this.grpErrors.Name = "grpErrors"; 320 this.grpErrors.Size = new System.Drawing.Size(676, 114); 321 this.grpErrors.TabIndex = 3; 322 this.grpErrors.TabStop = false; 323 this.grpErrors.Text = "Errors"; 324 // 325 // frmMain 326 // 327 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 328 this.ClientSize = new System.Drawing.Size(690, 493); 329 this.Controls.Add(this.grpErrors); 330 this.Controls.Add(this.statAll); 331 this.Controls.Add(this.grpSearch); 332 this.Controls.Add(this.grpIndex); 333 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D; 334 this.MaximizeBox = false; 335 this.Name = "frmMain"; 336 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 337 this.Text = "Indexing/Search Engine"; 338 this.Load += new System.EventHandler(this.frmMain_Load); 339 this.grpIndex.ResumeLayout(false); 340 this.grpSearch.ResumeLayout(false); 341 ((System.ComponentModel.ISupportInitialize)(this.dgResults)).EndInit(); 342 this.grpErrors.ResumeLayout(false); 343 this.ResumeLayout(false); 344 345 } 346 #endregion 347 #region Private Vars ... 348 349 private const string APPTITLE = "Indexing/Search Engine"; 350 private IndexFiles mobjIndexer; 351 #endregion 352 #region Main ... 353 354 [STAThread] 355 static void Main() 356 { 357 Application.Run(new frmMain()); 358 } 359 #endregion 360 #region frmMain_Load ... 361 362 private void frmMain_Load(object sender, System.EventArgs e) 363 { 364 //Initialize form fields 365 lblTotalTime.Text = ""; 366 lblNumHits.Text = ""; 367 statAll.Text = ""; 368 txtLastModTo.Text = DateTime.Today.AddDays(1).ToShortDateString(); 369 } 370 #endregion 371 #region cmdFilesDir_Click ... 372 373 private void cmdFilesDir_Click(object sender, System.EventArgs e) 374 { 375 //Allow user to select the Files Location 376 fldbrwsdiag.RootFolder = System.Environment.SpecialFolder.MyComputer; 377 if (fldbrwsdiag.ShowDialog() == DialogResult.OK) 378 txtFilesLocation.Text = fldbrwsdiag.SelectedPath; 379 } 380 #endregion 381 #region cmdIndexDir_Click ... 382 383 private void cmdIndexDir_Click(object sender, System.EventArgs e) 384 { 385 //Allow user to select the Index Location 386 fldbrwsdiag.RootFolder = System.Environment.SpecialFolder.MyComputer; 387 if (fldbrwsdiag.ShowDialog() == DialogResult.OK) 388 txtIndexLocation.Text = fldbrwsdiag.SelectedPath; 389 } 390 #endregion 391 #region cmdStart_Click ... 392 393 private void cmdStart_Click(object sender, System.EventArgs e) 394 { 395 Cursor = System.Windows.Forms.Cursors.WaitCursor; 396 //Enable/disable controls 397 txtFilesLocation.Enabled = false; 398 txtIndexLocation.Enabled = false; 399 cmdStart.Enabled = false; 400 cmdStop.Enabled = true; 401 grpSearch.Enabled = false; 402 grpErrors.Enabled = false; 403 404 //Initialize controls 405 lstErrors.Items.Clear(); 406 lblTotalTime.Text = ""; 407 statAll.Text = "Starting up..."; statAll.Refresh(); 408 409 //Trim folder paths 410 txtFilesLocation.Text = txtFilesLocation.Text.Trim(); 411 txtIndexLocation.Text = txtIndexLocation.Text.Trim(); 412 413 //Validate input data 414 if (txtFilesLocation.Text.Length == 0) 415 { 416 lstErrors.Items.Add("Missing file/directory to index."); 417 } 418 else if (txtIndexLocation.Text.Length == 0) 419 { 420 lstErrors.Items.Add("Missing index directory location."); 421 } 422 else 423 { 424 //Ensure slashes are at the end of the folder paths 425 if (txtFilesLocation.Text.Substring(txtFilesLocation.Text.Length - 1, 1) != "\\") 426 {txtFilesLocation.Text = txtFilesLocation.Text + "\\";} 427 if (txtIndexLocation.Text.Substring(txtIndexLocation.Text.Length - 1, 1) != "\\") 428 {txtIndexLocation.Text = txtIndexLocation.Text + "\\";} 429 430 //Validate folders exist 431 if (!Directory.Exists(txtFilesLocation.Text)) 432 { 433 lstErrors.Items.Add("File/directory to index doesn't exist."); 434 } 435 else if (!Directory.Exists(txtIndexLocation.Text)) 436 { 437 lstErrors.Items.Add("Index directory doesn't exist."); 438 } 439 else 440 { 441 //Input data is good, start indexing! 442 mobjIndexer = new IndexFiles(); 443 try 444 { 445 //Assign methods to invoke when events are triggered from indexer 446 mobjIndexer.OnPercentCompleteChanged += new IndexFiles.OnPercentCompleteChangedHandler(this.IndexFiles_OnPercentCompleteChangedHandler); 447 mobjIndexer.OnErrorIndexing += new IndexFiles.OnErrorIndexingHandler(this.IndexFiles_OnErrorIndexingHandler); 448 //Set the indexer's public properties 449 mobjIndexer.FilesLocation = txtFilesLocation.Text; 450 mobjIndexer.IndexLocation = txtIndexLocation.Text; 451 //Start indexing 452 if (mobjIndexer.StartIndexing()) 453 { 454 //Overall status 455 lblTotalTime.Text = mobjIndexer.NumDocsIndexed + " documents indexed in " + 456 (mobjIndexer.TotalTime / 1000) + " seconds. " + 457 mobjIndexer.NumDocsSkipped + " skipped. " + 458 mobjIndexer.NumDocsErrored + " errored."; 459 } 460 else 461 { 462 throw new Exception(mobjIndexer.Error); 463 } 464 } 465 catch (Exception ex) 466 { 467 lstErrors.Items.Add("Exception: " + ex.Message); 468 lblTotalTime.Text = "An error interrupted processing..."; 469 } 470 finally 471 { 472 mobjIndexer = null; 473 } 474 } 475 } 476 //Clear status 477 statAll.Text = ""; 478 //Enable/disable controls 479 txtFilesLocation.Enabled = true; 480 txtIndexLocation.Enabled = true; 481 cmdStart.Enabled = true; 482 cmdStop.Enabled = false; 483 grpSearch.Enabled = true; 484 grpErrors.Enabled = true; 485 Cursor = System.Windows.Forms.Cursors.Default; 486 } 487 #endregion 488 #region cmdStop_Click ... 489 490 private void cmdStop_Click(object sender, System.EventArgs e) 491 { 492 //Stop indexing (set indexer's public property) 493 mobjIndexer.StopProcessing = true; 494 } 495 #endregion 496 #region IndexFiles_OnPercentCompleteChangedHandler ... 497 498 private void IndexFiles_OnPercentCompleteChangedHandler(object sender, System.EventArgs e, 499 int intPC, long lngNumDocsIndexed, 500 string strMessage) 501 { 502 //Invoked when the indexer's OnPercentCompleteChanged event is fired 503 statAll.Text = " " + intPC.ToString() + "% complete (" + 504 lngNumDocsIndexed + " documents) - " + strMessage; 505 statAll.Refresh(); 506 System.Windows.Forms.Application.DoEvents(); 507 } 508 #endregion 509 #region IndexFiles_OnErrorIndexingHandler ... 510 511 private void IndexFiles_OnErrorIndexingHandler(object sender, System.EventArgs e, string strError) 512 { 513 //Invoked when the indexer's OnErrorIndexing event is fired 514 lstErrors.Items.Add(strError); 515 System.Windows.Forms.Application.DoEvents(); 516 } 517 #endregion 518 #region cmdSearch_Click ... 519 520 private void cmdSearch_Click(object sender, System.EventArgs e) 521 { 522 Cursor = System.Windows.Forms.Cursors.WaitCursor; 523 //Enable/disable controls 524 txtFilesLocation.Enabled = false; 525 txtIndexLocation.Enabled = false; 526 cmdStart.Enabled = false; 527 cmdStop.Enabled = true; 528 grpSearch.Enabled = false; 529 grpErrors.Enabled = false; 530 531 //Initialize controls 532 lstErrors.Items.Clear(); 533 dgResults.Visible = false; 534 lblNumHits.Text = ""; 535 536 //Trim folder paths 537 txtIndexLocation.Text = txtIndexLocation.Text.Trim(); 538 txtSearchFor.Text = txtSearchFor.Text.Trim(); 539 540 //Validate input data 541 if (txtIndexLocation.Text.Length == 0) 542 { 543 lstErrors.Items.Add("Missing index directory location."); 544 } 545 else if (txtSearchFor.Text.Length == 0) 546 { 547 lstErrors.Items.Add("Missing search phrase."); 548 } 549 else if (txtLastModFrom.Text.Length == 0 || !IsDate(txtLastModFrom.Text)) 550 { 551 lstErrors.Items.Add("Last Modified From date invalid."); 552 } 553 else if (txtLastModTo.Text.Length == 0 || !IsDate(txtLastModTo.Text)) 554 { 555 lstErrors.Items.Add("Last Modified To date invalid."); 556 } 557 else 558 { 559 //Ensure slashes are at the end of the folder paths 560 txtIndexLocation.Text = txtIndexLocation.Text.Trim(); 561 if (txtIndexLocation.Text.Substring(txtIndexLocation.Text.Length - 1, 1) != "\\") 562 txtIndexLocation.Text = txtIndexLocation.Text + "\\"; 563 564 //Validate folders exist 565 if (!Directory.Exists(txtIndexLocation.Text)) 566 lstErrors.Items.Add("Index directory doesn't exist."); 567 else 568 { 569 //Input data is good, search! 570 SearchFiles objSearch = new SearchFiles(); 571 try 572 { 573 //Set the searcher's public properties 574 objSearch.IndexLocation = txtIndexLocation.Text; 575 objSearch.NumHitsRequested = 10; 576 objSearch.SearchFor = txtSearchFor.Text; 577 objSearch.LastWriteFrom = Convert.ToDateTime(txtLastModFrom.Text); 578 objSearch.LastWriteTo = Convert.ToDateTime(txtLastModTo.Text); 579 //Perform Search 580 if (objSearch.StartSearch()) 581 { 582 //Overall status 583 lblNumHits.Text = objSearch.NumHitsFound + " hits in " + 584 objSearch.TotalTime + " ms."; 585 //Results found? 586 if (objSearch.NumHitsFound > 0) 587 { 588 //Bind dataview to datagrid 589 dgResults.DataSource = objSearch.ResultsDataView; 590 dgResults.Visible = true; 591 //Autoresize columns so data fits nicely 592 SizeColumnsToContent(dgResults); 593 } 594 else 595 dgResults.Visible = false; 596 } 597 else 598 { 599 //Show any errors that occurred 600 if (objSearch.Error.Length > 0) 601 { 602 MessageBox.Show(objSearch.Error, APPTITLE, MessageBoxButtons.OK); 603 } 604 else 605 { 606 MessageBox.Show("Unknown error.", APPTITLE, MessageBoxButtons.OK); 607 } 608 } 609 } 610 catch (Exception ex) 611 { 612 lstErrors.Items.Add("Exception: " + ex.Message); 613 } 614 finally 615 { 616 objSearch = null; 617 } 618 } 619 } 620 //Clear status 621 statAll.Text = ""; 622 //Enable/disable controls 623 txtFilesLocation.Enabled = true; 624 txtIndexLocation.Enabled = true; 625 cmdStart.Enabled = true; 626 cmdStop.Enabled = false; 627 grpSearch.Enabled = true; 628 grpErrors.Enabled = true; 629 Cursor = System.Windows.Forms.Cursors.Default; 630 } 631 #endregion 632 #region IsDate ... 633 634 public static bool IsDate(object dt) 635 { 636 //Simple IsDate function 637 try 638 { 639 System.DateTime.Parse(dt.ToString()); 640 return true; 641 } 642 catch 643 { 644 return false; 645 } 646 } 647 #endregion 648 #region SizeColumnsToContent ... 649 650 public void SizeColumnsToContent(DataGrid dataGrid) 651 { 652 Graphics graphics = dataGrid.CreateGraphics(); 653 DataGridTableStyle tableStyle = new DataGridTableStyle(); 654 655 try 656 { 657 if (dataGrid.DataSource is DataView) 658 { 659 DataView dataView= (DataView)dataGrid.DataSource; 660 DataTable dataTable = dataView.Table; 661 int nRowsToScan = dataTable.Rows.Count; 662 dataGrid.TableStyles.Clear(); 663 tableStyle.MappingName = dataTable.TableName; 664 665 DataGridTextBoxColumn columnStyle; 666 float iWidth; 667 int iCurrCol = 0; 668 669 while (iCurrCol < dataTable.Columns.Count) 670 { 671 DataColumn dataColumn = dataTable.Columns[iCurrCol]; 672 columnStyle = new DataGridTextBoxColumn(); 673 columnStyle.MappingName = dataColumn.ColumnName; 674 switch (iCurrCol) 675 { 676 case 0 : 677 columnStyle.HeaderText = "Rank"; 678 columnStyle.Alignment = HorizontalAlignment.Center; 679 break; 680 case 1 : 681 columnStyle.HeaderText = "Path"; 682 break; 683 case 2 : 684 columnStyle.HeaderText = "Last Modified"; 685 columnStyle.Alignment = HorizontalAlignment.Center; 686 break; 687 case 3 : 688 columnStyle.HeaderText = "Score"; 689 columnStyle.Alignment = HorizontalAlignment.Center; 690 break; 691 } 692 iWidth = graphics.MeasureString(columnStyle.HeaderText, dataGrid.Font).Width; 693 DataRow dataRow; 694 int iRow= 0; 695 while (iRow < nRowsToScan) 696 { 697 dataRow = dataTable.Rows[iRow]; 698 if (dataRow[dataColumn.ColumnName] != null) 699 { 700 float iColWidth = graphics.MeasureString(dataRow.ItemArray[iCurrCol].ToString(), dataGrid.Font).Width; 701 iWidth = System.Math.Max(iWidth, iColWidth); 702 } 703 iRow += 1; 704 } 705 columnStyle.Width = Convert.ToInt32(iWidth + 4); 706 tableStyle.GridColumnStyles.Add(columnStyle); 707 iCurrCol += 1; 708 } 709 dataGrid.TableStyles.Add(tableStyle); 710 } 711 } 712 catch (Exception e) 713 { 714 MessageBox.Show(e.GetBaseException().ToString()); 715 } 716 finally 717 { 718 graphics.Dispose(); 719 } 720 } 721 #endregion 722 #region dgResults_DoubleClick ... 723 724 private void dgResults_DoubleClick(object sender, System.EventArgs e) 725 { 726 //Launch the selected row's item 727 DataGridCell objCell = new DataGridCell(); 728 objCell.RowNumber = dgResults.CurrentCell.RowNumber; 729 objCell.ColumnNumber = 1; 730 string strLink = dgResults[objCell].ToString(); 731 try 732 { 733 Process.Start(strLink); 734 } 735 catch (Exception ex) 736 { 737 MessageBox.Show("Error: " + ex.Message, APPTITLE, MessageBoxButtons.OK); 738 } 739 } 740 #endregion 741 } 742}