proah
proah

Reputation: 354

How to create multiple buttons from existing strings in .txt file

I wonder how I can create buttons in my Toolbar by reading lines from a .txt file. For example:

//bookmarks.txt

http://example.com
http://example2.com
http://example3.com
...

What I want is that my program on start should create a button for each line in my .txt with this event:

public void Button_Click(object sender, RoutedEventArgs e) //fire bookmark event
{
    string text = e.Source.ToString().Replace("System.Windows.Controls.Button: ", "");  
    WebBrowser1.Navigate(text);

}

UPDATE

This is how I read the .txt:

for (int i = 0; i < File.ReadLines(@"bookmarks.txt").Count(); i++)
{
      //Add button right here
}

Upvotes: 0

Views: 315

Answers (2)

Sheridan
Sheridan

Reputation: 69979

You're trying to use WPF as if it were WinForms. This is how you would fulfil your requirements in WPF... first create a DependencyProperty collection in your Window code behind and populate it with your text entries:

public static DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(ObservableCollection<string>), typeof(YourWindowOrUserControl));

public ObservableCollection<string> Items
{
    get { return (ObservableCollection<string>)GetValue(ItemsProperty); }
    set { SetValue(ItemsProperty, value); }
}

...

Items = new ObservableCollection<string>(File.ReadLines(@"bookmarks.txt"));

Then you simply data bind the collection to the ToolBar.ItemsSource property and declare a DataTemplate to define what each string should look like... in your case, we'll set it as the text in a Button:

<ToolBar ItemsSource="{Binding Items}">
    <ToolBar.ItemTemplate>
        <DataTemplate>
            <Button Content="{Binding}" Margin="1,0,0,0" />
        </DataTemplate>
    </ToolBar.ItemTemplate>
</ToolBar>

Of course, you'll need to set the Window.DataContext to the class with your properties... the simplest way is to set it in the code behind constructor like this:

public YourWindowOrUserControl
{
    InitializeComponent();
    DataContext = this;
}

You must read up about how to set the DataContext properly though, as setting it this way is easy, but not necessarily correct.

Finally, you could create a class with all the necessary properties for the Button... for example, you could add a property named Text and another called Command and then make your Items property a collection of those. Then you could data bind to it like this:

<ToolBar ItemsSource="{Binding Items}">
    <ToolBar.ItemTemplate>
        <DataTemplate>
            <Button Content="{Binding Text}" Command="{Binding Command}" Margin="1,0,0,0" />
        </DataTemplate>
    </ToolBar.ItemTemplate>
</ToolBar>

Upvotes: 1

AsfK
AsfK

Reputation: 3476

You can create buttons dynamic and add click event on fly:

Button btn = new Button();
btn.Location = new Point(yourX, yourY);
btn.Font = new Font(btn.Font.Name, 10);
btn.Text = "Text from your txt file here";
btn.ForeColor = Color.SeaShell; // choose color
btn.AutoSize = true;
btn.Click += (sender, eventArgs) =>
                {
                    string text = btn.Text.Replace("System.Windows.Controls.Button: ", "");  
                    WebBrowser1.Navigate(text);
                };

(Insert this code in your For. Btw, you can replace the for with while. see this link)

Upvotes: 0

Related Questions