Blue Theme Orange Theme Green Theme Red Theme
 
Discover the top 5 tips for understanding .NET Interop
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | 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 » WPF RepeatButton

WPF RepeatButton

This article demonstrates how to create and use a RepeatButton control in WPF using XAML and C#.

Author Rank :
Page Views : 7955
Downloads : 112
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
RepeatButtonSample.zip
 
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


Creating a RepeatButton

The RepeatButton XAML element represents a WPF RepeatButton control.

 

<Button/>

 

The Width and Height attributes represent the width and the height of a RepeatButton. The Content property sets the text of button. The Name attribute represents the name of the control, which is a unique identifier of a control.

 

The code snippet in Listing 1 creates a Button control and sets its name, height, width, and content.

 

<RepeatButton Margin="10,10,0,0" VerticalAlignment="Top"

              HorizontalAlignment="Left"                      

              Name="GrowButton"  Width="80" Height="30">

</RepeatButton>

Listing 1

The default property of a button is Content. The code snippet in Listing 2 creates the same button as created by Listing 1.

<RepeatButton Margin="10,10,0,0" VerticalAlignment="Top"

              HorizontalAlignment="Left"                      

              Name="GrowButton"  Width="80" Height="30">

    Grow

</RepeatButton>

Listing 2

The output looks like Figure 1.

RBFig1.gif

Figure 1

Delay and Interval

The Delay and Interval properties make a RepeatButton different than a normal button.

A RepeatButton is a button that fires Click events repeatedly when it is pressed and held. The rate and aspects of repeating are determined by the Delay and Interval properties that the control exposes.

The code snippet in Listing 3 sets the Delay and Interval properties.

<RepeatButton Margin="10,10,0,0" VerticalAlignment="Top"

              HorizontalAlignment="Left"                      

              Name="GrowButton"  Width="80" Height="30"

              Delay="500" Interval="100"

              >

    Grow

</RepeatButton>

Listing 3

Adding a Button Click Event Handler

The Click attribute of a RepeatButton element adds the click event handler and it keeps firing the event for the given Interval and delay values. The code in Listing 4 adds the click event handler for a Button.

<Button x:Name="DrawCircleButton" Height="40" Width="120"

        Canvas.Left="10" Canvas.Top="10"

        Content="Draw Circle"

        VerticalAlignment="Top"

        HorizontalAlignment="Left">

Click="DrawCircleButton_Click"

</Button>

Listing 4

The code for the click event handler looks like following.

private void GrowButton_Click(object sender, RoutedEventArgs e)

{

   

}

OK now let's write a useful application.

We are going to build an application with two buttons - Grow and Shrink and a rectangle. The application looks like Figure 2.

RBFig2.gif

Figure 2

When you click and keep pressing the Grow button, the width of rectangle will keep growing and when you click on the Shrink button, the width of rectangle will keep shrinking.

The final XAML code is listed in Listing 5.

<Window x:Class="RepeatButtonSample.Window1"

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

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

    Title="Window1" Height="300" Width="300">

   

    <Grid Name="LayoutRoot">

        <RepeatButton Margin="10,10,0,0" VerticalAlignment="Top"

                      HorizontalAlignment="Left"                      

                      Name="GrowButton"  Width="80" Height="30"

                      Delay="500" Interval="100"

                      Click="GrowButton_Click">

            Grow

        </RepeatButton>

        <RepeatButton Margin="100,10,0,0" VerticalAlignment="Top"

                      HorizontalAlignment="Left"                      

                      Name="ShrinkButton"  Width="80" Height="30"

                      Delay="500" Interval="100"

                      Click="ShrinkButton_Click">

            Shrink

        </RepeatButton>

       

        <Rectangle Name="Rect" Height="100" Width="100" Fill="Orange"/>

 

    </Grid>

</Window>

Listing 5

Listing 6 is the click event handlers for the buttons that change the width of rectangle.

private void GrowButton_Click(object sender, RoutedEventArgs e)

{

    Rect.Width += 10;

}

 

private void ShrinkButton_Click(object sender, RoutedEventArgs e)

{

    Rect.Width -= 10;

}

Listing 6

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
 
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle.  If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
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.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
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:
DevExpress Free UI Controls
Become a Sponsor
 Comments
test by Mahesh On July 7, 2009
test
Reply | Email | Modify 
question by Deepak On May 10, 2011
how we can control the shrinking (or growing) of rectangle so it doesn't get disappear from window(or it doesn't grow big than the size of window)?
Reply | Email | Modify 
Re: question by Mahesh On May 10, 2011
You can compare the width of rectangle with the width of the window. Something like this. You may need to fix the exact syntax. if (this.Width >= (Rect.Width + 10)) { Rect.Width += 10; } Apply reverse logic on the shrink button click.
Reply | Email | Modify 
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.