Share via

Visual Basic dynamic add object

Marek Borkowski 26 Reputation points
2021-07-09T14:19:31.82+00:00

How can i dynamicly contrlols in this example? I was searching and I tried

Imports Windows.UI.Popups
Imports Windows.UI
Imports Windows.UI.Xaml.Controls.ToolTip
Imports Windows.UI.Xaml.Shapes
Imports Windows.UI.Xaml.Controls.Image
'Szablon elementu Pusta strona jest udokumentowany na stronie https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x415

''' <summary>
''' Pusta strona, która może być używana samodzielnie lub do której można nawigować wewnątrz ramki.
''' </summary>
Public NotInheritable Class MainPage
Inherits Page

Public Sub New()
    InitializeComponent()
End Sub

Private Sub Button3_Click(sender As Object, e As RoutedEventArgs) Handles Button3.Click
    Dim image1 As Image
    image1 = New Image
    image1.Source = New BitmapImage(New Uri("ms-appx:/Assets/tabela.png", UriKind.Absolute))
    image1.Visibility = Visibility.Visible
    image1.Width = 200
    image1.Height = 300
    image1.MinHeight = 200
    image1.MinWidth = 300
End Sub

Private Sub Button4_Click(sender As Object, e As RoutedEventArgs) Handles Button4.Click
    Dim toltip1 As ToolTip
    toltip1 = New ToolTip
    toltip1.Content = "Text"
    toltip1.FontSize = 24
    toltip1.Visibility = Visibility.Visible
End Sub

End Class

Developer technologies | Universal Windows Platform (UWP)
0 comments No comments

Answer accepted by question author

Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,871 Reputation points
2021-07-12T01:40:36.43+00:00

Hello, Welcome to Micorosoft Q&A,

Visual Basic dynamic add object

You need declare RootGrid in the xaml like following, and add image1 into RootGrid children collection.

<Grid x:Name="RootGrid">  
    <Button  
        VerticalAlignment="Top"  
        Click="Button_Click"  
        Content="Add" />  
    <Button  
        HorizontalAlignment="Center"  
        VerticalAlignment="Top"  
        Click="Button_Click_1"  
        Content="ToolTip" />  
</Grid>  

Code Behind

Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)  
    Dim image1 = New Image()  
    image1.Source = New BitmapImage(New Uri("ms-appx:/Assets/StoreLogo.png", UriKind.Absolute))  
    image1.Visibility = Visibility.Visible  
    image1.Width = 200  
    image1.Height = 300  
    image1.MinHeight = 200  
    image1.MinWidth = 300  
    image1.VerticalAlignment = VerticalAlignment.Center  
    image1.HorizontalAlignment = HorizontalAlignment.Center  
    RootGrid.Children.Add(image1)  
End Sub  

Add ToolTip

Private Sub Button_Click_1(ByVal sender As Object, ByVal e As RoutedEventArgs)  
    Dim toltip1 As ToolTip = New ToolTip()  
    toltip1.Content = "Text"  
    toltip1.FontSize = 24  
    toltip1.Visibility = Visibility.Visible  
    ToolTipService.SetToolTip(TryCast(sender, Button), toltip1)  
End Sub  

If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.