Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Gauge for SharePoint
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » WPF » TextBox control in WPF using VB.NET

TextBox control in WPF using VB.NET

In this article, I will discuss how to create a TextBox control in WPF and also I will discussing various properties of the TextBox control.

Author Rank :
Page Views : 1997
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


In this article, I will discuss how to create a TextBox control in WPF and also I will discussing various properties of the TextBox control.

TextBox control

TextBox is the control which accepts user input on a Form.

Properties - This control has the following properties.

txt1.gif

Figure 1.

Width - Width attributes of the TextBox element represent the width of a TextBox.

Height - Height attributes of the TextBox element represent the height of a TextBox.

Text - Text property of the TextBox element sets the content of a TextBox.

Background - The Background attributes set the background colors of text box.

Foreground - The Foreground attributes set the foreground colors of text box.

TextWrapping - The TextWrapping attributes sets the wrapping of text.

Scrolling Text -  VerticalScrollBarVisibility and HorizontalScrollBarVisibility sets the vertical and horizontal scroll bars visible.

IsReadOnly - IsReadOnly attribute to true makes the text box non editable.

Creating a TextBox in XAML

<TextBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="TextBox1" VerticalAlignment="Top" Width="120" Text="" TextWrapping="WrapWithOverflow" />

The Height and Width attributes sets the height and width of text box.

The form looks like this.

txt2.gif

Figure 2.

Restricting Text Size of a TextBox

The MaxLength property of the TextBox sets the number of characters allowed to input in a text box.

MaxLength="100"

Setting Background and Foreground Colors.

The Background and Foreground attributes set the background and foreground colors of text box. The following code sets background color as red and foreground color as yellow for the text box.

<TextBox Height="23" HorizontalAlignment="Left" Margin="128,90,0,0" Name="TextBox1" VerticalAlignment="Top" Width="180" Text="Rohatash Kumar" TextWrapping="WrapWithOverflow" Background="Crimson" Foreground="Blue" />

The form looks like this.

txt3.gif

Figure 3.

Wrapping and Scrolling Text:

The TextWrapping attributes sets the wrapping of text and VerticalScrollBarVisibility and HorizontalScrollBarVisibility sets the vertical and horizontal scroll bars visible.

<TextBox Height="38" HorizontalAlignment="Left" Margin="128,90,0,0" Name="TextBox1" VerticalAlignment="Top" Width="180" Text="Rohatash Kumar please enter something in the textbox" TextWrapping="Wrap" Background="Crimson" Foreground="Blue" HorizontalContentAlignment="Center" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Visible" />

The form looks like this.

txt4.gif

Figure 4.

Setting the background image

<TextBox Height="109" HorizontalAlignment="Left" Margin="128,90,0,0" Name="TextBox1" VerticalAlignment="Top" Width="180" Text="Rohatash Kumar please enter something in the textbox" TextWrapping="Wrap" Foreground="Blue" HorizontalContentAlignment="Center" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Visible">

            <TextBox.Background>

                <ImageBrush ImageSource="/WpfApplication18;component/Images/flowers-image.jpg" />

            </TextBox.Background>

        </TextBox>

The form looks like this.

txt5.gif

Figure 5.

Making TextBox as a Masking TextBox

While we take a input from the user it's very necessary that user should type input in correct datatype, mean if integer value required then user should type integer value and if string required then should type string. In WPF I am going to show how we can bound user to do that, by masking textbox.

XAML code

<Window x:Class="MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="MainWindow" Height="350" Width="525">

    <Grid>

        <TextBlock Height="23" HorizontalAlignment="Left" Margin="98,80,0,0" Name="textBlock1" Text="Enter Value:" VerticalAlignment="Top" >

        </TextBlock>

 

        <TextBox Height="23" HorizontalAlignment="Left" Margin="184,80,0,0" Name="textBoxValue" PreviewTextInput="textBoxValue_PreviewTextInput" DataObject.Pasting="textBoxValue_Pasting" VerticalAlignment="Top" Width="120">

 

        </TextBox>

 

    </Grid>

</Window>

 

Add the following code with the event of TextBox.

Private Sub textBoxValue_PreviewTextInput(ByVal sender As Object, ByVal e As TextCompositionEventArgs)
        e.Handled = Not TextBoxTextAllowed(e.Text)
    End Sub

    Private Sub textBoxValue_Pasting(ByVal sender As Object, ByVal e As DataObjectPastingEventArgs)
        If e.DataObject.GetDataPresent(GetType([String])) Then
            Dim Text1 As [String] = DirectCast(e.DataObject.GetData(GetType([String])), [String])
            If Not TextBoxTextAllowed(Text1) Then
                e.CancelCommand()
            End If
        Else
            e.CancelCommand()
        End If
    End Sub

    Private Function TextBoxTextAllowed(ByVal Text2 As [String]) As [Boolean]
        Return Array.TrueForAll(Of [Char])(Text2.ToCharArray(), Function(c As [Char]) [Char].IsDigit(c) OrElse [Char].IsControl(c))
    End Function

 

The form looks like this.


txt6.gif
 

Figure 6.

 

Now run the application and test it. If user try to type string value in this textbox then he can't type. Permission only to type integer value.


txt7.gif
 

Figure 7.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Author
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.