Using wpf tabcontrol and at run time adding a richtexttbox1 to tab control.
now need to save the contents of richtextbox.
with Winforms I used addhandler etc and worked vey well
can achieve ok if richtextbox is added at design time
Please point me in the right direction for C# and wpf
Thank you
Peter Howlett

Jithu ThomasRank #437 · 5.7K reputationPosted Jan 27, 2024, 1:31 PM EST
In WPF, you can dynamically add controls to a TabControl just like in WinForms. To achieve this, you can create a RichTextBox dynamically and add it to the TabItem of the TabControl. Follow the example to how you can achieve this in WPF:
Define your TabControl in XAML:
In your code-behind, add the logic to dynamically create RichTextBox and add it to a new TabItem
using System.Windows; using System.Windows.Controls; namespace YourNamespace { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); // Call this method to dynamically add a RichTextBox to a new tab AddNewTabWithRichTextBox(); } private void AddNewTabWithRichTextBox() { // Create a new TabItem TabItem newTab = new TabItem(); newTab.Header = "New Tab"; // Set the header as needed // Create a new RichTextBox RichTextBox richTextBox = new RichTextBox(); newTab.Content = richTextBox; // Add the TabItem to the TabControl myTabControl.Items.Add(newTab); } // Add a button click event or some other trigger to save the content of the RichTextBox private void SaveRichTextBoxContent_Click(object sender, RoutedEventArgs e) { // Get the currently selected TabItem if (myTabControl.SelectedItem is TabItem selectedTab) { // Get the RichTextBox from the TabItem's content if (selectedTab.Content is RichTextBox richTextBox) { // Access the content of the RichTextBox string content = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd).Text; // Now you can save the content as needed // Add your save logic here } } } } }The example assumes that you have a TabControl named myTabControl in your XAML. It dynamically creates a new TabItem and adds a RichTextBox to it. The SaveRichTextBoxContent_Click method demonstrates how you can access the content of the RichTextBox and save it.