C# Transparent Form in Panel - c#

im trying to create a semi-transparent form which is displayed in a panel. i can display the form in the panel but the opacity property wont work and the form is non-transparent.
private void button1_Click(object sender, EventArgs e)
{
Form fr = new Form();
fr.FormBorderStyle = FormBorderStyle.None;
fr.BackColor = Color.Black;
fr.TopLevel = false;
fr.Opacity = 0.5;
this.panel1.Controls.Add(fr);
fr.Show();
}
any ideas how i can handle that?
Thanks for your answeres!

Winforms only supports partial transparency for top-level forms. If you want to create an application with partially-transparent UI elements, you either need to use WPF, or handle all the drawing yourself. Sorry to be the bearer of bad news.

Your form is added as a child control of panel1 which is child of the main form which is of default Opacity = 1.
To see Opacity at work, try this:
private void button1_Click(object sender, EventArgs e)
{
Form fr = new Form();
fr.FormBorderStyle = FormBorderStyle.None;
fr.BackColor = Color.Blue;
fr.TopLevel = false;
//fr.Opacity = 0.5;
this.Opacity = 0.5; // add this
this.panel1.Controls.Add(fr);
fr.Show();
}
I guess you want the panel to look semi-transparent, you have to use another method and work with the form itself.

Related

Adding a child form to a Non MDI win form prevents child controls inside child form mouse clickable

