zoukankan      html  css  js  c++  java
  • Working with the TextBox Control In Silverlight 4.0

    This exercise demostrates the use of the TextBox control in Silverlight by creating a simple application that will request the red,green,and blue values to fill an ellipse with a given color. The resulting application will appear as shown below.

    Here is the codes.

    1. In Visual Studio, create a new Silverlight application named TextBoxControl. Allow Visual Studio to create a Web Application project to host your application.

    2. In the MainPage.xaml file, within the root Grid element, add three RowDefinition items, as follows:

      <Grid x:Name="LayoutRoot" Background="White">

        <Grid.RowDefinitions>

          <RowDefinition Height = "50" />

          <RowDefinition Height = "50" />

          <RowDefinition />

        </Grid.RowDefinitions>

      </Grid>

      Add three Textbox and textarea controls contained in the horizontal-oriented StackPanel to the first row, a Button control to the second row, and an Ellipse control to the third row. In addition, plcace a textblock in the thrid row to stack on top of the Ellipse control for error-reporting purposes. Name each of the TextBox controls, as well as the Button control and the TextBlock. These additions are shown in the following code:

      <Grid x:Name="LayoutRoot" Background="White">

        <Grid.RowDefinitions>

          <RowDefinition Height = "50" />

          <RowDefinition Height = "50" />

          <RowDefinition />

        </Grid.RowDefinitions>

        <StackPanel Orientation = "Horizental" HorizontalAlignment = "Center">

          <TextBlock VerticalAlignment = "center" Text="Red:" />

          <TextBox Name="txtRed" Height = "24" Width = "50" Margin="5" />

          <TextBlock VerticalAlignment = "center" Text="Green:" />

          <TextBox Name="txtGreen" Height="24" Width="50" Margin="5" />

          <TextBlock VerticalAlignment = "Center" Text="Blue:" />

          <TextBox Name="txtBlue" Height="24" Width="50" Margin="5" />

        </StackPanel>

        <Button Name="btnTry" Content="Try Color" Height="24" Width="100" Grid.Row="1" />

        <Ellipse Name="ellipse" Grid.Row="2" Stroke="Black" StrokeThickness="5" Margin="20" />

        <TextBlock Name="lblColor" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="20" FontFamily="Arial" FontWeight="Bold" />

      </Grid>

      Now add the Click event to the Button control. Do this in the code behind.

      public partial class MainPage: UserControl

      {

        public MainPage()

        {

          InitializeComponent();

          this.btnTry.Click==new RoutedEventHandler(btnTry_Click);

        }

        void btnTry_Click(object sender, RoutedEventArgs e)

        {

          throw new NotImplementedException();

        }

      }

    3. When the button is clicked, the application will change the Fill property of the ellipse control, which expects a SolidColorBrush(). You can create the SolidColorBrush using the Colors.FromArgb() method, which accepts four arguments: one for opacity, and one byte each for the red, green, and blue values.  You will get the red, green, and blues values from the TextBox controls using the Text property.

      void btnTry_Click(object sender, RoutedEventArgs e)

      {

        this.ellipse.Fill = new SolidColorBrush(Color.FromArgb(255, byte.Parse(this.txtRed.Text), byte.Parse(this.txtGreen.Text), byte.Parse(this.txtBlue.Text)));

      }

      Since the values for red, green and blue must be an integer from 0 to 255, you can either validate them using Silverlight validation or take the easy way out and just wrap your code in a try/catch block, and then report the error using the TextBlock. you'll go with the latter approch here. To keep things clean, you will make sure the error message is cleared if all workds correctly. Here is the updated code:

      void btnTry_Click(object sender, RoutedEventArgs e)

      {

        try

        {

          this.ellipse.Fill = new SolidColorBrush(Color.FromArgb(255, byte.Parse(this.txtRed.Text), byte.Parse(this.txtGreen.Text), byte.Parse(this.txtBlue.Text)));

          this.lblColor.Text="";

        }

        catch

        {

          this.lblColor.Text="Error with R,G,B Values" ;

        }

      }

     4. Build and run the application to see what you get. Type 255, 0 and 0 in the Red, Green, and Blue text boxes, respectively, and then click the Try Color button .You shoud see the ellipse turn red. Just fro the fun of it, if you leave one of the values blank or enter a value other than 0 through 255, you will see the error message.

  • 相关阅读:
    QT两个字符串转化函数,避免文字乱码。
    QT隐藏工具栏上的右键菜单
    QT线程初次使用。遇到的问题。
    QTableView根据标题文字和表格文字自适应宽度
    void QTableView::setColumnWidth ( int column, int width),隐藏列不起作用
    在VS2010中去掉ipch和sdf文件方法
    QT的一个奇怪问题,设置了Qt::Tool后,点击弹出对话框的确定取消按钮,程序直接退出
    上传图片并显示缩略图的最简单方法(c#)
    总结一下散乱的开发点滴(2) (高手勿入)
    在global里捕获黄页并写入异常日志库
  • 原文地址:https://www.cnblogs.com/jerrychenfly/p/InputControl.html
Copyright © 2011-2022 走看看