Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all :)

[__i am="" sorry="" for="" misspellings="" -="" english="" isn="t my 1st language><br mode="hold" /><br mode="hold" />I" m="" a="" student="" new="" to="" i="ve been trying for the last 24 hours to overcome this problem: <br mode="hold" /><br mode="hold" />Problem description: instade of adding 5 labels that represent scores of random diceThrow() by dragging the label to the Designer... <br mode="hold" />I want to create those 5 Labels - by code - creating an array of labels and generate them and present them during the program runnning time. << This is what I need<<<br mode="hold" /><br mode="hold" />I took the "XMAL" "XAML" code and I am struggling with it" s="" translation="" into="" my="" code="" didn="t learn xmal, but only introduction to programing using C# and WPF].<br mode="hold" />I" using="" wpf="" application="" and="" grid.=""__]

This is the XMAL line I'm trying to replace by code:
// Label Height="28" HorizontalAlignment="Left" Margin="221,98,0,0" Name="lblScoreD6" VerticalAlignment="Top" Width="100" Content="0" /
// Grid.ColumnSpan="4" Grid.Column="1"

This is as far as I managed to go:
[my problem is ** I CAN'T SEE THE LABELS ONCE THE PROGRAM IS RUNNING **]
(mybe it's becouse I don't know how to translate this line into my code:
Grid.ColumnSpan="4" Grid.Column="1" from xmal to c# code)

Please advise..
Thx on advance!
Eyal David,
C#
public MainWindow()
        {      
            InitializeComponent();
            Label[] scoresLabelArr = new Label[5];
            int location = 98;
            for (int i = 0; i < scoresLabelArr.Length; i++)
            {
                Label currentLabel = new Label();
                scoresLabelArr[i] = (Label)currentLabel;
                scoresLabelArr[i].Name = "lblScoreDice" + i;
                scoresLabelArr[i].Height = 28;
                scoresLabelArr[i].Width = 100;
                scoresLabelArr[i].HorizontalAlignment = HorizontalAlignment.Left;
                scoresLabelArr[i].VerticalAlignment = VerticalAlignment.Top;
                scoresLabelArr[i].Content = "0";
                scoresLabelArr[i].Margin = new Thickness(211, location, 0, 0);
                scoresLabelArr[i].Foreground = Brushes.Black;
                
                //Label currentLabel = scoresLabelArr[i];
                // In the designer - i've named the Grid [grid1]
               // Grid grid = new Grid();
                //  grid.Children.Add((Label)scoresLabelArr[i]);
                // grid1.CommandBindings.AddRange(Top.CompareTo) = 
                grid1.Children.Add(currentLabel);
                location += 34;
Posted
Updated 3-Dec-11 15:09pm
v2
Comments
Sergey Alexandrovich Kryukov 2-Dec-11 23:14pm    
What kind of grid is grid1?
--SA

It looks like just the layout problem. Try to use System.Windows.Controls.StackPanel to hold your labels. Also, why labels? You should better use System.Windows.Controls.TextBlock. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.controls.stackpanel.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.aspx[^].

—SA
 
Share this answer
 
XML
Hi SA,
Thx for your knowlage.
I'm afraid I'm too new to this. The grid I'm using is the 1 that you start the application with (or i can simply drag it from the objects bar - it's called: "Grid";
However, if there are several types - I coppied the XMAL info of that grid - maybe it would help u to help me:

   <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="156*" />
            <ColumnDefinition Width="86*" />
            <ColumnDefinition Width="4*" />
            <ColumnDefinition Width="24*" />
            <ColumnDefinition Width="88*" />
            <ColumnDefinition Width="39*" />
        </Grid.ColumnDefinitions>

Can you help me by adding the dirrect commands to my Code to display / layout those labels?
It's improtant for my project to use them as labels instead of textBlock.

Thank you very much!
Eyal :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900