Tag Archives: Visual C#

Visual C#: Navigation in Applications

In most client applications, users can have any number of windows open at the same time. In Outlook, you can have the Inbox, Calendar, Tasks, and Contacts open at the same time. You can have multiple messages, appointments, tasks and contacts open at the same time. You are limited solely by your tolerance for navigating amongst large numbers of windows.

When you create a Windows Forms Application project in Visual Studio, the first thing you see is a form. Often this will be your startup form. You then add any number of forms to the application. Each form is a window and it serves a purpose, whether it is data entry or reporting or a splash screen or navigation. Whether users navigate by selecting a menu item or clicking a button, the application displays a new form in a new window. Web applications take the opposite approach. Typically, you will have one window open, and that is the browser. As you navigate within a site, you load a different page in the browser and you have one page open at a time. This is not always the case, but it is true much more often than not.

In a WPF application, you can choose either of these navigation paradigms. The WindowNavigationDemo sample application consists of three windows. The MainWindow.xaml file defines the application’s startup window. It contains buttons to display the other two windows, defined in Window1.xaml and Window2.xaml. Both of these windows can be open at the same time, as
shown in the figure below.

nav_in_application

Figure above, this application uses window-based navigation.

When you create an application based on the WPF Application project template, Visual Studio assumes that you will use windows-based navigation. It sets the startup window to MainWindow.xaml.

If you want to use page-based navigation, and make your application look and feel more like a Web application, you need to use pages rather than windows. You should also consider using hyperlinks rather than buttons.

The Page class is similar to the Window class. Each is a container and can contain a single element, such as a StackPanel, DockPanel, or Grid. A primary difference between a window and a page is that a page is designed to be navigated to.

A page can be hosted in a Window, a NavigationWindow, a Frame, another page, or the browser. Because it is not a standalone container, it has a smaller set of properties than a window.

The Page class contains several properties, including the following:

  • Background: Determines the background for the page
  • Foreground: Determines the default foreground for the page. All elements in the page will have this foreground by default.
  • FontFamily and FontSize: Determine the default font family and size for the page. All elements in the page will have this font size and family by default.
  • ShowsNavigationUI: Determines whether the page displays back and forward buttons.
  • Title: Sets the name of the page used in the navigation history.
  • WindowHeight and WindowWidth: Determine the height and widthof the window that hosts the page.
  • WindowTitle: Determines the title of the window that hosts the page.

The NavigationWindow class represents a window with built-in navigation support. This class derives from the Window class and includes the ability to display content and navigate. The content can be any .NET Framework object or HTML page, but typically the content will be in a page.

The NavigationWindow object supports not only navigation but the navigation history. Some of the properties of the NavigationWindow class are:
BackStack and ForwardStack: Return an IEnumerable you can use to enumerate the entries in the window’s back and forward navigation history.

  • CanGoBack and CanGoForward: Indicate whether there is at least one entry in the back and forward navigation history.
  • CurrentSource: Gets the uniform resource identifier (URI) of the content that was last navigated to.
  • ShowNavigationUI: Determines whether a NavigationWindow shows its navigation buttons.
  • Source: Gets or sets the uniform resource identifier (URI) of the current content, or the URI of new content that is currently being navigated to.

This post is an excerpt from the online courseware Presentation Foundation Using Visual C# 2010 course written by expert Ken Getz and Robert Green.

Ken Getz is a Visual Studio expert with over 25 years of experience as a successful developer and consultant. He is a nationally recognized author and speaker, as well as a featured instructor for LearnNowOnline.

Robert Green is a Visual Studio expert and a featured instructor for several of our Visual Basic and Visual C# courses. He is currently a Technical Evangelist in the Developer Platform and Evangelism (DPE) group at Microsoft. He has also worked for Microsoft on the Developer Tools marketing team and as Community Lead on the Visual Basic team. Robert also has several years of consulting experience focused on developer training, and is a frequent speaker at technology conferences including TechEd, VSLive, VSConnections and Advisor Live.

Radio Button Control – Visual C#

The RadioButton control works much like the CheckBox control—it provides the same properties, and the same events. The big difference between the RadioButton and CheckBox controls is that RadioButton controls are grouped by their container. Within a single container, you can generally only select one RadioButton control. If you place multiple RadioButton controls into multiple containers, you can select one RadioButton control from each container.

You can override this behavior using the GroupName property of the control. You can set this string value to create more than one group per container, or to create a single group that spans multiple containers. To demonstrate this behavior, select the RadioButton tab in the sample application, shown in Figure 1.

TIP: There’s no need to specify a GroupName property for a RadioButton control unless you want to override the default behavior.

radiobuttoncontrl

Figure 1. RadioButton controls normally allow you to select only one per
container; override the behavior with the GroupName property.

The markup for the sample page looks like the following:

If you try out the demonstration, you’ll see that you can only select a RadioButton control from the group named Group 1, because they’re all in the same container. You can select only a single item from the group named Group 2, again, because they’re in the same container. You can also select one item from the group named Group 3, because these items have their GroupName property set to the same text (the text is arbitrary, and it’s set to
Group3 in the example). Because these controls share the same group name, and because they have a group name set, you can select multiple RadioButton controls within a single container.

In contrast, the right-hand Border control contains multiple CheckBox controls. Nothing keeps you from selecting as many CheckBox controls as you like. That’s the point: When you use RadioButton controls, you intend for users to select only one per group. When you use CheckBox controls, you intend users to select multiple items.

NOTE This sample uses a StackPanel control as the container for the
groups of RadioButton controls. You may also want to investigate
the GroupBox control, which not only groups the controls but
also adds a caption describing the group. The GroupBox control is,
effectively a simple container with a caption added.