I am adding a child form inside a parent form without setting MDI parent of child form to parent form. Following is the code :
private void Form1_Load(object sender, EventArgs e)
{
ChildForm openForm = new ChildForm();
openForm.Show();
openForm.Visible = true;
openForm.TopLevel = false;
this.Controls.Add(openForm);
}
Clicking text inside any control within child form just selects the text completely and does not allow editing text directly using mouse. Editing text using keyboards is working fine though.
I cannot set ChildForm.MDI= this because of some other issues with a tab control. Is there any way to prevent this and allow user to edit text using mouse.
This works for me in metroframework
private void metroButton1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.TopLevel = true;
frm.Opacity = 10;
frm.Show();
frm.ControlBox = false;
frm.Movable = false;
frm.BorderStyle = MetroFormBorderStyle.None;
frm.DisplayHeader = false;
frm.TopLevel = false;
panel1.Controls.Add(frm);
frm.Dock = DockStyle.Fill;
frm.BringToFront();
this.TopMost = true;
frm.StyleManager = metroStyleManager1;
}
Consider using a UserControl instead. It is designed the same way as a Form but it does not have borders and it is intended to be used inside other forms.
You only have to compile the project that contains it once so that it appears in Visual Studio toolbox (assuming the project is in the same solution and is either the same project as the one containing the Form or has a reference to it.
Or alternatively, you can load it dynamically similar to what you have done in your example. But if you systematically load the same single user control, it is easier to do in the designer...
Sometime, you might also want to make some adjustment to the layout.

how to exclude control from a WinForm.Opacity in C#

I have a form that i set it's Opacity as 50% like this:
this.Opacity = 0.5D; <--this==Form
My problem is that everything that on the form is with an Opacity of 50%
I have two buttons on the form and I want them without Opacity.
I know that this.Opacity included all Controls and for some reason the graphics too
My question is, How to Exclude the Opacity of the controls?
Example Image:
Thanks!
Since Control doesn't have Opacity property and plus that, most of the controls doesn't support transparent colors, then a working solution can be this:
Create a Form called MainForm and place all the controls you're going to be excluded.
1.1 Set both of BackColor and TransparencyKey properties of MainForm to the same color, e.g Color.Red
Create another form named TransparentForm and place all controls that must become transparent. Set ShowInTaskbar property to False.
In the MainForm Load event show the TransparentForm and send it to back.
private void MainForm_Load(object sender, EventArgs e)
{
TransparentForm form = new TransparentForm();
form.Opacity = 0.5D;
form.Show();
form.SendToBack();
}
The position of controls in both form must be such that, when combined, it shows the proper user interface.
Crate a C# project and add 3 forms named
MAIN
BACKGOUND
Child
and add the following code for "MAIN" form load event;
private void MAIN_Load(object sender, EventArgs e)
{
Child frm1 = new Child();
BACKGOUND frm2 = new BACKGOUND();
frm2 .WindowState = System.Windows.Forms.FormWindowState.Maximized;
frm2.Opacity = 0.5;
frm2.Show();
frm1.ShowDialog();
frm2.Close();
}

How to close a tab when a form embedded in it closed?

I have my form embedded on top of the tabpage. When I closed the form which is on top of the tabpage, how do I also make the tabpage close as well?
The code when I put my form on the tabpage is more less like this:
client c = new client(car_name, owner); //here client is another winform class
c.TopLevel = false;
c.Visible = true;
c.BackColor = Color.Ivory;
c.FormBorderStyle = FormBorderStyle.None;
c.Dock = DockStyle.Fill;
tabControl1.TabPages[tab_index].Controls.Add(c);
Use the FormClosing event :
private void ClientForm_FormClosing(object sender, FormClosedEventArgs e)
{
((TabControl)((TabPage)this.Parent).Parent).TabPages.Remove((TabPage)this.Parent);
}

Can I load a new form into panel?

I am working on Windows Application.I am having a menustrip in one form and I want to ask that, can I have a panel which will load new form on particular click of menustripitem.
Ex:
File Data
ABC Hello
XYZ Bye
These is my menu bar.On click of ABC I dont want to go on different form can I do something
(whatever I want to)on the same form using panel.
Thanks
I think that I had the same question.
But I found the answer for it
CodeProject Example
First you have to configurate the Form:
myForm.FormBorderStyle = FormBorderStyle.None;
And then, manipulates the action:
Form1 myForm = new Form1();
myForm.TopLevel = false;
myForm.AutoScroll = true;
frmMain.Panel2.Controls.Add(myForm);
myForm.Show();
Hope to help you. Hugs :D
You can use MDI Form.
Try something like this
//Create a new instance of the MDI child template form
Form2 child= new Form2();
//Set parent form for the child window
child.MdiParent=this;
//Display the child window
child.Show()
you can also refer to this site.
If you put the whole content of the target form into a UserControl, you can add a panel to your main form and place the UserControl on that panel.
You still have option to display a separate form by creating an empty form and placing the same UserControl on that form, too.
As Int3 ὰ already points out, you could use MDI forms instead. However, if you want to use dockable panels, the UserControl would be the way to go.
Add two panels on your form, only one will be visible at the same time. Then, add two events on your menus:
private void ABCToolStripMenuItem_Click(object sender, EventArgs e) {
panelABC.Visible = true;
panelXYZ.Visible = false;
}
private void XYZToolStripMenuItem_Click(object sender, EventArgs e) {
panelABC.Visible = false;
panelXYZ.Visible = true;
}
private void pbxpurchase_Click(object sender, EventArgs e)
{
contentpnl.Controls.Clear();//contentpnl is the panelname
purchasebook purchasebk = new purchasebook();//purchasebook is a formname
purchasebk.TopLevel = false;
purchasebk.AutoScroll = true;
contentpnl.Controls.Add(purchasebk);
purchasebk.Dock = DockStyle.Fill;
purchasebk.Show();
}
try this 100% tested

full screen mode, but don't cover the taskbar

I have a WinForms application, which is set to full screen mode when I login.
My problem is it's covering the Windows taskbar also. I don't want my application to cover the taskbar.
How can this be done?
The way I do it is via this code:
this.MaximizedBounds = Screen.FromHandle(this.Handle).WorkingArea;
this.WindowState = FormWindowState.Maximized;
This is probably what you want. It creates a 'maximized' window without hiding the taskbar.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load( object sender, EventArgs e )
{
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
Left = Top = 0;
Width = Screen.PrimaryScreen.WorkingArea.Width;
Height = Screen.PrimaryScreen.WorkingArea.Height;
}
}
I had answer it here:
One thing I left out of the description--I'd turned off the maximize button. When I tested turning that property back on, the task bar showed up again. Apparently it assumes if you don't want a maximize button you are creating a kiosk-style application where you don't want your users to see anything but the application screen. Not exactly what I'd expect, but works I guess.
I had this problem and solved it by Jeff's help. First, set the windowstate to Maximized. but Do not disable the MaximizeBox. Then if you want MaximizeBox to be disabled you should do it programmatically:
private void frmMain_Load(object sender, EventArgs e)
{
this.MaximizeBox = false;
}
Arcanox's answer is great for a single monitor, but if you try it on any screen other than the leftmost, it will just make the form disappear. I used the following code instead.
var workingArea = Screen.FromHandle(Handle).WorkingArea;
MaximizedBounds = new Rectangle(0, 0, workingArea.Width, workingArea.Height);
WindowState = FormWindowState.Maximized;
The only difference is I'm overriding the top & left values to be 0, 0 since they will be different on other screens.
If you have multiple screens, you have to reset location of MaximizedBounds :
Rectangle rect = Screen.FromHandle(this.Handle).WorkingArea;
rect.Location = new Point(0, 0);
this.MaximizedBounds = rect;
this.WindowState = FormWindowState.Maximized;
I'm not good at explaining but this is the code I used to maximize or to full screen the winforms which would not cover up the taskbar. Hope it helps. ^^
private void Form_Load(object sender, EventArgs e)
{
this.Height = Screen.PrimaryScreen.WorkingArea.Height;
this.Width = Screen.PrimaryScreen.WorkingArea.Width;
this.Location = Screen.PrimaryScreen.WorkingArea.Location;
}
If you want to use WindowState = Maximized;, you should first indicate the size limits of the form maximized by the MaximizedBounds property...
Example:
MaximizedBounds = Screen.FromHandle(this.Handle).WorkingArea;
WindowState = FormWindowState.Maximized;
Where are you limiting the size of your form to the work area that is the desktop area of ​​the display
If Maximizing isn't what you're looking for, then you'll need to calculate the window size yourself by checking for the location and size of the taskbar:
find-out-size-and-position-of-the-taskbar
Try without FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; and comment line like :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load( object sender, EventArgs e )
{
// FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
Left = Top = 0;
Width = Screen.PrimaryScreen.WorkingArea.Width;
Height = Screen.PrimaryScreen.WorkingArea.Height;
}
}
private void frmGateEntry_Load(object sender, EventArgs e)
{
// set default start position to manual
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
// set position and size to the Form.
this.Bounds = Screen.PrimaryScreen.WorkingArea;
}
This was very useful to me:
private void Form1_Load(object sender, EventArgs e)
{
this.MaximizedBounds = Screen.FromHandle(this.Handle).WorkingArea;
this.WindowState = FormWindowState.Maximized;
}
I know it's a bit too late, but it will help others too in the future.
the answered code above is working but still in my case if the taskbar is auto-hiding and showing it wont show the taskbar once it hides from the screen. I solved this problem by adding -1` to the working area height.
var workingArea = Screen.FromHandle(Handle).WorkingArea;
MaximizedBounds = new Rectangle(0, 0, workingArea.Width, workingArea.Height - 1);

Categories

Resources