Removing control from panel doesn't remove it from the Form? - c#

I have a routine where I loop recursively through all the controls on a form and process some code on some of them.
I add and remove controls through the use of the screen depending on selections the user makes.
I found that panel.Controls.Remove(control1) didn't actually remove it from the form. When I would run the routine that loops recursively through the controls on the form, the control I thought I had remove was still being found.
It didn't "disappear" until I did:
panel.Controls.Remove(control1);
this.Controls.Remove(control1)
Is this expected? Can someone explain this to me, and or point me to somewhere that explains control behavior in Windows Forms.
Thanks!

Clearly the control has the form as its Parent, not the panel. These kind of accidents tend to happen easily with the designer. You can use View + Other Windows + Document Layout to get a good view of the child-parent relationships. You can use drag+drop in this list to fix.

Related

Moving Controls from One Tab Page to Another

I currently am working on a WinForm project in which there are several different tabs. Within each tab there are various controls such as buttons, sub-tabs, text-boxes, ect...
I need to consolidate the overall application which involves taking certain controls from one tab and moving them to another. When I first tried doing so, I simply copy and pasted the controls. As you can imagine this didn't work due to the fact that I didn't move the properties with the controls, I really just created NEW ones on a different tab. Therefore when I complied the code, nothing worked because there was no code assigned to the new controls.
When I tried it again, this time I CUT and paste which also maintains the same properties as the old controls (specifically the reference name in the code), so as far as I can tell, the code should identify the controls by name, and apply the same actions. However, when I compile the code, the application successfully builds but the controls do not perform any actions.
At this point I am not sure what to do...
Use the Document Outline.
View... Other Windows... Document Outline.
Select the required component and drag it from one tab page to the other in the tree control. I did this and the actions are preserved in the new tab page.
Drag the item out of the tab control and onto the form itself. Change to the other tab. Then drag the item into that tab. It is essentially 2 drag moves, but since you do not ever cut, all code linking is maintained. If your tab control takes up the entire form, simply make it smaller while you do the preceding steps and then make it large again when you are done.
When you "cut" the controls, you sever the connections between the controls and their respective events. When you "paste" them again, they're not hooked up to the events anymore, so they don't appear to do anything.
The "event" methods should still be present in your code, but you'll have to manually go through and subscribe each event to each control again (via the Properties window).
Alternatively, revert those changes, then open the .Designer.cs file and look for something like this:
this.tabPage1.Controls.Add(this.dataGridView1);
Which (for example) places dataGridView1 inside tabPage1.
If you wanted to move the DataGridView to another TabPage, you could just change this.tabPage1 in the above code to this.tabPage2.
this.tabPage2.Controls.Add(this.dataGridView1);
Then you can flip back over to the designer view and move the control around to wherever you want it within the TabPage.
I just tested it. What is happening when you cut and paste your controls, you losing the wiring of the events. What you need to do after cut and paste is to go to control properties-events, find the event in question and on the right, select a method that you want to handle that event.
This will cut them from the first TabPage and paste them on the second, i think you can do this as often as you want. And with a small change you can make it a truly copy.
hope it helps
private void ControlsToTabPage(TabPage from, TabPage to)
{
Control[] ctrlArray = new Control[from.Controls.Count];
from.Controls.CopyTo(ctrlArray, 0);
to.Controls.AddRange(ctrlArray);
}

TreeView detail architecture question

I've got windows form app I'm developing, and my client wants a TreeView on the left with nodes that when clicked allow their users to work in detail screens on the right. The simplest approach was to create panels which are disabled until the appropriate node is clicked. However, this app is growing and way too much of it is living in the main form.
I'm wondering if it is possible to have one form per node that will open and expand into the detail area on the right, and then close when I am done with it. That way I don't have a single monolithic form, however I am not sure of how to go about that.
Anybody have any insight into how to do something like that?
Thanks.
You should try using UserControls.
Basically, each UserControl is a form (more or less) that you can add to your main form just like you would any other control.
I would inherit from Panel for each page, attach an instance of each Panel-derived object to the Tag property of each TreeView node, and display that (Dock=Fill) when a node is selected.
You can use split controls and load the forms right side but needs to arrange it correctly. As #codethis mentioned, usercontrol is best enough to handle these as their code is written separately. Just you need to pass the parameter (from node selection).
You may need to multiple user controls and place them in your form according to your screen changes.

c# winForms open forms inside mainform