This post is an excerpt from the online courseware Windows Presentation Foundation Using Visual C# 2010 course written by expert Ken Getz and Robert Green.

Ken Getz is a Visual Studio expert with over 25 years of experience as a successful developer and consultant. He is a nationally recognized author and speaker, as well as a featured instructor for LearnNowOnline.

Robert Green is a Visual Studio expert and a featured instructor for several of our Visual Basic and Visual C# courses. He is currently a Technical Evangelist in the Developer Platform and Evangelism (DPE) group at Microsoft. He has also worked for Microsoft on the Developer Tools marketing team and as Community Lead on the Visual Basic team. Robert also has several years of consulting experience focused on developer training, and is a frequent speaker at technology conferences including TechEd, VSLive, VSConnections and Advisor Live.

Generic Lists in LINQ

LINQ allows you to query over any type of generic list. The most common list type you’ll use is List, but LINQ allows you to work with any of the following list types, in the System.Collections.Generic namespace unless otherwise specified:

  • List<T>
  • LinkedList<T>
  • Queue<T>
  • Stack<T>
  • HashSet<T>
  • System.Collections.ObjectModel.Collection<T>
  • System.ComponentModel.BindingList<T>

Just to prove the point, the sample project includes the QueryGenericList method. This method performs similar work to the earlier QueryArray method, this time showing both query syntax, and functional syntax. Listing 2 shows the entire procedure.


Listing 2. The QueryGenericList method shows off several different techniques, including query vs. functional syntax.

Running this procedure displays the output shown in the Figure below.

The Figure above. The QueryGenericList procedure provides a list of files.

The QueryGenericList procedure starts by retrieving the array of files, and copying the data into a generic list:

Next, the code uses standard query syntax to retrieve from the list all the files whose length is less than 1000 bytes, ordered first by length in descending order, and then for files with matching lengths, by file name. The query retrieves an anonymous type containing the Name and Length properties of the file:

Finally, the procedure demonstrates the equivalent query, created using function syntax instead. This query returns the same data in the same order, but does it by calling extension methods with lambda expressions defining their behavior:

Note a few things about this query definition:

  • The Where function accepts a lambda expression that returns true or false for each item in the collection. Items for which the lambda expression returns false are excluded from the collection:
  • The OrderByDescending method (and its “cousin,” the OrderBy method) accepts a lambda expression that returns information indicating how to sort the data. In this case, the sorting occurs based on the Length property of each FileInfo object:
  • Using the standard query syntax, you can indicate ordering by multiple columns by simply including the columns separated with a comma. In function syntax, you must use the ThenBy or ThenByDescending method to indicate a secondary sort. As a parameter to the method, pass a lambda expression which, again, indicates the sort order. In this case, the secondary sort uses the Name property of the input FileInfo object:
  • The Select method accepts a lambda expression that identifies which field or fields you want the query to return. In this case, the query returns an anonymous type containing the Name and Length properties 

NOTE Remember, all the techniques you’ve seen in this example apply to any type of LINQ query, not just LINQ to Objects, and not just queries that work with generic lists.

TIP: The System.Linq.Enumerable class provides a large number of extension methods that add behavior to queryable objects, much like the Select, Where, OrderBy, ThenBy, OfType, and other methods you’ve already seen. Later sections in this chapter introduce many of these methods. For more information, review the Microsoft documentation for the System.Linq.Enumerable class and its many methods.

 

ldn-expertkgetzThis post is an excerpt from the online courseware for our Microsoft LINQ Using Visual C# 2010 course written by expert Ken Getz.

Ken Getz is a Visual Studio expert with over 25 years of experience as a successful developer and consultant. He is a nationally recognized author and speaker, as well as a featured instructor for LearnNowOnline.

Learning A New Programming Language

VisualStudio2010

 

Microsoft’s .NET interpreted languages are some of the easiest to learn and some of the easiest to use to develop fully functional software applications. Visual Basic has long been used as a training language – it’s easy to learn, but not particularly robust. C++ on the other hand is the granddaddy of the modern programming language. Object-oriented with sophisticated memory management, it’s the professional language of choice for software development. But, it’s very difficult to learn.

A popular choice is Visual C#. It combines the object-oriented power of C++ with the simplicity of Visual Basic. It’s very easy to learn.

There are several ways to learn a new programming language. Programming books come loaded with samples and often include actual code on a CD or as downloads. Don’t have enough time for a book? Just choose a visual studio 2010 tutorial video and start building some software.


XAML vs. HTML ain’t like VB vs. C#

windows-8-logo0817

 

Before Windows 8 apps came along, I spent a lot of time writing courseware for both VB and C#, and converting between the two was relatively easy–I got it down to a science. At worst, it involved some minor changes, once you got past the language differences. And those minor changes got smaller and smaller with each version of the languages.

So, wow, was I surprised to find that it’s nothing like that when comparing C# and JavaScript apps for Windows 8.

Basically, they have little, if anything, in common. Because C# (and VB) use the .NET Framework, more or less, and JavaScript apps use WinJS, the tools, classes, and even concepts are different. I was totally surprised how different the environments are, down to big differences in the available controls–things you find in JavaScript simply aren’t available to C#/VB developers. Want a date picker control? Better look outside Visual Studio to find that control (and several others that JavaScript supplies but C#/VB do not).

Anyway, my point is that if you’re a writer (or a developer) thinking that you can create apps in C# and then later convert them to JavaScript (or vice versa) think again. That ain’t happening.

Ken Getz is a Visual Studio expert with over 25 years of experience as a successful developer and consultant. He is a nationally recognized author and speaker, as well as a featured instructor for LearnNowOnline.