Reputation: 309
I have an application which has a basic text editor features. I use System.Windows.Controls richtextbox for the purpose of opening files. Currently my application can only open one file at a time. However I want to enhance the applicatin to open multpile files in multiple tabs. How do I implement multiple tab feature? I am pretty new to WPF framework.Any help is appreciated. The code I have is as below:
<Window x:Class="Editor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Pattern Editor" Height="400" Width="600"
>
<Grid>
<!-- Set the styles for the tool bar. -->
<Grid.Resources>
<Style TargetType="{x:Type Button}" x:Key="formatTextStyle">
<Setter Property="FontFamily" Value="Palatino Linotype"></Setter>
<Setter Property="Width" Value="30"></Setter>
<Setter Property="FontSize" Value ="14"></Setter>
<Setter Property="CommandTarget" Value="{Binding ElementName=mainRTB}"></Setter>
</Style>
<Style TargetType="{x:Type MenuItem}" x:Key="formatImageStyle">
<Setter Property="Width" Value="30"></Setter>
<Setter Property="CommandTarget" Value="{Binding ElementName=mainRTB}"></Setter>
</Style>
</Grid.Resources>
<DockPanel Name="mainPanel" >
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Header="_New" Click="New_Click"/>
<Separator />
<MenuItem Header="_Open" Click="Open_Click"/>
<Separator />
<MenuItem Header="_Save" Command="ApplicationCommands.Save" ToolTip="Save" Click="Save_Click">
<MenuItem.Icon>
<Image Source="C:\Users\Documents\Visual Studio 2012\Projects\Editor\Editor\Images\FileSave.png" Height="21"></Image>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="_Save As" Click="Save_As_Click">
<MenuItem.Icon>
<Image Source="C:\Users\Documents\Visual Studio 2012\Projects\Editor\Editor\Images\FileSaveAs.png" Height="21"></Image>
</MenuItem.Icon>
</MenuItem>
<Separator />
<MenuItem Header="_Close" Click="Close_Click"/>
<Separator />
<MenuItem Header="_Exit" Click="Exit_Click"/>
</MenuItem>
<MenuItem Header="_Edit">
<MenuItem Header="_Cut" Command="ApplicationCommands.Cut" ToolTip="Cut">
<MenuItem.Icon>
<Image Source="C:\Users\Documents\Visual Studio 2012\Projects\Editor\Editor\Images\EditCut.png" Height="21"></Image>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="_Copy" Command="ApplicationCommands.Copy" ToolTip="Copy">
<MenuItem.Icon>
<Image Source="C:\Users\stambi\Documents\Visual Studio 2012\Projects\PatternEditor\PatternEditor\Images\EditCopy.png" Height="21"></Image>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="_Paste" Command="ApplicationCommands.Paste" ToolTip="Paste">
<MenuItem.Icon>
<Image Source="C:\Users\Documents\Visual Studio 2012\Projects\Editor\Editor\Images\EditPaste.png" Height="21"></Image>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="_Undo" Command="ApplicationCommands.Undo" ToolTip="Undo">
<MenuItem.Icon>
<Image Source="C:\Users\stambi\Documents\Visual Studio 2012\Projects\PatternEditor\PatternEditor\Images\EditUndo.png" Height="21"></Image>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="_Redo" Command="ApplicationCommands.Redo" ToolTip="Redo">
<MenuItem.Icon>
<Image Source="C:\Users\Documents\Visual Studio 2012\Projects\Editor\Editor\Images\EditRedo.png" Height="21"></Image>
</MenuItem.Icon>
</MenuItem>
</MenuItem>
<MenuItem Header="_Tools">
<MenuItem Header="_DisplayFormat">
<MenuItem Header="_Binary" IsCheckable="True"/>
<MenuItem Header="_Hexadecimal" IsCheckable="True"/>
</MenuItem>
<MenuItem Header="_Split" IsCheckable="True" Click="Split_Click"/>
<MenuItem Header="_HighLight"/>
<MenuItem Header="_Show Linenumbers"/>
</MenuItem>
</Menu>
<!--By default pressing tab moves focus to the next control. Setting AcceptsTab to true allows the
RichTextBox to accept tab characters. -->
<RichTextBox Name="mainRTB" AcceptsTab="True" FontFamily="Arial" FontStretch="Normal" VerticalScrollBarVisibility="Auto"></RichTextBox>
</DockPanel>
</Grid>
</Window>
Upvotes: 0
Views: 1640
Reputation: 339
Id start with Creating a TabControl in WPF,
<TabControl Height="500" Width="Auto" FontSize="14" Name="Mytabcontrol"> </TabControl>
Then in your c# code add something like:
private void AddTabitem()
{
TabItem ti = new TabItem();
ti.Header = "Tab";
ti.Content = // Your richtextbox;
Mytabcontrol.Items.Insert(Mytabcontrol.Items.Count, ti);
Mytabcontrol.SelectedIndex = Mytabcontrol.Items.Count - 1;
And then a SelectionChanged Event
Mytabcontrol.SelectionChanged += new SelectionChangedEventHandler(TabSelectionChanged);
}
This code might also get in handy:
void TabSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (Mytabcontrol.SelectedItem != null)
{
TabItem ti= Mytabcontrol.SelectedItem as TabItem; // Selected Tab
if (tabitem.Content != null)
{
RichTextBox txt = ti.Content as RichTextBox; // your textbox
}
}
}
Upvotes: 2
Reputation: 806
you will need a TabControl
1) add a tabControl to your window
2) add this code in your window source code file
//code for adding tabItems to your tabControl
//when you open a file, string[] richtextBoxLines are the lines
public TabItem TItem(string HeaderText, string[] richtextBoxLines)
{
TabItem t = new TabItem();
t.Header = HeaderText;
RichTextBox r = new RichTextBox();
foreach(string s in richtextBoxLines)
{
r.Selection.Text += s;
}
t.Content = r;
return t;
}
//when you just add a tab
public TabItem TItem(string HeaderText)
{
TabItem t = new TabItem();
t.Header = HeaderText;
RichTextBox r = new RichTextBox();
t.Content = r;
return t;
}
now, you can just add tabs like in the examples below
//read text file text to get an array
string[] allLines = null; //set to value of textfile lines
//you can add a tab with this text
myTabControl.Items.Add(TItem("FileName", allLines));
//or you could also add a tab by creating a new text file
myTabControl.Items.Add(TItem("new Textfile"));
Upvotes: 0