Tuesday, 24 September 2013

WPF - How to Read each line from a TextBox

To read each line from a TextBox control in WPF, we can use the below code snippet.
StringCollection type represents a collection of strings. The type is part of the namespace - System.Collections.Specialized. import the namespace in your code file:
using System.Collections.Specialized;
private StringCollection GetTextLines()
{
var lines = new StringCollection();
int lineCount = txtPlainTxt.LineCount;
for (int line = 0; line < lineCount; line++)
lines.Add(txtPlainTxt.GetLineText(line));
return lines;
}

The usage is straightforward as below:
Please note that each item of the StringCollection is a string holding each line of the TextBox.
// Usage
var lines = GetTextLines();
foreach (var line in lines)
{
// line is string with each line.
}

WPF - How to set Start Window

To set the Start window / page in a WPF app, we can configure it in App.xaml file.
In the "Application" element of App.xaml file in your project, set the "StartupUri" attribute to the xaml page you want to load first in the WPF Application.
In the below sample code "QuizTxt2XmlPage.xaml" is the start up page.

<Application x:Class="Quiz.WpfApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="QuizTxt2XmlPage.xaml">

A reference screen shot is provided below:


Saturday, 21 September 2013

SQL Server:Login failed for user 'sa'. (Microsoft SQL Server, Error: 18456)

Sometimes you can connect to MS SQL Server Management Studio with Windows Authentication but is unable to connect with SQL Server Authentication. You might get the below error message:

Login failed for user 'sa'. (Microsoft SQL Server, Error: 18456)
The most probable reason for this could be that when you first installed SQL Server and configured, you might have set the Server Authentication mode to Windows Authentication Mode only. Follow the below steps to solve this issue.

  1. Login to the MS SQL Server Management Studio with Windows Authentication. 
  2. In SQL Server Management Studio Object Explorer, right-click the server, and then click Properties. 
  3. In the Server Properties window, select "Security" under "Select a page" section. 
  4. Select the Server authentication as "SQL Server and Windows Authentication mode" and click Ok. 
  5. Restart the SQL Services and then try to login with 'sa' details.
Set Server Authentication mode to SQL Server and Windows

Restart MSSQL servies before trying to login again.

Thursday, 5 September 2013

Windows Phone - How to use Resource Files

Resource Files in Windows phone has the similar concepts as it was in Web Forms. It can be used for localization as well as store string values to be used across your apps. Resource files are a nice way to reduce hardcoding strings inside your application code.
The default resource file added to your solution when the project is created is "AppResources.resx" which is in the "Resources" folder at the root of your project folder hierarchy. This default file is called the "neutral language resource" because it is the fallback resource file even in localization scenarios.

Note: As in web forms and win forms, each culture has its own resource file, with the culture suffixed to the filename (AppResources.en-US.resx). We will discuss more on localization scenarios in another post.

Below is the code to dynamically bind your resource to XAML page element's properties.
"{Binding Path=LocalizedResources.ApplicationHeader, Source={StaticResource LocalizedStrings}}"
For example if we want to bind a string resource to the Text property of TextBlock Control:
<TextBlock Text="{Binding Path=LocalizedResources.ApplicationHeader, Source={StaticResource LocalizedStrings}}" />

Wednesday, 4 September 2013

Windows Phone - How to include ampersand (&) in the content of XAML elements like Combobox and TextBlock

/* XAML */
<ComboBox><ComboBoxItem> Awake & Alive</ComboBoxItem></ComboBox>
The above code throws exception :
Entity references or sequences beginning with an ampersand '&' must be terminated with a semicolon ';'.

The solution is to encode the special characters:
Use &amp; to encode the ampersand.
/* XAML */
<ComboBox><ComboBoxItem> Awake &amp; Alive</ComboBoxItem></ComboBox>
The same is applicable for other elements like TextBlock

Alternative:
/* XAML */
<ComboBox><ComboBoxItem><![CDATA[Awake & Alive]]></ComboBoxItem></ComboBox>
Explanation:
XAML being xml based, the xml parser when parsing an xml element, it also parses the text between XML tags, because XML elements can contain child elements.
CDATA indicated the text that should not be parsed by the XML parser. So any text that includes characters like '<' and '&' are to be included in CDATA, because these characters have special meaning in XML.
CDATA section starts with "<![CDATA[" and ends with "]]>"

Tuesday, 6 August 2013

Windows Phone - Navigation from App.xaml.cs

To navigate to pages win windows phone app we use the Navigate method of NavigationService class:
NavigationService.Navigate(new Uri("/MyPage.xaml", UriKind.RelativeOrAbsolute));
But this doesn't work within App.xaml.cs.
Instead it works by using the below code:
Solution:
(Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/MyPage.xaml", UriKind.RelativeOrAbsolute));

Explanation:
Each application has only one Frame. It's this frame that exposes the NavigationService. Therefore, the NavigationService is always accessible via the frame since there's always an instance of it in any Windows Phone app. Since you don't usually instantiate a new NavigationService, it's easy to think that it's a static method. However, it's actually a non-static class that gets instantiated automatically when your app is run. All you're doing in this case is getting the global instance, which is attached to the always-present Frame, and using that to navigate between pages. This means your class does not have to instantiate, or explicitly inherit, a NavigationService.

Saturday, 3 August 2013

Windows Phone - How to make TextBlock scrollable

To make your windows phone textbox a scrollable one, just wrap the textbox within a ScrollViewer element. Do not forget to add textwrapping for the textbox. Set the vertical or horizontal scroll settings according to your needs.
<ScrollViewer>
    <TextBlock TextWrapping="Wrap" x:Name="txtbox1">
</TextBlock>
</ScrollViewer>