zoukankan      html  css  js  c++  java
  • string 转 label,button等空间类型

    写代码过程中,遇到问题,要定义一个string ,用for循环增量string的序号,对应前台的不同label,由于string类型无法直接转换成label类型。如何转换呢?

    for (int i = 1; i <= numleft; i++)
                            {
                                string str = "label" + i + "";
                                string but = "button_" + i + "";
                                Button bu = grid.FindName(but) as Button;
                                bu.Content = "4535353";
                                string st = "label" +( 20 + i) + "";
                                Label time = grid.FindName(st) as Label;
                                time.Content = "45534";
                                Label la= grid.FindName(str) as Label;
                               la.Content = "" + i + ".rtret" + i + "";
                            }

    可以看到,我前台的label命名规则是label1,label2,label3......,要逐个对每个label进行Content赋值;由于label.name是string型,如何组个增量呢,具体的就看代码的实现了。前提是这些label要在同一个grid下面,具体的看前台代码:

    <Grid  Name="grid">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="20*" />
                <ColumnDefinition Width="761*" />
                <ColumnDefinition Width="19*" />
            </Grid.ColumnDefinitions>
            <!--<Image Name="image1" Stretch="Fill" Source="/Resources/Resources1/1.png" Grid.ColumnSpan="2" />-->
            <Label Height="28" Margin="0,24,0,0" Name="label1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="227" FocusVisualStyle="{x:Null}" Foreground="Brown" FontSize="16px" Grid.ColumnSpan="2"></Label>
            <Label Height="28" HorizontalAlignment="Left" Margin="0,69.001,0,0" Name="label2" VerticalAlignment="Top" Width="227" FocusVisualStyle="{x:Null}" Foreground="Brown" FontSize="16px" Grid.ColumnSpan="2"></Label>
            <Label Height="28" HorizontalAlignment="Left" Margin="0,112,0,0" Name="label3" VerticalAlignment="Top" Width="227" FocusVisualStyle="{x:Null}" Foreground="Brown" FontSize="16px" Grid.ColumnSpan="2"></Label>
            <Label Height="28" HorizontalAlignment="Left" Margin="0,153,0,0" Name="label4" VerticalAlignment="Top" Width="227" FocusVisualStyle="{x:Null}" Foreground="Brown" FontSize="16px" Grid.ColumnSpan="2"></Label>
            <Label Height="28" HorizontalAlignment="Left" Margin="0,196,0,0" Name="label5" VerticalAlignment="Top" Width="227" FocusVisualStyle="{x:Null}" Foreground="Brown" FontSize="16px" Grid.ColumnSpan="2"></Label>
            <Label HorizontalAlignment="Left" Margin="0,242.717,0,229.283" Name="label6" Width="227" FocusVisualStyle="{x:Null}" Foreground="Brown" FontSize="16px" Grid.ColumnSpan="2"></Label>
            <Label HorizontalAlignment="Left" Margin="0,0,0,190" Name="label7" Width="227" FocusVisualStyle="{x:Null}" Height="28" VerticalAlignment="Bottom" Foreground="Brown" FontSize="16px" Grid.ColumnSpan="2"></Label>
            <Label HorizontalAlignment="Left" Margin="0,0,0,144" Name="label8" Width="227" Height="28" VerticalAlignment="Bottom" FocusVisualStyle="{x:Null}" Foreground="Brown" FontSize="16px" Grid.ColumnSpan="2"></Label>
            <Label Height="28" HorizontalAlignment="Left" Margin="0,0,0,103" Name="label9" VerticalAlignment="Bottom" Width="227" FocusVisualStyle="{x:Null}" Foreground="Brown" FontSize="16px" Grid.ColumnSpan="2"></Label>
            <Label Height="26.716" HorizontalAlignment="Left" Margin="0,0,0,60.568" Name="label10" VerticalAlignment="Bottom" Width="227" FocusVisualStyle="{x:Null}" Foreground="Brown" FontSize="16px" Grid.ColumnSpan="2"></Label>
            <Label Height="28" Margin="0,24,153,0" Name="label11" VerticalAlignment="Top" FocusVisualStyle="{x:Null}" Grid.Column="1" Foreground="Brown" FontSize="16px" HorizontalAlignment="Right" Width="215"></Label>
            <Label Height="28" Margin="0,69,153,0" Name="label12" VerticalAlignment="Top" FocusVisualStyle="{x:Null}" Grid.Column="1" Foreground="Brown" FontSize="16px" HorizontalAlignment="Right" Width="215"></Label>
            <Label Height="28" Margin="0,112,153,0" Name="label13" VerticalAlignment="Top" FocusVisualStyle="{x:Null}" Grid.Column="1" Foreground="Brown" FontSize="16px" HorizontalAlignment="Right" Width="215"></Label>
            <Label Height="28" Margin="0,153,153,0" Name="label14" VerticalAlignment="Top" FocusVisualStyle="{x:Null}" Grid.Column="1" Foreground="Brown" FontSize="16px" HorizontalAlignment="Right" Width="215"></Label>
            <Label Height="28" Margin="0,197.717,153,0" Name="label15" VerticalAlignment="Top" FocusVisualStyle="{x:Null}" Grid.Column="1" Foreground="Brown" FontSize="16px" HorizontalAlignment="Right" Width="215"></Label>
            <Label Margin="0,242.717,153,229.283" Name="label16" FocusVisualStyle="{x:Null}" Grid.Column="1" Foreground="Brown" FontSize="16px" HorizontalAlignment="Right" Width="215"></Label>
         
    
  • 相关阅读:
    JS提取子字符串函数比较
    js事件定义方式和获取事件对象event总结
    让body的clientHeight与html的clientHeight相等的方法
    关于原型链和继承问题的思考:为什么不能直接把父类的prototype赋值给子类的prototype
    [javascript权威指南笔记02]Throw语句和异常处理机制try/catch/finally
    转载:javascript语句标签
    转:JS中强大的正则表达式
    分享我常用的Javascript工具函数
    对prototype,instanceof和constrctor的理解
    xml基础知识总结和回顾
  • 原文地址:https://www.cnblogs.com/wandd/p/2704051.html
Copyright © 2011-2022 走看看