Sadeep Weerasinghe
Sadeep Weerasinghe

Reputation: 1087

Adding controls to a from C# 2015, this.Controls.Add(bla) method not found?

public partial class MainWindow : Window
{
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        WebBrowser wb = new WebBrowser();
        this.Controls.Add(wb);
    }
}

This results in an error:

'MainWindow' does not contain a definition for 'Controls' and no extension method accepting a first argument of type 'MainWindow' could be found

Whats wrong here? This is a WPF Application. I'm new to C#. I find it on internet that to add controls to the form the function is

this.Controls.Add(fdfdf)

But here this doesn't contain Controls.

Upvotes: 1

Views: 135

Answers (2)

Salah Akbari
Salah Akbari

Reputation: 39976

In WPF it should be Children. In WPF you need to add items as Childrens of layout panels like your main Grid. For example if you have a Grid set it's name to grid1 and then in the code you can:

grid1.Children.Add(fdfdf)

Upvotes: 2

Marius
Marius

Reputation: 577

You can add a component like your WebBrowser directly to the content of the Window.

In WPF you are doing it like this:

public partial class MainWindow : Window
{
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        WebBrowser wb = new WebBrowser();
        this.Content = wb;
    }
}

But I suggest to do this via the XAML.

Upvotes: 2

Related Questions