CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2008
    Posts
    111

    Question Programmatically add label to grid using WPF

    Hey all i am trying to add a label to my grid using the following code:
    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
      Dim dynamicLabel As New Label()
      Dim g As New Grid
    
      dynamicLabel.Name = "NewLabel"
      dynamicLabel.Content = "TEST"
      dynamicLabel.Width = 240
      dynamicLabel.Height = 30
      dynamicLabel.Margin = New Thickness(0, 21, 0, 0)
      dynamicLabel.Foreground = New SolidColorBrush(Colors.White)
      dynamicLabel.Background = New SolidColorBrush(Colors.Black)
    
      Grid.SetRow(dynamicLabel, 0)
      Grid.SetColumn(dynamicLabel, 6)
      g.Children.Add(dynamicLabel)
    End Sub
    My WPF code is this:

    Code:
    <Window x:Class="msgWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="msgWindow" Height="110" Width="930"
        WindowStyle="None" AllowsTransparency="True" Background="Transparent" 
        ShowInTaskbar="False" Topmost="True" BorderThickness="1" BorderBrush="Black" 
        Focusable="False" ShowActivated="False" Opacity="1">
        <Grid Width="930" Opacity="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="778*" />
            </Grid.ColumnDefinitions>
            <Label Background="#FFCDCDE5" Content="ID" FontFamily="Arial" FontSize="9" Height="20" HorizontalAlignment="Left" Name="Label1" VerticalAlignment="Top" Width="58" Foreground="White" Margin="0,0,0,0" />
            <Label Background="#FFCDCDE5" Content="Status" FontFamily="Arial" FontSize="9" Height="20" HorizontalAlignment="Left" Name="Label2" Margin="60,0,0,0" VerticalAlignment="Top" Width="76" Foreground="White" />
            <Label Background="#FFCDCDE5" Content="Priority" FontFamily="Arial" FontSize="9" Height="20" HorizontalAlignment="Left" Name="Label3" VerticalAlignment="Top" Width="53" Grid.Column="1" Margin="2,0,0,0" Foreground="White" />
            <Label Background="#FFCDCDE5" Content="Title" FontFamily="Arial" FontSize="9" Height="20" HorizontalAlignment="Left" Name="Label4" Margin="57,0,0,0" VerticalAlignment="Top" Width="310" Grid.Column="1" Foreground="White" />
            <Label Background="#FFCDCDE5" Content="Expire" FontFamily="Arial" FontSize="9" Height="20" HorizontalAlignment="Left" Name="Label5" Margin="369,0,0,0" VerticalAlignment="Top" Width="113" Grid.Column="1" Foreground="White" />
            <Label Background="#FFCDCDE5" Content="Date" FontFamily="Arial" FontSize="9" Height="20" HorizontalAlignment="Left" Name="Label6" Margin="484,0,0,0" VerticalAlignment="Top" Width="122" Grid.Column="1" Foreground="White" />
            <Label Background="#FFCDCDE5" Content="Assigned" FontFamily="Arial" FontSize="9" Height="20" HorizontalAlignment="Left" Name="Label7" Margin="608,0,0,0" VerticalAlignment="Top" Width="183" Grid.Column="1" Foreground="White" />
            <Label Background="Red" Content="X" FontFamily="SansSerif" FontSize="9" Height="20" HorizontalAlignment="Left" Margin="777,0,0,0" Name="Label8" VerticalAlignment="Top" Width="16" Grid.Column="1" Foreground="White" />
            <Button Content="Button" Grid.Column="1" Height="27" HorizontalAlignment="Left" Margin="35,30,0,0" Name="Button1" VerticalAlignment="Top" Width="74" />
        </Grid>
    </Window>
    However, i never see anything on the grid after i push the button... what am i missing?

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Programmatically add label to grid using WPF

    All you are doing now is adding a dynamic label to a grid that doesn't have a parent associated with it. This means that your grid ( with its child label ) is not attached to anything.

    I would first create a new grid dynamically, set all the proper column definitions, then add all its child controls to it, lastly, I would attach the grid to the background.

    Have a look here :

    http://www.c-sharpcorner.com/resourc...namically.aspx

    This would give you a very good idea of what I am talking about. Yes, it is in C#, but the code is basically 100% the same in VB

    I hope it helps!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured