I'm having a difficult time trying to work with tooltips.
I'm trying to add a tooltip with different text to each dynamically created tab in a tabcontrol.
It's important to note that this tab is created from a form that contains a docked form where the tabcontrol is in.
This is, a main Form with a docking area, in where I have docked a results form, which contains an - initially empty - tabcontrol.
When you start the application this results form doesnt exists, I also create it dynamically whenever the user press certains parts of the main form, each one created as a new tab in the results form tabcontrol.
This is how I generate the tabs:
generateResultForm();
TabPage newtp = new TabPage("Nuevo paciente")
_result.TabControl.TabPages.Add(newtp);
newtp.Name = setTabName("np");
Now, I've tried putting a tooltip in the results form, then tried to first generate the tooltip by adding below something like _result.ResultsTooltip.SetToolTip(newtp, "Creación de un nuevo paciente.");, which didn't work. Then, since once the tab is created, it becomes selected, I tried to add it in the results class by something like WorkareaTooltip.SetToolTip(tabControl.SelectedTab, "Cosas"); in the selectedindexchange event from the tabcontrol.
I don't think it would have been a great solution, but I don't know what else to try.
Of course the tabcontrol has its ShowToolTips property set to true.
If anyone could help me that'll be great.
Thanks for reading, and sorry if there are any language mistakes :)
//EDITED
This is the code i'm actually using (and doesn't work)
TabPage newtp = new TabPage("Nuevo paciente");
_workareaform.TabControl.TabPages.Add(newtp);
newtp.Name = "np";
var tooltip = new ToolTip();
tooltip.SetToolTip(newtp, "Creación de un nuevo paciente.");
Now, it doesn't work, might be because of the whole configuration.
Just to be clear, this tab is in a TabControl which is in a Form docked into a dockContainer in another Form.
Here is an image if it.
http://i.imgur.com/fVz6e06.png
As you can see, no tooltip at all.
Have you tried setting ToolTipText property as shown below?. It worked for me.
_result.TabControl.ShowToolTips = true;
TabPage newtp = new TabPage("Nuevo paciente");
_result.TabControl.TabPages.Add(newtp);
newtp.ToolTipText = "this is tooltip";
If you're working with another form you need to reference the other form TabControl
In this sample I create a Form1 instance (with TabControl in it) from my Form2 and then add page and tooltip.
private void Form2_Load(object sender, EventArgs e)
{
//instantiate another form
var f1 = new Form1();
//find tabcontrol on new form
var tc = (TabControl) f1.Controls["tabControl1"];
//create page
TabPage newtp = new TabPage("Nuevo paciente");
newtp.Name = "Paciente1";
tc.TabPages.Add(newtp);
//add tooltip
var tt1 = new ToolTip();
tt1.SetToolTip(newtp, "paciente 1 tooltip");
//show other form
f1.Show();
}
Related
I have a form in which I would like to be able to add tabs dynamically through the use of a button (much like the buttons most modern browsers have for adding tabs). These tabs should also contain a text box which is stretched to the individual tab's width and height upon creation.
I apologise for the lack of code but besides instantiating a TabControl container within a Form class, I have no clue as to what I should do next.
Thanks in advance.
All you need is to call the Add method on the TabControl.TabPages collection, then add other controls to that TabPage, like so:
private void button1_Click(object sender, EventArgs e)
{
TabPage tp = new TabPage("Test");
tabControl1.TabPages.Add(tp);
TextBox tb = new TextBox();
tb.Dock = DockStyle.Fill;
tb.Multiline = true;
tp.Controls.Add(tb);
}
Hope this helps
I have a windows form and i dont want to make any other windows forms just one windows form and different user controls how can i change between user controls for example hide one and show the other user control programmatically ?
private void Btt_info_Click(object sender, EventArgs e)
{
Frm_Main frm_main = new Frm_Main();
frm_main.Controls["panel1"].Controls.Clear();
UC_Info uc_info = new UC_Info();
frm_main.Controls["panel1"].Controls.Add(uc_info);
}
i added this but it doesnt work
Add a container control (if I remember correctly, there's a containers section in the toolbox?), like a panel. Create usercontrols for what you want to dynamically switch around. So make like a 'HomePage' usercontrol and a 'LoginPage' usercontrol. Dynamically add the usercontrol that you want to display to the container. WHen you want, remove it from the container and add a different usercontrol:
Panel myPanel = new Panel();
LoginPage ctlLoginPage = new LoginPage();
HomePage ctlHomePage = new HomePage();
//add the loginpage to the panel first
myPanel.Controls.Add(ctlLoginPage);
...do stuff...
//remove whatever control is currently in the Panel
myPanel.Controls.Clear();
//add the other control, the HomePage control instead now
myPanel.Controls.Add(ctlHomePage);
..do other stuff...
I usually do it this way so you leave your form itself open to add common controls and stuff that might be shared between your different 'pages'.
EDIT: Note that I normally would add the panel in the designer and not create it dynamically in the code. This was just an example.
EDIT: The interaction between your mainform and usercontrols can be handled in a few different ways, and I am not saying that any of these is the correct method.
You create a static property for your Panel on the Mainform, so that
you can always access it to swap your controls around.
In this example I'll also add a static method for it
enum PanelControlsEnum {HomePage, LoginPage};
public static Panel MyContainerPanel {get;set;}
public static void SwitchPanelControls(PanelControlsEnum selControl){
..put your switch panels code here..
}
Then inside your usercontrol you call a predefined method, something like:
MainForm.SwitchPanelControls(PanelControlsEnum.HomePage);
Another method is to bind the button click event on your mainform
instead of inside the form.
Like This:
HomePage ctlHomePage = new HomePage();
ctlHomePage.Click += MyClickEvent;
myPanel.Controls.Add(ctlHomePage)
...
private void MyClickEvent(object sender, RoutedEventArgs e)
{
..switch user control code here...
}
Create a method that returns a UserControl object. Then put conditions in that method as to which control you want to load at a specific condition and then in your main form code.
UserControl control = GetControlFromMyMethod();
form1.Controls.Add(control);
where 'control' is the returned control from your method.
To remove the existing one you have to loop through the form1.Controls and find out the control and call 'Remove'.
Update:
Mike C has a better idea of adding a panel and loading your desired control on the panel as then it's easy to remove your control and you then don't have to loop through the forms controls to find it and then remove it.
Try this:
this.Controls.Clear();
usercontrol load = new usercontrol ();
this.Controls.Add(load);
load.Show();
you could try this it will definitely help you as it did helped me a lot it short and straight to the point hope that will help
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 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!
I have a tab control in a windows form and I want to be able to click on a tab and in the body area of the tab I want it to display another form as an embedded component. Is this possible? If so, can someone please provide an example or a link to an example of how to accomplish this?
You are probably looking for Tabbed MDI Child Forms
You can embed a Form but it's not the best choice.
Better place the contents on UserControls and add that to the TabPage.
Set your MainForm (Parent) as IsMDIContainer = true;
Create an instance of the ChildForm and call this function:
FormChild frmChild = new FormChild();
AddNewTab(frmChild);
Copy this Function to your code:
private void AddNewTab(Form frm)
{
TabPage tab = new TabPage(frm.Text);
frm.TopLevel = false;
frm.Parent = tab;
frm.Visible = true;
tabControl.TabPages.Add(tab);
frm.Location = new Point((tab.Width - frm.Width) / 2, (tab.Height - frm.Height) / 2);
tabControl.SelectedTab = tab;
}
I think the other answer has the right idea; Tabbed MDI is probably what you want.
There is an approach where you create a UserControl that has the same content as the form and use that on the TabPage.
TabPage myTabPage = new TabPage(sometext);
myUserControl = new myUserControlType();
myUserControl.Dock = DockStyle.Fill;
myTabPage.Controls.Add(myUserControl);
myTabControl.Add(myTabPage);
http://bytes.com/topic/c-sharp/answers/270457-can-i-add-form-tabpage goes into more detail; but I'd look at the MDI stuff first.
If you do not want to use MDI, you can try to put everything from desired form to user control and add this user control in both form and tab.