Custom textbox validation

May 10, 2010 Leave a comment

The task is to use IDataErrorInfo interface to control validation on a textbox(s). We will end up with something that looks like this:

then

then all you have to do is make the tooltip so that you can see what the error is:

<Window.Resources>
<Style TargetType=”{x:Type TextBox}”>
<Style.Triggers>
<Trigger Property=”Validation.HasError” Value=”true”>
<Setter Property=”ToolTip” Value=”{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}”/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
Advertisements
Categories: WPF

Simple textbox validation

May 10, 2010 Leave a comment

The thing that i really get out of this is the UpdateSourceTrigger – i always forget that bit.

For this example, pretend you have a class which holds an integer. Go ahead and bind the textbox to that integer.

How do you validate that an integer is being typed in, as it is being typed?

easy:

<TextBox Height="23" Margin="94,0,64,93" Name="TextBox2" VerticalAlignment="Bottom">
<TextBox.Text>
<Binding Path="Age" UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<ExceptionValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>

Categories: Uncategorized

CanExecuteChanged for ICommand

May 3, 2010 Leave a comment

If you want to create a command in WPF, you probably want to tell the application when the command is able to be executed. Of course you put this code in the CanExecute() method. But what about when the conditions change that effect the CanExecute() method?

Add the following code to your Command class, and your button will magically toggle on and off enabled. (or whatever has the command, such as a MenuItem, etc.)


Public Custom Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged
    AddHandler(ByVal value As EventHandler)
        AddHandler CommandManager.RequerySuggested, value
    End AddHandler
    RemoveHandler(ByVal value As EventHandler)
        RemoveHandler CommandManager.RequerySuggested, value
    End RemoveHandler
    RaiseEvent(ByVal sender As System.Object, ByVal e As System.EventArgs)
    End RaiseEvent
End Event

Categories: WPF

Email validation (regex + VB.NET)

May 3, 2010 Leave a comment

Private Shared Function IsValidEmailAddress(ByVal email As String) As Boolean

 

‘ This regex pattern came from: http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx

Dim pattern As String = “^(?!\.)(“”([^””\r\\]|\\[“”\r\\])*””|([-a-z0-9!#$%&’*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$” 

Return Regex.IsMatch(email, pattern, RegexOptions.IgnoreCase)

 End Function

Categories: Uncategorized

Google Search Suggestions

December 11, 2009 Leave a comment

I was able to get a Google-like search suggestions box happening in almost no time with help from http://www.codeproject.com/KB/aspnet/Search_SuggestTextBox.aspx. I’ve found this one to be REALLY handy.

Categories: Uncategorized

Excel Sheets being ‘Half’ Active

November 1, 2009 Leave a comment

I encountered a very interesting bug this week. The initial symptom was:

When any values in a SQL Server 2005 stored procedure result that I’m using in an Add-In are of type DATETIME or SMALLDATETIME then the corresponding cells (i.e. same sheet address) in the previously active worksheet (?!) are formatted to DATETIME, even when the data in those cells is formatted in Excel as Number or Currency.

CopyFromRecordset was being used to populate the currently active worksheet.

The answer ended up coming from harfang on Experts-Exchange.com:

Even though the following SHOULD give you a reference to a newly added, active worksheet:

‘Put results to new sheet

Dim NewSheet As Worksheet

Worksheets.Add

Set NewSheet = ActiveSheet

it only ‘half’ recognizes the new sheet as active. The implication being that NewSheet.CopyRecordset DOES copy values into the new sheet, but the formatting subroutines somehow link to the previously active sheet. More annoyingly, this only occurs for DATETIME field types, because any currency or numeric formatting wasn’t applied to the previously active sheet. This can be solved by adding one line:

NewSheet.Activate

which ‘fully’ registers NewSheet as active.

How frustrating! I’d be interested to see any Microsoft articles documenting this.

For more code samples and details on the solution check: http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_24856799.html

-Dave

P.S. I’d love to get comments on good / free screencasting / screen recording software. This bug was on the more tricky side to explain and a short video showing symptoms would be really helpful.

Categories: Uncategorized

Very cool graphing techniques

September 7, 2009 1 comment

Thanks to Hans Rosling and his TED Talk for a cool example of scatter graphs.

Sure, the talk was interesting (I never thought of the ‘developing world’ paradigm as outdated) but, more impotantly to this blog, the coolest points for me of the Trendalyzer beta are:

  • Select a few dots out of dozens on the graph that will have a kind of ‘trail’ when animated over time.
  • Control the ratio of dot size in the bottom right (i.e. what are the min and max bounds for each dot?)

-Dave

Categories: Uncategorized