I am needing a way to loop through every form and every control on that form.
I am needing to find everything ..-be it forms, controls, nested forms, or nested controls within controls.
Regardless of type, and regardless of how deeply nested the form within a form control within a control might be.
I was hoping to add them all to a List<Control> or whatever array necessary so that I can access them accordingly.
I simply need access to everything everywhere, and the only way I've been able to access anything without directly
calling these controls explicitly by name is to instantiate first.
My code beneath doesn't call the need for everything of everything, but everytime I try to call stuff blind or by string name I get caught
up in the world of instantiation (and I wind up with controls I cannot instantiate because I cannot locate them blindly by string name).
..So in essence I just figured what the heck, why not get everything of every type regardless of deeply nested so that I will always be able to
instantiate new controls based on string names no matter what.
What I am doing is trying to change tabPages within tabControls (amongst manipulate other controls elsewhere by the control string name).
Some tabControls are nested within others, and some tabControls are nested in forms as well.
My plan is to add absolutely everything into an array when my app starts.
By doing this (I hope), I can then reloop through that array instantiating as I need to (for anything I need to).
Beneath is an example of what I mean, but of course it would loop through every form and every nested control or form.
My ultimate goal is to change tabPages with one function call
private void button1_Click(object sender, EventArgs e)
{
LoadInterface("tabControllerNameHere", "tabPageNameHere");
}
The reason I want to implement one function call to change a tabPage is so that I won't have to paste
a lot of code in every new button_Click I create.
With this, I simply paste LoadInterface("tabControllerNameHere", "tabPageNameHere"); into any button click and it changes my tabPage.
One last thing, the reason I request all controls and everything else is because I began creating other one call functions like this,
but I find myself stuck trying to instantiate nested things and controls I can't point to programmatically without explicitly calling them by name.
Of course with one function call like this, the whole point is to simply pass the string names in, and simply have it find the control by name, instantiate
that control, and do whatever I want to that control accordingly (regardless of type or how nested that is) through one function call.
Beneath is a working example, but it only works with tabPages (1-4) in tabControl1.
tabControl1 is not instantiated (and is called explicitly), just couldn't figure out how to instantiate that name also (so the first arg in my function call
is really not doing anything right now.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Reflection;
namespace Nested_Tab_Controls
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.tabControl1.Size = new Size(655, this.ClientRectangle.Height);
showPage1.Click += new EventHandler(ShowPageOne);
showPage2.Click += new EventHandler(ShowPageTwo);
showPage3.Click += new EventHandler(ShowPageThree);
showPage4.Click += new EventHandler(ShowPageFour);
}
//************************************************************************************************************
public void ShowPageOne(object sender, EventArgs e)
{
LoadInterface("tabControl1", "tabPage1");
}
public void ShowPageTwo(object sender, EventArgs e)
{
LoadInterface("tabControl1", "tabPage2");
}
public void ShowPageThree(object sender, EventArgs e)
{
LoadInterface("tabControl1", "tabPage3");
}
public void ShowPageFour(object sender, EventArgs e)
{
LoadInterface("tabControl1", "tabPage4");
}
//************************************************************************************************************
public void LoadInterface(string interfaceControlTabHolder, string loadTabInterface)
{
TabPage newInstantiatedTabPage = new TabPage();
List<Control> newTabPageCtrlList = new List<Control>();
foreach (TabPage newTabCtrl in tabControl1.Controls)
{
if (newTabCtrl.Name == loadTabInterface)
{
newInstantiatedTabPage = (TabPage)Activator.CreateInstance(newTabCtrl.GetType());
// Assign newInstantiatedTabPage Data From newTabCtrl
newInstantiatedTabPage.Name = newTabCtrl.Name;
newInstantiatedTabPage.BackColor = newTabCtrl.BackColor;
newInstantiatedTabPage.Size = new Size(newTabCtrl.Width, newTabCtrl.Height);
// Collect Filled Contents From newTabCtrl
foreach (Control inheritTabPageCtrl in newTabCtrl.Controls)
{
newTabPageCtrlList.Add(inheritTabPageCtrl);
}
// Assign Those Filled Contents Into newInstantiatedTabPage
for (int newTabPageCounter = 0; newTabPageCounter < newTabPageCtrlList.Count; newTabPageCounter++)
{
newInstantiatedTabPage.Controls.Add(newTabPageCtrlList[newTabPageCounter]);
}
// Get newTabCtrl Index Position
int tabIndexPos = tabControl1.TabPages.IndexOf(newTabCtrl);
// Remove newInstantiatedTabPage
tabControl1.TabPages.Remove(newTabCtrl);
// Add newInstantiatedTabPage Back
tabControl1.TabPages.Insert(tabIndexPos, newInstantiatedTabPage);
// Set Focus On newInstantiatedTabPage
tabControl1.SelectedIndex = tabIndexPos;
}
}
}
//************************************************************************************************************
}
}

New Topic/Question
Reply




MultiQuote







|