The Set Up:
I have a System.Windows.Forms class called ProjectForm. In this form I have a TabControl called tabControl. When the form is initialized, so is the tabControl; however, the tabControl has no TabPages loaded. TabPages are created and loaded at runtime on demand when a user selects an item in a treeView control.
Example Call From ProjectForm:
this.tabControl.TabPages.Add(PageLibrary.CallStackPage(e.Node.Name, e.Node.Text));
(TabPageLibrary) as PageLibrary Class reference
class TabPageLibrary
{
private TabPageToolBar tabToolBar = new TabPageToolBar();
public TabPage CallStackPage(string name, string label)
{
TabPage tabPage = NewProjectPage();
tabPage.Name = "STACK:" + name;
tabPage.Text = label;
tabPage.Tag = name;
tabPage.ImageKey = "viewstack.png";
return tabPage;
}
private TabPage NewProjectPage()
{
TabPage tabPage = new TabPage();
tabPage.Padding = new Padding(3);
tabPage.UseVisualStyleBackColor = true;
tabPage.Controls.Add(this.tabToolBar);
return tabPage;
}
}
Problem
When the TabPage is loaded into the control at runtime - no image shows on the tab. the TabControl.ImageList is set to an ImageList that does contain the image I am referencing. Subsequently, the tree control is referencing the same ImageList and the images do show in the tree control.
I would be grateful for any suggestions, solutions or blinding flashes of the obvious you could share.
--Peace
+++ FIX UPDATE ++++
With DonBoitnott's insight - I was able to get these images to properly render with minor refactoring.
New Example Call From ProjectForm:
TabPage page = PageLibrary.NewProjectPage();
this.tabControl.TabPages.Add(page);
page = PageLibrary.CallStackPage(e.Node.Name, e.Node.Text, page);
Refactored (TabPageLibrary) as PageLibrary Class reference
class TabPageLibrary
{
private TabPageToolBar tabToolBar = new TabPageToolBar();
internal TabPage CallStackPage(string name, string label, TabPage page)
{
page.Name = "STACK:" + name;
page.Text = label;
page.Tag = name;
page.ImageKey = "viewstack.png";
//TODO: Load Additional CallStack Controls
return page;
}
internal TabPage NewProjectPage()
{
TabPage tabPage = new TabPage();
tabPage.Padding = new Padding(3);
tabPage.UseVisualStyleBackColor = true;
tabPage.Controls.Add(this.tabToolBar);
return tabPage;
}
}
Thanks again #DonBoitnott, works like a champ!
The code for TabPage tells us that the page will only pick up the image list if it has a parent from which to pull it. Odd, but you can prove it. Here it is:
//From TabPage.cs
public string ImageKey
{
get
{
return this.ImageIndexer.Key;
}
set
{
this.ImageIndexer.Key = value;
TabControl parentInternal = this.ParentInternal as TabControl;
if (parentInternal != null)
{
this.ImageIndexer.ImageList = parentInternal.ImageList;
}
this.UpdateParent();
}
}
That means you have to ensure you parent the tab page before you attempt to assign the image list key.
So, with that in mind, here's a bare bones example you can put in any form (you'll need to supply your own art, of course):
public partial class Form1 : Form
{
private ImageList _imgList;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(Object sender, EventArgs e)
{
foreach (TabPage p in tabControl1.TabPages)
p.Dispose();
_imgList = new ImageList();
_imgList.Images.Add("image0", Properties.Resources.ImageOne);
_imgList.Images.Add("image1", Properties.Resources.ImageTwo);
_imgList.Images.Add("image2", Properties.Resources.ImageThree);
tabControl1.ImageList = _imgList;
}
private void button1_Click(Object sender, EventArgs e)
{
Int32 count = tabControl1.TabPages.Count;
if (count < 3)
{
TabPage p = new TabPage();
p.Name = "page" + count;
p.Text = "page" + count;
tabControl1.TabPages.Add(p);
p.Parent = tabControl1;
p.ImageKey = "image" + count;
}
}
}
Related
I would iterative a TabPage and a WebBrowser controls.
I want to add a new webBrowser in a new Tab after each click. But I've got this error message :
Impossible to fix the index [] to this control-object
the code is the following :
TabPage var_i = new TabPage();
webBrowser webBrowser = new webBrowser();
int i = 0;
private void toolStripButton4_Click(object sender, EventArgs e)
{
webBrowser[i].Dock = DockStyle.Fill;
tabControl1.TabPages.Add(var_i[i]);
var_i[i].Controls.Add(webBrowser[i]);
i++;
if (textBox1_url.Text != "")
{
webBrowser[i].Navigate(textBox1_url.Text);
var_i[i].Text = textBox1_url.Text;
}
}
thanks you for your contribution.
M.A.
I found the solution. I used the function "AddRange" to the control TabControl and add a new value of tab collection.
tabcontrol1.Controls.AddRange(new Control [] { this.tabPage1 };
running this in linux, all fine.
running in Windows making trouble, all tabs are twice present and i can't insert content into tabs.
https://abload.de/img/unbenannt82jfh.png
someone knows about?
public class CalendarApp : Form {
public CalendarApp() : base() {
tabControl = new TabControl();
tabPages = new TabPage[3];
string []tabText = {"Haupt", "Kontakte", "Termine"};
for (int i=0; i<3; i++) {
Console.WriteLine ("Ahhh windoof: {0}", i);
tabPages[i]=new TabPage();
tabPages[i].Text = tabText[i];
tabPages[i].TabIndex = i;
tabPages[i].Parent = tabControl;
tabControl.Controls.Add(tabPages[i]);
}
FlowLayoutPanel mainPanel = new FlowLayoutPanel();
tabControl.Size = new Size(mainPanel.Size.Width+8, mainPanel.Size.Height+24);
this.Text = "CalendarApp";
Controls.Add (tabControl);
AutoSize=true;
this.FormBorderStyle = FormBorderStyle.FixedSingle;
ResumeLayout(false);
PerformLayout();
}
[STAThread]
public static void Main() {
Application.Run(new CalendarApp());
}
private TabControl tabControl;
private TabPage []tabPages;
}
ok, i've yust found the Problem:
// tabPages[i].Parent = tabControl; <-this Line is causing the Problem
tabControl.Controls.Add(tabPages[i]);
I'm a newbie and I decided to enhance my program's GUI using Krypton Toolkit.
I was able to load it into my project.
The issue that I have created a control with name mycontrol which have a KryptonDataGridView with name mydatagrid.
In my form, I have the following code:
public partial class Form1 : KryptonForm
{
private KryptonPage[] mycontrolpage;
private Control mycontrolcontent;
public Form1()
{
InitializeComponent();
}
private KryptonPage NewmycontrolPage()
{
mycontrolcontent = new mycontrol();
KryptonPage page = new KryptonPage("mypage", null, "OS mypage");
// Add the control for display inside the page
mycontrolcontent.Dock = DockStyle.Fill;
page.Controls.Add(mycontrolcontent);
// Document pages cannot be docked or auto hidden
page.ClearFlags(KryptonPageFlags.DockingAllowAutoHidden | KryptonPageFlags.DockingAllowDocked);
return page;
}
private void Form1_Load(object sender, EventArgs e)
{
// Setup docking functionality
KryptonDockingWorkspace w = kryptonDockingManager.ManageWorkspace(kryptonDockableWorkspace);
kryptonDockingManager.ManageControl(kryptonPanel, w);
kryptonDockingManager.ManageFloating(this);
mycontrolpage = new KryptonPage[] { NewmycontrolPage() };
kryptonDockingManager.AddToWorkspace("Workspace", mycontrolpage);
}
}
My problem is how to access the grid in mycontrol?
Without some code, You could always view the Examples..
Or
Some code snippets:
internal ComponentFactory.Krypton.Toolkit.KryptonDataGridView dgvRules;
Then...
int offset = dgvRules.Rows.Add((bool)condition, name, description, applied);
KryptonDataGridViewCheckBoxCell dataGridViewCell = dgvRules.Rows[offset].Cells[0] as KryptonDataGridViewCheckBoxCell;
if (condition == Tribool.Unknown
|| accumulationRules[offset] == Tribool.Unknown)
{
dataGridViewCell.ReadOnly = condition == Tribool.Unknown;
dataGridViewCell.ThreeState = true;
dataGridViewCell.Value = CheckState.Indeterminate;
}
else
{
dataGridViewCell.Value = (CheckState)accumulationRules[offset];
}
I've a panel.
I add WinForms inside it. The WinForms added have the TopLevel and Visible properties set to FALSE and TRUE.
I can do a panel.SetChildIndex(WinForm1,0) to bring WinForm1 to front.
What I've not managed to do is keep a track of the actual ChildIndex of the panel.
The idea is to have buttons that opens forms inside the panel, and that when the panel opens a new button is added in a Windows menu.
Something like when many files are open on a VS Project, you can go to Window menu and select one. Also, if you change the active page by clicking the page, the Window menu auto-updates and checks the actual active page.
I want to do this, but with a panel container. I've managed to get done everithing, but not the the Window menu auto-updates and checks the actual active page part.
Isn't there an event fired when BringToFront() or SetChildIndex(form, index) are called? Any event when I click another form that's inside the panel and it becomes the "active one"? Or some property of the panel that I can keep track of that changes when active form changes?
It is taken from here
When Control's ZOrder is chaged layout operation is always performed
in control's container control.
When I subscribed to container's Layout event and called
BringToFront() it showed me Control that changed its
ZOrder(LayoutEventArgs.AffectedControl) and changed property
(LayoutEventArgs.AffectedProperty).
Found that when a form inside a panel is closed, the Controls property of the panel gets reindexed, where the index zero is the form that gets the new focus. Now that I've a way to check the form that's in front when I close another one, windows administration in panels is done.
Going to put the source code, maybe it can help someone :)
Please note that I'm using a RadRibbonForm, a standard panel, and RadForms inside the panel. Rad's are from Telerik. Some things should change to make this work on standardWinForms, but the changes are minimal.
Also, I'm not using a menu that shows the forms, I'm using RadButtonElement's in a page of the ribbon menu instead.
AddRadFormWindow must be called to put a window and manage it automatically.
Example of adding a window:
AddRadFormWindow(typeof (MyRadForm))
Now, the source. It must be inside the code of the RadRibbonForm's class.
public static class ExtensionsRadForm
{
[DllImport("user32.dll")]
private static extern int ShowWindow(IntPtr hWnd, uint msg);
public static void Deminimize(this RadForm form)
{
if (form.WindowState == FormWindowState.Minimized)
ShowWindow(form.Handle, 9);
}
}
private void RefreshButtonsChecks(string windowName)
{
if (windowName != null)
{
principalPanel.Controls[windowName].BringToFront();
}
if (principalPanel.Controls.Count > 0)
{
if (principalPanel.Controls.Cast<RadForm>().Any(radForm => radForm.WindowState != FormWindowState.Minimized))
{
foreach (RadItem item in radRibbonBarGroupOpenWindows.Items)
{
var buttonBorder = ((RadButtonElement) item).BorderElement;
if (item.Name == panelPrincipal.Controls[0].Name + "Button")
{
buttonBorder.ForeColor = Color.LimeGreen;
buttonBorder.BottomColor = Color.LimeGreen;
buttonBorder.TopColor = Color.LimeGreen;
buttonBorder.LeftColor = Color.LimeGreen;
buttonBorder.RightColor = Color.LimeGreen;
principalPanel.Controls[0].Focus();
}
else
{
buttonBorder.ForeColor = Color.Transparent;
buttonBorder.BottomColor = Color.Transparent;
buttonBorder.TopColor = Color.Transparent;
buttonBorder.LeftColor = Color.Transparent;
buttonBorder.RightColor = Color.Transparent;
}
}
}
else
{
foreach (RadItem item in radRibbonBarGroupAbiertas.Items)
{
var buttonBorder = ((RadButtonElement)item).BorderElement;
buttonBorder.ForeColor = Color.Transparent;
buttonBorder.BottomColor = Color.Transparent;
buttonBorder.TopColor = Color.Transparent;
buttonBorder.LeftColor = Color.Transparent;
buttonBorder.RightColor = Color.Transparent;
}
}
}
}
private void PrincipalPanelLayout(object sender, LayoutEventArgs e)
{
RefreshButtonsChecks(null);
}
private void RadButtonElementCloseAllWindowsClick(object sender, EventArgs e)
{
int limitButtons = radRibbonBarGroupOpenWindows.Items.Count;
for (int index = 0; index < limitButtons; index++)
{
RadItem radItem = radRibbonBarGroupOpenWindows.Items[0];
radItem.Dispose();
}
int limitControls = principalPanel.Controls.Count;
for (int index = 0; index < limitControls; index++)
{
Control control = principalPanel.Controls[0];
control.Dispose();
}
Update();
GC.Collect();
}
private void AddRadFormWindow(Type windowToAdd)
{
if (!principalPanel.Controls.ContainsKey(windowToAdd.Name))
{
var window = (RadForm) Activator.CreateInstance(windowToAdd);
window.TopLevel = false;
window.Visible = true;
window.FormClosing += (method, args) =>
{
radRibbonBarGroupOpenWindows.Items[window.Name + "Button"].Dispose();
GC.Collect();
};
window.Enter += (method, args) => RefreshButtonsChecks(window.Name);
var closeMenuItem = new RadMenuItem("Close");
closeMenuItem.MouseDown += (method, args) =>
{
panelPrincipal.Controls[window.Name].Dispose();
radRibbonBarGroupOpenWindows.Items[window.Name + "Button"].Dispose();
};
var contextMenu = new RadContextMenu();
contextMenu.Items.Add(closeMenuItem);
var button = new RadButtonElement(window.Text) {Name = window.Name + "Button"};
button.MouseDown += (method, args) =>
{
switch (args.Button)
{
case MouseButtons.Left:
if (((RadForm) principalPanel.Controls[window.Name]).WindowState ==
FormWindowState.Minimized)
((RadForm) principalPanel.Controls[window.Name]).Deminimize();
principalPanel.Controls[window.Name].BringToFront();
principalPanel.Controls[window.Name].Focus();
break;
case MouseButtons.Right:
contextMenu.Show(MousePosition);
break;
}
};
radRibbonBarGroupOpenWindows.Items.Add(button);
principalPanel.Controls.Add(window);
principalPanel.Controls[window.Name].BringToFront();
principalPanel.Controls[window.Name].Focus();
}
principalPanel.Controls[windowToAdd.Name].BringToFront();
principalPanel.Controls[windowToAdd.Name].Focus();
Update();
GC.Collect();
}
public Constructor()
{
panelPrincipal.Layout += PanelPrincipalLayout;
}
I create a label "label1" dynamically in a method. Then when I click a button I want to remove that label created but if I write Controls.Remove(label1) it says that the control doesn't exist in the context.
How could I do to achieve this?
EDIT: Following Jon suggestion I implemented the foreach loop but it doesn't do anything. This is my code, the panel which I use is created by design:
void GenerateControls() {
Label labelOne = new Label();
Button btnContinue = new Button();
panel.SuspendLayout();
SuspendLayout();
//btnContinue
btnContinue.BackColor = System.Drawing.Color.Black;
btnContinue.ForeColor = System.Drawing.SystemColors.Menu;
btnContinue.Location = new System.Drawing.Point(145, 272);
btnContinue.Name = "btnContinue";
btnContinue.Size = new System.Drawing.Size(95, 28);
btnContinue.TabIndex = 13;
btnContinue.Text = "Continue";
btnContinue.Visible = true;
Controls.Add(btnContinue);
btnContinue.Click += new System.EventHandler(btnContinue_Click);
//labelOne
labelOne.Location = new Point(0,65);
labelOne.Size = new System.Drawing.Size(100,20);
labelOne.Text = "labelOne";
labelOne.Name = "labelOne";
labelOne.Visible = true;
labelOne.TextChanged += new System.EventHandler(this.lbl_TextChanged);
labelOne.BackColor = System.Drawing.Color.PaleGreen;
Controls.Add(labelOne);
//panel
panel.Controls.Add(labelOne);
panel.Visible = true;
panel.Location = new Point(0,0);
panel.Size = new Size(240, 320);
//
Controls.Add(panel);
panel.ResumeLayout();
ResumeLayout();
}
And then in when I click on btnContinue:
private void btnContinuar_Click(object sender, EventArgs e) {
foreach (Control control in panel.Controls) {
if (control.Name == "labelOne"){
panel.Controls.Remove(control);
break;
}
}
}
I debug it and in the panel.Control it continues as if it were empty panel.
Thanks for your help!
I suspect it says the variable doesn't exist in that context. You'll have to find the label by its text, or knowing something else about it. For example, when you create it you could set the Name property and find it by that when you want to remove it:
panel.Controls.RemoveByKey("YourLabelName");
EDIT: As noted in the comments, RemoveByKey doesn't exist in the compact framework. So you'd either have to remember the reference yourself (in which case you don't need the name) or use something like:
foreach (Control control in panel.Controls)
{
if (control.Name == "YourLabelName")
{
panel.Controls.Remove(control);
break;
}
}
EDIT2: And to make it even more "generic" and desktop compatible, you could keep the RemoveByKey call and add this to your app:
public static class FormExtensions
{
public static void RemoveByKey(this Control.ControlCollection collection,
string key)
{
if(!RemoveChildByName(collection, key))
{
throw new ArgumentException("Key not found");
}
}
private static bool RemoveChildByName(
this Control.ControlCollection collection,
string name)
{
foreach (Control child in collection)
{
if (child.Name == name)
{
collection.Remove(child);
return true;
}
// Nothing found at this level: recurse down to children.
if (RemoveChildByName(child.Controls, name))
{
return true;
}
}
return false;
}
}
After 20 edits to the OP question, and Jon's answer with no resemblance to the original problem, you are left with one small glitch.
Your Not adding labelOne to the panel you are adding it to the Form.
Change
Controls.Add(labelOne);
to
panel.Controls.Add(labelOne);
Then everything should work