How to make Panel Visible in WinForms? - c#

I have the following Code:
marathonPanel.Visible = false;
resultPanel.Visible = true;
but only the marathonPanel gets invisible and the resultPanel stays invisible.
When I check the value of resultPanel.Visible it is set to false.
I also tried
resultPanel.BringToFront();<br>
resultPanel.Visible = true;
Can anyone help me?

This happens when you design two overlapping panels in Visual Studio Form Designer. It is too easy to drag one panel inside the other and the dragged one becomes the child of the first.
I usually draw the panels in different locations. The first one in the expected place, the second one in a different place, then at Runtime move the second one on the same spot of the first one.
in Form_Load
resultPanel.Left = marathonPanel.Left;
resultPanel.Top = marathonPanel.Top;

This is a common designer accident, caused by Panel being a container control. Overlapping two panels is a problem. Your resultPanel will end up as a child of marathonPanel. So when you make marathonPanel invisible, the child will always be invisible as well.
Use View + (Other Windows) + Document Outline to fix the problem. Drag resultPanel and drop it on the form. Edit the Location property by hand, don't move the control with the mouse or the panel will suck it right back in.
Another way to do it is to intentionally mis-place it so it won't be sucked-up and fix the Location property in the form constructor. A more friendly hack that works better in the designer is to use a TabControl instead. Check the sample StackPanel in this answer.

There is another way to find out such issues .
if you look at *.resx file , it will tell which control is taking place as parent and which is child
Also you can view this thing in Document outline which is available in Visual Studio.

Related

Split Windows with panels

I'm reading a Windows Forms book and I came to one example which is pretty confusing to me.
Here are two pictures, the first is the initial state, and the second is when the user click the Hide button.
This form contains two split containers, one horizontal, and one vertical, which is in the right panel of horizontal one.
The book says:
One of the best characteristics of docked designs is that they easily accommodate hidden
or modified controls. To implement
this design, two panels are placed in the left region of the
SplitContainer, one named pnlFileList and the other named pnlShow.
However, only one of these panels is shown at a time. The contents of
the rest of the window automatically resize themselves to accommodate
the additional view when it is displayed
private void cmdHide_Click(object sender, System.EventArgs e)
{
splitContainer1.Panel1Collapsed = true;
pnlShow.Visible = true;
}
private void cmdShow_Click(object sender, System.EventArgs e)
{
pnlShow.Visible = false;
splitContainer1.Panel1Collapsed = false;
}
And I made it, but the problem is with the button which appears when the left panel of the SplitContainer is collapsed.
I don't know where to put the panel "pnlShow"
If I put it on the right side of the horizontal SplitContainer control, it will disappear also.
Any suggestions?
The quote from the book doesn't seem accurate to me. You can't place pnlShow inside the "left region" of the SplitContainer because it will not be visible once you set the Panel1Collapsed property to true.
Instead, you can place the pnlShow on the left to the SplitContainer and set its Dock property to Left. Also, you don't seem to actually need a Panel in this case since it only contains one Button. You can simply use a Button only. Anyhow, your form would look something like this in design-time:
Then, your code should work fine.
Some remarks:
Of course, if you decided to use a button without a panel, you'd need to use YourButtonName.Visible instead of pnlShow.Visible.
If you found that the button (panel) on the left covers the SplitContainer, you just need to right-click on the SplitContainer and select "Bring to front".
It's preferred to use "btn" as a prefix for the Button name instead of "cmd". The latter was kind of widely used in the classic visual basic language, because it used to be called CommandButton. In the .NET world, the standard is to use "btn" instead.
Finally, based on what I mentioned in the last point above and the look of the screenshots in your question, this book seems to be quite outdated and was written based on the early versions of the .NET framework, so unless you have to study this book specifically, I would recommend you find a more recent book or tutorials.

Recommended way to manipulate User Control panels into a Windows From

I just started working with Visual Studio C# and to be honest I didn't fully understand what happens when we chose to hide a form or a user control.
My intuition tells me this hide/show method is kind of "inefficient" way to get an user through all the functions of my app.
So I am asking you guys if there is another workaround to "load" user control parts in a form.
Right now my main_menu form has all the user control objects placed on the form, but hidden, and I am using buttons to show them.
Is there a better way to achieve the same result? (I was thinking of a workaround like having an empty panel where I can load the User Control - not sure if possible)
Thank you!
You can create the controls on the fly and add them to or remove them from the Controls collection. On the class level, define this field
private Control _currentPanel;
You can use a more specific type here, if you are deriving all your panels from a common base type.
Then change the panel with
// Remove previous one.
if (_currentPanel != null) {
Controls.Remove(_currentPanel);
}
// Add new one
_currentPanel = new MyNewPanel();
//TODO: possibly set the panels Docking property to Fill here.
Controls.Add(_currentPanel);
In the example I am working with the form's Controls collection; however, you might have to use the Controls collection of some container control holding the panel.

how to create drop down panel with two steps

I would like to create a drop down panel that when you collapse it, it will close only half and when you expand, it will expand and show the other items as shown in below image.
I already tried to use SplitConatainer and by hide the panel manage my work but my problem is when I collapse it the other items not rearrange. please refer to below image.
button code:
if (splitContainer1.Panel2Collapsed)
splitContainer1.Panel2Collapsed = false;
else
splitContainer1.Panel2Collapsed = true;
Also I tried to set size of panel but it is not rendering and effecting.
would you mind help me in introducing some previous work that have ability of customization. Or help me to start create such component please?
I solve my problem by add a parameter to project "ExpandCollapsePanel" in GitHub.
So Just by adding a value to MaximumCollapseHeight, you can control panel.
you can get the source code from here.
enter link description here

User Control that acts like a standard Window, only confined to a pane

I'm making a program to generate code for me, and I'm fashioning the UI after Game Maker due to how easy the interface is. It has a SplitContainer with Panel1 containing a TreeView and Panel2 containing an arbitrary amount of self-contained windows (real windows, not some hacky workaround). I wanted to use user-controls to store the controls I use to modify things, but I can't figure out any way to put it in a window inside the splitContainer's Panel2. Can anyone help me?
Here's a good example:
http://i.stack.imgur.com/CG6kO.png
Those two sprite property windows are what I'm trying to do.
i think what you are looking for is called mdi-container
however the only real mdi container i've seen so far (in .NET) is a form ... sadly no panel or something similar...
but if you just want the "window in a window" effect: simply create your new form, set the TopLevel property of that instance to false, and add the instance to your form/panel/splitcontainer/whatever like any other usual control
You could try using an MDI form and to implement your TreeView control, check out some sort of docking panel. I've used this one in the past (http://sourceforge.net/projects/dockpanelsuite/).
It is very flexible. You set up one of these dockpanel forms, docked to the left of your MDI form. It will always be "on top" and the user can resize it exactly like the splitter control on a form. If you like, it can also has an "autohide" feature which may or may not be desirable in your case.
It can then contain you treeview, which can load all the MDI Child forms you like.
You'll find you're not fighting how "Windows" really want to behave and things will run a lot more smoothly.
Put it into the Panel2's Control collection via the Add() method, apply coordinates, anchor and docking programmaticaly.
I did similar thing once, and for that reason, I have ReplaceControl method, which I paste below:
static public void ReplaceControl(Control ToReplace, Form ReplaceWith) {
ReplaceWith.TopLevel=false;
ReplaceWith.FormBorderStyle=FormBorderStyle.None;
ReplaceWith.Show();
ReplaceWith.Anchor=ToReplace.Anchor;
ReplaceWith.Dock=ToReplace.Dock;
ReplaceWith.Font=ToReplace.Font;
ReplaceWith.Size=ToReplace.Size;
ReplaceWith.Location=ToReplace.Location;
ToReplace.Parent.Controls.Add(ReplaceWith);
ToReplace.Visible=false;
}
Only thing left to do is to create some control manually on the form, as the placeholder for your Form. Use label, for example.
From How to implement a-form-inside-a-form with runtime embedded forms switching?

C#: How to access public member of a custom control from another custom control

I am working on windows application form. I have a CustomControl (say MasterControl) on which i put a split panel and now my MasterControl is split into three parts say:
Pannel1
Pannel2
Pannel3
Now i develop three custom controls and put one in each of pannels e.g
Pannel1 have CustomControl1
Pannel2 have CustomControl2
Pannel3 have CustomControl3
Now somewhere in CustomControl3 I need to access a public member of CustomControl1. For which i wrote the following code:
((MasterControl)this.Parent)._oCustomControl1.PublicMember = this.PublicMember;
The code above doen't work in my case. When this line of code is executed in debug mode then a message box appears and states that "There is no code available for current location"
It's a really bad design for your controls to depend on how are the arranged on the parent container.
e.g. inside your third control, you are quering the property of the first one by accessing it from the parent, and then it's child control by name.
Your code will break very easily, if it can be compiled at all - I think the problem you're having is the order of compilation: in order for your parent form to be compiled, it needs to have child user controls finished. On the other hand the user controls you created need to have finished form.
It would be far better to set whatever behaviour you're after from the container of those controls - for example, by reacting to events from the control, and setting appropriate stuff on appropriate other controls (there are other ways as well ofcourse - the point is in the direction and flow of information - who's setting and using what).
If you have a split panel in your master control, you should go two levels up to find your master control:
((MasterControl)this.Parent.Parent)._oCustomControl1.PublicMember = this.PublicMember;
I found the answer by myself. I am positing here because it might help some one else.
The exact code is:
((MasterControl)this.Parent.Parent.Parent)._oCustomControl1.PublicMember = this.PublicMember;
Basically my coustomcontrol3 lies inside a split container panel, so when i wrote:
this.Parent then it points to Panel In which it is residing and if i wrote
this.Parent.Parent then it points to the spliter container in which above panel reside and if i wrote
this.Parent.Parent.Parent then it points to control in which this split container resides
I got the idea from "Farzin Zaker" answer, so thanks to him for his contribution

Categories

Resources