I have a user control and in this control i have a bunch of text boxes and labels. Now I have linked this user control to another form's tab control. Here is the code I am using
TabPage tp = new TabPage();
tp.Controls.Add(TipUserControl);
tp.Text = "Tab "+ tabctrl_Fields.TabCount + 1;
tabctrl_Fields.TabPages.Add(tp);
When I click on a "Add another tab" button, the above code gets executed and a new tab page with the text boxes (similar to Tab 1) is created.
Now what I am looking for is When the user click on "Done" button in the form (not in the user control), it should be able to loop through every tab and every control (textboxes, labels etc) within that tab. Can anyone suggest on how to write this code?
Thanks in advance,
Swamy
I would add a Tag to the controls that you are searching for and use that approach:
Ability to find WinForm control via the Tag property
private void FindTag(Control.ControlCollection controls)
{
foreach (Control c in controls)
{
if (c.Tag != null)
//logic
if (c.HasChildren)
FindTag(c.Controls); //Recursively check all children controls as well; ie groupboxes or tabpages
}
}
Or iterate recursively over the Tab controls
Related
i have a main form which is containing a menu.
when i click a menu button there is an usercontrol is loading panel control in main form:
splitContainerControl1.Panel2.Controls.Add(new Moduller.userControlStokListesi() {
Dock = DockStyle.Fill
});
And there is a form on UserControl. when a user fill that form and hit the save button i want to remove that user control form from panel control.
How can i do that?
So if I understand your question correct, you want to remove the instance of Moduller.userControlStokListesi from the Panel2 ?
There are several ways to achieve this. You can remove all controls from a panel this way:
splitContainerControl1.Panel2.Controls.Clear();
You can also remove specific items:
splitContainerControl1.Panel2.Controls.RemoveByKey("the key of your control");
Or if you want the user control removes itself from the panel, you can call this snippet within the user control instance:
SplitContainerControl splitPanel = (SplitContainerControl) this.Parent;
splitPanel.Panel2.Controls.Remove(this);
I have a C# winform program that has many tabs, and I want to move controls between tabs. and code as below:
private void OnTabControlTabSelected(object sender , EventArgs e)
{
TabPage page = (sender as TabControl).SelectedTab;
foreach(Control control in mLastSelectedTab.Controls)
{
this.mLastSelectedTab.Controls.Remove(control);
page.Controls.Add(control);
}
}
I remove the control from the last selected tab and add it to the new selected tab, but the result is that some controls are added successfully, some are not, I set a breakpoint in this code, and found that the foreach loop doesn't loop through every control in the last selected tab. please help me to solve this problem, thank you!
I am using Ajax tab container control with 3 tabs.
I have placed a user control in each of the tabs.
All the 3 controls on getting loaded on page load. How do I refresh the Tabcontainer on click of each tab.
The reason I need this is, In the userControl that is in the 1st tab, I am hiding some controls based on a buttonclick.
If I click on the 2nd tab and 3rd tab if I click on the first tab, the hidden controls are not visible. I want to reload the tabcontainer when a user clicks on the tabs.
Please help.
You have the DynamicContextKey on tabPanel you can use to. On the ActiveChanged Event of your TabContainer, your will put visible true or false to the user control you want conditional to the DynamicContextKey.
void yourTabContainer_ActiveTabChanged(object sender, EventArgs e)
{
switch (yourTabContainer.ActiveTab.DynamicContextKey)
{
case "Key1":
userControl1.Visible = false;
userControl2.Visible = true;
break;
case "Key2":
userControl1.Visible = true;
userControl2.Visible = false;
break;
}
}
You will load all your control in your panel, and hide the control you want depend the DynamicContextKey selected.
Is it answer to your question ?
I have a custom user control on my windows forms. This control has a few labels on it.
I will be dynamically displaying an array of these controls on my form which will contain different bits of data.
What I am trying to do is know which user control was selected when I click on it.
This works when I click on an empty space on the user control, however, if I click on any label on the user control it will not recognize the user control click.
Any thoughts on how I can do a full user control click, even if a label on the control is being clicked?
If this question is not clear, or you need more info, please leave a comment.
I am doing this in c#.
Thanks!
User control's click event won't fire when another control is clicked on the user control. You need to manually bind each element's click event. You can do this with a simple loop on the user control's codebehind:
foreach (Control control in Controls)
{
// I am assuming MyUserControl_Click handles the click event of the user control.
control.Click += MyUserControl_Click;
}
After this piece of code workd, MyUserControl_Click will fire when any control on the user control is clicked.
foreach (Control c in this.Controls)
{
c.Click += new EventHandler(SameAsForm_Click);
}
Keep in mind that this won't add labels' clickevents in groupboxes, panels etc to the "SameAsForm_Click"-EventHandler.
My scenario is the following:
I am working on a winforms application in C# that has a button inside the main page of a tabcontrol that will generate another tabpage each time that it is clicked. Each new tabpage will contain a layout defined by a user control.
My Questions are:
How can I allow the user to then close one of the tabs that were created dynamically at runtime?
How might I go about modifying the tabcontrol itself so that it has a small 'X' in each tab that the user may click on in order to close that particular tab? (Like Firefox has)
How can I expose the SelectedIndex property of the tabcontrol to the user control if I want to close the tab with a button inside the user control instead?
I found this code and was very helpful to me:
private void tabControl_MouseUp(object sender, MouseEventArgs e)
{
// check if the right mouse button was pressed
if(e.Button == MouseButtons.Right)
{
// iterate through all the tab pages
for(int i = 0; i < tabControl1.TabCount; i++)
{
// get their rectangle area and check if it contains the mouse cursor
Rectangle r = tabControl1.GetTabRect(i);
if (r.Contains(e.Location))
{
// show the context menu here
System.Diagnostics.Debug.WriteLine("TabPressed: " + i);
}
}
}
}
TabControl: How To Capture Mouse Right-Click On Tab
I created a derived tab control about one year ago. I am not going to post the source here, because it's about 700 lines long and coded quite messy. Maybe I will find some time to clean the code up and then release it here. For now I will briefly outline the way it is build.
Each tab page has a 'X' icon to the left of the title and the tab pages support reordering by drag and drop and moving them between multiple tab control.
I choose the easy way to get the icon on the tab pages. The tab control has the TabControl.ImageList property and a tab page has a TabPage.ImageIndex property. So I just added three icons to a image list - normal, hover, pressed - and process the mouse events.
With TabControl.GetTabRect() you can test if the mouse is over a specific tab pages and with some math you find if it is over the icon. Then you just need to change the icon depending on the mouse button state and eventually remove the tab page under the mouse if the button was pressed.
The main problem with this solution is, that calculating if the mouse is over the icon requires to know where the icon is painted relative to the tab page and this might change with a new windows version. And the icon is to the left of the title, but that does not look too bad.
I did the following:
on the create (add) TabPage stage, I added a toolStrip
ToolStrip ts = new ToolStrip();
ts.Dock = DockStyle.Top;
ts.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
Then, create the X button and add it to toolstrip
ToolStripButton ToolStripButton = new ToolStripButton("X");
ts.Items.Add(ToolStripButton);
create an event on clicking the X button
ToolStripButton.Click += new EventHandler(ToolStripButton_Click);
add toolstrip to the tabpage
tabControl1.TabPages[curenttabpage].Controls.Add(ts);
now for the ToolStripButton_Click is as follows:
void ToolStripButton_Click(object sender, EventArgs e)
{
ToolStripButton t = (ToolStripButton)(sender);
ToolStrip ts = t.Owner;
TabPage tb = (TabPage)
(ts.Parent);tabControl1.TabPages.Remove(tb);
}
Maybe it is not as you want, but it will work well.
I created a setup that is similar.
Each control that is added to the tab page at runtime is derived from a special base control I created. This base control has a close button (along with some other features such as safe to close flag).
Close tab code I'm using on my base control:
TabPage tabpage = (TabPage)this.Parent;
TabControl tabControl = (TabControl)tabpage.Parent;
tabControl.TabPages.Remove(parent);
I know this is an old thread but I did find this link that will allow you to "hide" tabs in an array and then you can just re-load the tabs you want at run time. I added this more for a place I can easily find it again.
This code might help throgh closing the tab controls with middle mouse click :
private void tabControl1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != System.Windows.Forms.MouseButtons.Middle)
return;
for (int i = 0; i < MainTabControl.TabPages.Count; i++)
{
if (this.MainTabControl.GetTabRect(i).Contains(e.Location))
{
this.MainTabControl.TabPages.RemoveAt(i);
return;
}
}
}
It´s works!
TabPage tabpage = (TabPage)this.Parent;
TabControl tabControl = (TabControl)tabpage.Parent;
tabControl.TabPages.Remove(tabpage);