user1786272
user1786272

Reputation:

Adding WPF controls with VB

I'm new here!

My problem is the following piece of XAML:

<ScrollViewer>
    <Label x:Name="MainText">
        <TextBlock x:Name="DefaultText">
            Application launched successfully!
        </TextBlock>
    </Label>
</ScrollViewer>
<TextBox x:Name="EntryText" KeyDown="EntryText_KeyDown" />

I want to add TextBlocks from EntryText_KeyDown inside MainText. Although it's able to access both MainText and DefaultText I don't know how I can add an element. Googling seems to provide the C# solution of MainText.Add which doesn't seem to be usable(?) in VB.

Any help would be much appreciated!

Upvotes: 1

Views: 200

Answers (1)

Nitin Purohit
Nitin Purohit

Reputation: 18578

Label can have only one child, so if you want to have multiple textblocks use layout containers like panels, grid

<ScrollViewer>

    <StackPanel x:Name="MainText">
        <TextBlock x:Name="DefaultText">
            Application launched successfully!
        </TextBlock>
    </StackPanel >
</ScrollViewer>

Then in code behind you can do:

MainText.Children.Add(new TextBlock());

If you want to add just one textblock in label then you can do:

TextBlock textblock = new TextBlock();
textblock.Text = "My Text";
MainText.Content = textblock ;

Upvotes: 2

Related Questions