Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Creating a Research Helper

  This is a tool that searches a variety of search engines with a keystroke.
3 Leaf Development

Difficulty: Intermediate
Time Required: 1-3 hours
Cost: Free
Software: Visual Studio Express Editions
Hardware:
Download: Lost!

I like productivity at my fingertips, so for this post, I'm going to walk through a tool that I've created to save time for online researching.  This tool can be made to launch by simply using a shortcut key while you're in any application.

ResearchHelp

One common task is to look up some term in a variety of search engines.  For example, I might come across the phrase "Visual Studio Team System".  Often I search Google to find the key sites, Google Groups to see what people are saying about it in the newsgroups, Feedster to get the recent buzz from the blogs, etc.  To facilitate searching quickly, I put together a simple utility that I can also launch from a key stroke.  With a simple CTRL+C to copy the phrase, and CTRL+SHIFT+Y, I can get the following:

This application dynamically creates tabs for my configured search engines, and then uses the new WebBrowser control to display the results.  Dissecting this application shows a number of the new features in Visual Studio 2005. I'll use Visual C# 2005 Express Edition for this example. Depending on the language I wanted, I could have used any of the Visual Studio Express Editions to create a similar sample. Beta 2 of the Express editions can be downloaded from http://msdn.microsoft.com/express

First, the form is divided into two sections using the new SplitContainer control.  This control is a combination of 2 panels, and a splitter.  It's much easier to work with than the Visual Studio .NET 2003 Splitter control, which got tricky depending on the order in which you added the panels and splitter to the form.

The top panel contains a section that lets you enter the search information.  This is populated by default with whatever's in the clipboard.

Next, you see the new ToolStrip control.  This control makes it trivial to set up professional "Office" style toolbars, that support docking, floating, repositioning, and other features.  This toolbar just has a couple of buttons on it, but you can also add drop-downs, text boxes, labels, and a variety of other controls.  To wire up code for an item on the toolbar, you simply double-click it in the designer.  This ToolStrip contains two buttons.  The first will open the currently selected page in a new browser window, which is useful when you've found something interesting, and you want the full features of Internet Explorer.  The second button provides simple "Back" button functionality.

Below the toolbar is a Tab control.  The tabs are actually created at run-time when the search is performed, and a WebBrowser control is added to each tab to display the results.

The code for this application was very straightforward to write.  First, the application contains a list of configured search engines using a custom SearchInfo class:

Visual C#

private SearchInfo[] searches = { 
new SearchInfo("Google Web", "http://www.google.com/search?q="),
new SearchInfo("Google Groups", "http://groups-beta.google.com/groups?q="),
new SearchInfo("Google News", "http://news.google.com/news?q="),
new SearchInfo("Feedster", "http://www.feedster.com/search.php?q="),
new SearchInfo("Technorati",
"http://www.technorati.com/cosmos/search.html?rank=&url=")
};

Visual Basic

Private searches As SearchInfo() = { _
New SearchInfo("Google Web", "http://www.google.com/search?q="), _
New SearchInfo("Google Groups", "http://groups-
beta.google.com/groups?q="
), _
New SearchInfo("Google News", "http://news.google.com/news?q="), _
New SearchInfo("Feedster", "http://www.feedster.com/search.php?q="), _
New SearchInfo("Technorati",
"http://www.technorati.com/cosmos/search.html?rank=&url=") _
}

If you have other search engines that you want to use, you can just add them here.

The Form_Load event does a couple of things.  First, it registers a shortcut for the application:

Visual C#

string shortcutPath = 
Environment.GetFolderPath(Environment.SpecialFolder.Programs)
+ @"\ResearchHelp.lnk";
if (!System.IO.File.Exists(shortcutPath))
{
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);
shortcut.Hotkey = "CTRL+SHIFT+Y";
shortcut.TargetPath = Assembly.GetExecutingAssembly().Location;
shortcut.Description = "ResearchHelp";
shortcut.Save();
}

Visual Basic

Dim shortcutPath As String = _
Environment.GetFolderPath(Environment.SpecialFolder.Programs) _
& "\ResearchHelp.lnk"
If Not System.IO.File.Exists(shortcutPath) Then
Dim shell As WshShell = New WshShell()
Dim shortcut As IWshShortcut =
CType(shell.CreateShortcut(shortcutPath), IWshShortcut)
shortcut.Hotkey = "CTRL+SHIFT+Y"
shortcut.TargetPath = Assembly.GetExecutingAssembly().Location
shortcut.Description = "ResearchHelp"
shortcut.Save()
End If

And then, some new methods on the Clipboard class are used to see if the Clipboard contains a search string:

Visual C#

if (Clipboard.ContainsText)
{
topicTextBox.Text = Clipboard.GetText();
}

Visual Basic

If Clipboard.ContainsText() Then
topicTextBox.Text = Clipboard.GetText()
End If

You can see that this makes it very simple to see if the clipboard contains data in a certain format, and then extract that data in a more strongly typed way.

When the search button is clicked, the application iterates through all the SearchInfo objects, and creates corresponding tabs and WebBrowser instances to show the results.  With the new WebBrowser control in Visual Studio 2005, you can see that the browser is simple to work with:

Visual C#

private void searchButton_Click(object sender, EventArgs e)
{
searchTab.Controls.Clear();
foreach (SearchInfo si in searches)
{
TabPage tp = new TabPage(si.Description);
WebBrowser wb = new WebBrowser();
searchTab.Controls.Add(tp);
tp.Controls.Add(wb);
wb.Dock = DockStyle.Fill;
wb.ScriptErrorsSuppressed = true;
wb.Navigate(si.BaseURL + topicTextBox.Text);
}
}

Visual Basic

Private Sub searchButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles searchButton.Click
searchTab.Controls.Clear()
For Each si As SearchInfo In searches
Dim tp As TabPage = New TabPage(si.Description)
Dim wb As WebBrowser = New WebBrowser()
searchTab.Controls.Add(tp)
tp.Controls.Add(wb)
wb.Dock = DockStyle.Fill
wb.ScriptErrorsSuppressed = True
wb.Navigate(si.BaseURL & topicTextBox.Text)
Next
End Sub

The TabPage objects are added to the TabControl, and then the WebBrowser objects are added to each tab.  Each browser is set to DockStyle.Fill to fill the available tab space.  Finally, the Navigate method is used to perform the actual search and show the results.  Navigate is an asynchronous method, so all the browser instances can be searching in parallel.

Conclusion

Visual Studio 2005 made it trivial for me to create a utility that I now use all the time.  New controls like the SplitContainer, ToolStrip, and WebBrowser made it very quick to put together an easy-to-use interface.  Enhancements to the Clipboard class make it more intuitive to use, and the WebBrowser is a nice managed wrapper around the browser control. Get started today by downloading one of the Visual Studio 2005 Express Editions from http://msdn.microsoft.com/express.

Parting tip: I'm a big fan of hot-keys instead of mouse clicks.  When I'm writing code, I like to keep my hands on the keyboard.  One thing that I often do in Visual Studio is CTRL+TAB to switch between my open code windows.  It was an interesting surprise to see that even this has been enhanced.  When I CTRL-TAB now, the following window opens in Visual Studio to make it easy to pick exactly the file I want:

Follow the Discussion

  • kfarmerkfarmer

    Perhaps use the IE7 search provider definitions?

  • GuruparanGuruparan

    Hi Microsoft Guy!...You should be using Live search for all these example sited above!...Even i am using Live search daily!...why dont you use your own product!..Sad...

    Live search Rocks!

  • ZacZac

    Maybe Live search is too broad sometimes, maybe he only wants to search on a select few engines.  I for one this this is incredibly useful.

  • louiselouise

    When people think outside of the box, they should be able to see that this code can help them with other solutions....

Remove this comment

Remove this thread

close

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.