I have programmed c# application i will post screenshot. In this main form is 3 buttons which opens different forms. Now i decided to modify this application I want to Make one main form with strip menu which will open this forms. I used this code but i don't like or i'm doing something wrong. I don't like because there is child controls(minimize, maximize, close) in parent (please see second picture ):
Please advice me something. Is MDI good for such job? Thanks!
Sell sell = new Sell();
sell.MdiParent = this;
sell.Dock = DockStyle.Fill;
sell.Show();`
So my problem is that parent form is not filling when i open child form this is creen how to make that it parent form was filled with child form
Seeing your latest edit, I assume the reason that your child form's content doesn't fill the screen even when it's maximized is because your content/layout is not flexible.
Wherever you've placed the controls during Design Mode is where they're going to end up at run time, regardless of how big or small you make the window. If the window is too small to contain all of them, they'll either be covered up or you will see scrollbars. Alternatively, if the window is made larger than necessary, you'll see a lot of empty space.
The way around this is either to set the Dock and Anchor properties of your controls, which causes them to expand and compress to fit the layout of their containing form. You could also place your controls inside a TableLayoutPanel or FlowLayoutPanel control to help manage their layout.
As far as the question you appeared to be asking originally, I still can't tell if you're opposed to the way an MDI application looks, or if you simply don't understand how to correctly implement it. The clarification comment you offered actually makes things less clear to me—you posted a code snippet, but didn't explain what it means. As I wrote in a comment, there's no (non-hackish) way to show a form that doesn't have minimize, maximize, and close buttons (setting the FormBorderStyle property to "None" does this, but I think this is a silly solution that simply allows you to use the wrong control for the job—it won't behave like a form, the user won't be able to move it around like a form, etc. so why use a form?).
If you truly want to have a single application window with changing content in the center, you should create a series of UserControls. You can lay out each user control with the necessary child controls, just like you would with a form (using the fluid layout techniques I discussed above), add each user control to your main form, set each control's Dock property to "Fill" (so that they fill the entire viewing area), and then write code to simply swap out the currently visible user control in your main form's viewing area. The advantage of using a UserControl versus something like a Panel is that you consolidate all of your code into a single control, much like you would with a Form. You could use a tab control, but if you don't want to show any indication that there are multiple forms (which is what your aim appears to be), this would also be the wrong control for the job.
If you literally want to open child forms inside your main form, as your question title indicates, you should indeed be using MDI. If you don't understand how to do this, you'll need to clarify your question further.
Set MDI Container property to true for your parent form. It will help.
Set
FormBorderStyle = None
for your child forms

WinForms TabOrder tool: Broken or just confusing?

I have a form with a bunch of panels, and some panels inside groupboxes. When using the TabOrder tool in Vs2005, the controls outside of containers are given integers (0), the controls inside panels are given decimals (72.0), and the controls within panels within groupboxes are given three-part values (73.73.0). Unfortunately the resulting tab order has nothing to do with the order I clicked my controls.
Does this tool simply not support nested containers? Am I doing something wrong? Perhaps holding Shift- or Ctrl- when I click (I've tried these with no success)?
Am I going to be forced to manually type in three-part tab orders for all my controls? That would be a bummer.
The tab order tool is not designed for you to enter values manually; it is designed for you to click on controls in the order that you'd like them to progress as the user tabs.
The numbers are not decimals; they represent the tab order of the control within its parent container. For example, if you have a Form with a Panel named panel1 and a Button inside of it named button1, then button1 would display a number like:
X.Y
X is the tab order of panel1
Y is the tab order of button1 within panel1.
I will acknowledge that the designer isn't as intuitive (or transparent) as it probably should be, but it does work.
I had the same problem with textboxes and buttons within group box in VS2010. TabOrder tool was just useless: Tab orders were broken no matter how I re-ordered the tab stops. In order to make the correct tab order I had to re-order of how controls are added to the group box in form designer initialization code:
this.groupBox2.Controls.Add(this.startTimeTextBox);
this.groupBox2.Controls.Add(this.endTimeTextBox);
this.groupBox2.Controls.Add(this.exitButton);
This way tab order would be startTimeTextBox -> endTimeTextBox -> exitButton and so on.
I think I figured out the way to do it in the designer: the trick is apparently that you have to click the panels/groupboxes as well in order to assign the different parts of the full ordering; in this way, it seems that a bredth-first clicking method needs to be used as opposed to clicking the child controls themselves.
Kinda sad, since it forces you to know the full structure of the whole form instead of just what the user sees.
I had this same problem and discovered this tool: http://archive.msdn.microsoft.com/cdstabindex
I had to change the manifest to make it work with VS2010 though. Also, I've modified the source code for myself to make the UI a little better, but even as it is, I would recommend having a look at this tool.
Remove Group-boxes from Controls and try again this works for me :)

C# Calendar Object on top

Hi programming some C# in a form with a bunch of group boxes. I have a two Calendar controls that when called upon, are partially invisible due to surrounding group boxes.
this.grp_TransactionDetails.Controls.Add(this.cal_Ctrl);
this.grp_TransactionDetails.Controls.Add(this.cal_Batch);
Any ideas? (I wish there was a z-index property)
Controls in .Net have a BringToFront() method that might be what you need. Calling this method on a control brings it to the front of the z-order (SendToBack() has the opposite effect, as you might expect).
To fix this, I had to actually change the order in which the controls were added to the form. I changed the order in the Initialze() method of the form.

Categories

Resources