Build a Flyout Search Control (Continued)
You must hook up the event handlers in code because you add the radio buttons to the SearchRadioPanel dynamically. Use the SearchRadioPanel's OnControlAdded event to hook up each RadioButton's OnRadioButtonChanged and OnRadioButtonClick method as you add it to the SearchRadioPanel. OnRadioButtonChanged expands and collapses the flyout panel. OnRadioButtonClick enhances the standard radio button behavior by allowing the user to uncheck a button by clicking on it a second time. This gives the user an intuitive way to close the search panel. This approach requires you to manage a flag indicating whether the button was checked already. The Checked property won't work because it's always True in the Click event.
OnRadioButtonChanged calls RollOutSearch to expand the panel and RollUpSearch to collapse it. RollUpSearch sets pnlSearch's visibility and SearchRadioPanel's height. The search panel stores the current radio button in its Tag property to simplify finding the currently selected radio button. RollOutSearch checks this property to see if the user pressed a new radio button. If the user pressed the same button, the flyout redisplays with the user's previous entries. If the user pressed a different button, RollOutSearch clears its collection and adds a new label and textbox for each criterion in the collection. Each textbox stores its criteria in the Tag property to facilitate providing this information through the GetCriteria method. When RollOutSearch clears the collection, it adds back the Go button, which you must site on this panel.
Define Custom Events
The SearchRadioPanel defines two custom events:
Public Event GoSearch(ByVal sender As _
Object, ByVal e As System.EventArgs)
Public Event CancelSearch(ByVal sender _
As Object, ByVal e As _
System.EventArgs)
The GoSearch and CancelSearch events follow the standard Windows.Forms pattern by providing the sender and an event argument object as parameters. The control fires the OnBtnClick event when the user clicks on the Go button. As Listing 1 shows, OnBtnGoClick raises the GoSearch event and closes the search panel. The test form captures this event and displays a message box with a prototype call to a search method. The prototype illustrates how you retrieve values from the SearchRadioPanel.
If the user clicks on a depressed radio button, the SearchRadioPanel assumes the user wants to close the search panel. It raises the CancelSearch event before doing this. You might need to handle this event to provide special behavior, such as closing the form if the user doesn't select a record.
Listing 1 doesn't show supporting methods, such as SelectedIndex and RadioButtonCount, which manage the radio buttons; Appearance and ButtonHeight, which manage the control's appearance; and CriteriaCount, GetCriteria, and GetCriteriaText, which return information about the most recently displayed search information (they're in the sample code). GetCriteria and GetCriteriaText are indexed properties, so you can access the criteria information:
For i As Int32 = 0 To _
rp.CriteriaCount - 1
criteria = rp.GetCriteria(i)
Console.WriteLine criteria.Type
Console.WriteLine.GetCriteriaText(i)
Next
Be aware that there are plenty of places to go wrong with this control. Exceptions at certain points will be swallowed and leave you wondering why things didn't work. If you forget the Serializable attribute for a class, your properties won't be persisted. Changing the criteria class can render the property data invalid because the criteria collection is persisted as a set of resources. Sometimes Windows.Forms has heartburn over this and gives a nonintuitive error about needing more than one entry in the collection. It's also easy enough for simple programming mistakes to leave the panel invisible. However, you can build from this working example to provide your users with a slick and efficient search panel.
You need to provide the search method, and fill the form with a detail or list view when the user retrieves data, to make the control work in your application. You'll replace the message box with a real call to this search method. You might also want to communicate the search criteria to the user, perhaps in the form's title bar. You can expand on these ideas to use different Windows.Forms controls for different data types, incorporate graphics, display a selection grid when multiple records are returned, or allow the panel to fly out from any side of the screen.
About the Author
Kathleen Dollard is an independent consultant doing real-world development in .NET technologies. She's writing a book on code generation in .NET for Apress. She's active in the Denver Visual Studio User Group and is a regular contributor to Visual Studio Magazine, a Microsoft MVP, and a VBITS/VSLive! speaker. Reach Kathleen at .
Back to top
|