I have seen this one before. We want a dedicated tab item with a '+' symbol on it. Like chrome browser in Silver light Application.
There is always a tab item appears at the end half visible and once you click on it, it turns into a complete tab item.
That was a little though to answer :P
Here it goes:
Create a class called LinqToVisualTree. You can find it at the end of this post, along with an explanation of what it does. Basically, it lets you query your Visual Tree through LINQ.
To add anything to the tabs row in a TabControl, you need to manipulate the TabPanel, which holds the "buttons" of tabs. TabPanel is in the System.Windows.Controls.Primitives namespace, so refer to it.
The easiest way to get the TabPanel I've found is to name at least one of your TabItems and do this:
using System.Windows.Controls.Primitives; // Contains TabPanel
using LinqToVisualTree;
void AddPlusButton() {
// Creates a button beside the tabs
var button = new Button()
{
Content = "+",
IsTabStop = false // To prevent keyboard press
};
// Links the Click with the "new tab" function
button.Click += new RoutedEventHandler(btPlus_Click);
// *** HERE IS THE TRICK ***
// Gets the parent TabPanel in the Visual Tree and cast it
var tabpn = tabItem1.Ancestors<TabPanel>().FirstOrDefault() as TabPanel;
// Links the button created
tabpn.Children.Add(button);
}
Here's the method for the plus button:
void btPlus_Click(object sender, RoutedEventArgs e)
{
// Creates a new TabItem
var ti = new TabItem();
ti.Header = "TabAdded";
ti.Content = new TextBlock() { Text = "Tab content!" };
// Links it
tabControl.Items.Add(ti);
}
That's it! Tip: I've just find out about the TabPanel class using Silverlight Spy. Searching on Google, I could just find methods of doing this by changing the Template Style from the TabControl.
Best regards!
Related
Edit:
This is not a duplicate of Icons in TabControl C# - How?. The question there is about adding icons to tab pages. Here it is about how the change the error provider error icon position to be inside the header instead of to the right of the tab page itself. Also, the error provider error icon has the functionality that when you hover the mouse on it, you see the error text, which you do not see if you simply add an icon to the header.
I have a form with a TabControl. The form has also an ErrorProvider. When I try to use the following code:
errorProvider1.SetError(tabPage1, "error");
The error icon is shown to the right of the tab page, and it is cut-off by the tab control itself:
I would like the icon to be shown next to the tab page header. Something like this (made with Photoshop):
I do not know where to start, or how to approach this.
Edit:
I have a class responsible for adding errors to a control, and showing them using an error provider. This class is used for TextBoxes, NumericUpDowns etc. I would like to use it also for TabPages. The problem is that when I use it for tab pages I get the result shown above. The trick of adding an error icon to the header using an ImageList and then add a tooltip is not good, because it is specific to tab pages, and I cannot implement it in my class which is general to all controls. So I really need to change the settings of the tab page so when I use errorProvider.SetError(...) it is shown in the header.
ErrorProvider shows error icon of the TabPage in tab page's client area.
By playing with IconAlignment or IconPadding, you can show error icon of TabControl on one of the tab pages' headers, but it's error icon for the whole TabControl.
In a real application each of the tab pages can contain controls which are not valid and you may want to show the validation icon on tab pages not for the tab control.
My suggestion is using tab page icon by setting ImageList containing the error icon as image list of the TabControl and by setting ImageIndex of the TabPage, show or hide the image icon. This way you can show the error icon for every tab page which needs it:
Example
To setup the example, follow these steps:
Create a Form.
Drop a TabControl, an ErrorProvider and an ImageList on the Form.
Set ImageList property of tabControl1 to imageList1.
Drop two TextBox on tabPage1.
I assume, just for example, you are going to validate these two text box controls using Validating event. The key point is here. When you validate any control, check if it's hosted in a TabPage, check validity of all children of TabPage and set the error icon based on that:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.AutoValidate = AutoValidate.EnableAllowFocusChange;
imageList1.ColorDepth = ColorDepth.Depth32Bit;
imageList1.Images.Add(errorProvider1.Icon);
tabControl1.ImageList = imageList1;
textBox1.Validating += textBox_Validating;
textBox2.Validating += textBox_Validating;
}
private void textBox_Validating(object sender, CancelEventArgs e)
{
var textBox = (TextBox)sender;
if (string.IsNullOrEmpty(textBox.Text))
{
this.errorProvider1.SetError(textBox, "Value is required.");
e.Cancel = true;
}
else
this.errorProvider1.SetError(textBox, null);
var tabPage = textBox.Parent as TabPage;
if (tabPage != null)
ValidateTabPage(tabPage);
}
void ValidateTabPage(TabPage tabPage)
{
var tabIsValid = tabPage.Controls.Cast<Control>()
.All(x => string.IsNullOrEmpty(errorProvider1.GetError(x)));
if (tabIsValid)
tabPage.ImageIndex = -1;
else
tabPage.ImageIndex = 0;
}
You can do the following.
Rectangle rc = tabControl1.GetTabRect(0); // Replace with the index of Tab required
errorProvider1.SetIconPadding(tabControl1, -rc.Left-20);;
errorProvider1.SetError(tabControl1, "Error String");
You also need to set
errorProvider1.SetIconAlignment(tabControl1, ErrorIconAlignment.TopLeft);
Sample (With Second Tab Selected - based on comments),
You would need to prepend whitespace to your TabPage Text to ensure there is sufficient space for showing the icon
With Icon on Second Tab
I'm attempting to get the TextEditor control off of the currently selected tab in my tab control. The tabs and text editors are created dynamically so simply referencing the text editor isn't an option. I've searched far and wide and so far, no answer has helped me.
The following code works for Winforms, but not WPF:
var currentTextEdit = tabControl.SelectedTab.Controls.OfType<TextEditor>().First();
Is there something along these lines that perhaps I'm missing?
This is how I'm creating each tab and adding a TextEditor control to each tab created:
TabControl itemsTab = (TabControl)this.FindName("tabControl");
TextEditor textEdit = new TextEditor();
Then to create the new tab and add the text editor:
TabItem newTab = new TabItem();
newTab.Content = textEdit;
itemsTab.Items.Add(newTab);
Further down in the code I get the currently selected tab like so:
TabItem ti = tabControl.SelectedItems as TabItem;
And using the GetChildOfType extension method, I'm attempting to get the current text editor like so:
var currentTextEditor = ti.GetChildOfType<TextEditor>();
This code returns the NullReferenceException:
File.WriteAllText(saveF.FileName, currentTextEditor.Text);
Indeed I wrote a not right thing in my comment. TabControl works in a little different way in comparing with other controls. It has a collection of TabItems. TabControl can show every header of each TabItem which belongs to its collection.
At the same time TabControl "grabs" the selected TabItem's content and add it to its ContentPresenter (it is called PART_SelectedContentHost - just use ILSpy).
So, returning to your issue, you have to search for your TextEditor directly in the TabControl. Then you can use this code:
TabControl itemsTab = (TabControl)FindName("tabControl");
TextEditor currentTextEditor = itemsTab.GetChildOfType<TextEditor>();
if (currentTextEditor != null)
{
File.WriteAllText(saveF.FileName, currentTextEditor.Text);
}
You should always check if the object you obtain from GetChildOfType<T> method is not null, since if GetChildOfType<T> cannot find a control whose type is T, it returns null.
As I told in my previous comment you can find here the code of GetChildOfType<T>.
I hope this answer can help you.
When working with WPF I usually use the method
var currentTextEdit = tabControl.SelectedTab.Children.OfType<TextEditor>().First();
I'm just learning how to make universal apps for windows 10. Most of the tutorials show you how to work with XAML and how to assign functions to elements when you click on them but I couldn't find a way to make a new control appear when I click a button.
I'm making a note taking application. I've designed most of the stuff that I need. Now I want whenever I click a button to create a new textblock where the user can write their note.
//Create a new note when clicking the add button
private void newNoteBtn_Click(object sender, RoutedEventArgs e)
{
TextBox newNote = new TextBox();
newNote.Text = "Enter your text";
}
This is the code that runs when the button is clicked. When I run the application nothing happens. I think I have to put the new textbox in some kind of Grid or something.
Most of the tutorials are really old or mention windows forms and use some sort of this.Controls.Add(newNote); but Visual studio doesn't give me the Controls.Add option. I've also created a <Grid x:Name="notes"></Grid> which I thought I could use as a placeholder for the notes that are being created but I can't access the Grid element through the code.
Container Controls like Grid have Children property so you should use Childern like this:
TextBox newNote = new TextBox();
newNote.Text = "Enter your text";
notes.Childern.Add(newNote);
When defining
<Grid x:Name="notes"></Grid>
in XAML on the page, you be able to use notes as the identifier to access this Grid from the page's code behind:
notes.Children.Add(newNote);
I searched the internet for this but i couldn't find how to do it with C#
What i am trying to do is make it so that when i click on my NewTab button, a new tab appears with the same controls that were on the first tab. I saw some information on how to add a UserControl to your form, but C# doesn't have anything like that.
And for everyone who would say "Post your code", i don't have any, so don't bother saying that, the only code i have is the code for the program and that wouldn't help anyone.
EDIT
I have rewritten my solution to use reflection.
using System.Reflection;
// your TabControl will be defined in your designer
TabControl tc;
// as will your original TabPage
TabPage tpOld = tc.SelectedTab;
TabPage tpNew = new TabPage();
foreach(Control c in tpOld.Controls)
{
Control cNew = (Control) Activator.CreateInstance(c.GetType());
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(c);
foreach (PropertyDescriptor entry in pdc)
{
object val = entry.GetValue(c);
entry.SetValue(cNew, val);
}
// add control to new TabPage
tpNew.Controls.Add(cNew);
}
tc.TabPages.Add(tpNew);
Some information can be found here.
http://www.codeproject.com/Articles/12976/How-to-Clone-Serialize-Copy-Paste-a-Windows-Forms
Your best bet would be to look at this article:
Code Project
Then apply the following code to add the cloned control (this would be in your button click handler code (based on article):
private void button1_Click(object sender, EventArgs e)
{
// create new tab
TabPage tp = new TabPage();
// iterate through each control and clone it
foreach (Control c in this.tabControl1.TabPages[0].Controls)
{
// clone control (this references the code project download ControlFactory.cs)
Control ctrl = CtrlCloneTst.ControlFactory.CloneCtrl(c);
// now add it to the new tab
tp.Controls.Add(ctrl);
// set bounds to size and position
ctrl.SetBounds(c.Bounds.X, c.Bounds.Y, c.Bounds.Width, c.Bounds.Height);
}
// now add tab page
this.tabControl1.TabPages.Add(tp);
}
Then you would need to hook the event handlers up. Will have to think about this.
I know it's an old thread but I just figured out a way for myself and thought I should share it. It's really simple and tested in .Net 4.6.
Please note that this solution does not actually create new controls, just re-assigns them all to new TabPage, so you have to use AddRange each time you change tabs. New tab will show the exact same controls, content and values included.
// Create an array and copy controls from first tab to it.
Array tabLayout = new Control [numberOfControls];
YourTabControl.TabPages[0].Controls.CopyTo(tabLayout, 0);
// AddRange each time you change a tab.
YourTabControl.TabPages[newTabIndex].Controls.AddRange((Control[])tabLayout);
I'm trying to programmatically add a ContextMenu to a Hyperlink. I've searched in the documentation and in forums, and seems that the correct solution should be this:
var link = new Hyperlink();
link.Inlines.Add(new Run() { Text = linkText });
link.FontWeight = FontWeights.Bold;
link.TargetName = linkText;
link.Click += new RoutedEventHandler(link_Click);
ContextMenu menu = new ContextMenu();
MenuItem item = new MenuItem();
item.Click += new RoutedEventHandler(CopyLink);
item.Header = "copy link";
item.Tag = linkText;
menu.Items.Add(item);
ContextMenuService.SetContextMenu(link, menu);
This code compiles and does not throw any exception. I've tested and the ContextMenu is indeed added to the Hyperlink. The problem is that it will not show anytime. I can tap & hold the link all the time of the world and the menu will not appear. Also tried to add a listener with GestureService and GestureListener, but the Hold event does not fire at all.
Anyone can help me? Thanks.
You can't do ContextMenus on Hyperlinks. You can do it on a HyperlinkButton, though. I'm not exactly sure of the reason, but it does work. Depending on what you're trying to do, HyperlinkButton may have been what you wanted anyway (Hyperlink is usually only used inside documents of text).
I just found the answer while reading the Windows Phone 7.5 Unleashed book by Daniel Vaughan. My problem was that I needed to add a hyperlink in a RichTextBox, and I can only use Inlines to show text. Hyperlink is an Inline, but does not support ContextMenu. As Tim suggested, I needed to use HyperlinkButton. The solution is that there is a class called InlineUIContainer. So, this
var inline = new InlineUIContainer { Child = SomeHyperlinkButton }
made the trick for me.