my request is suppose i have two sdi form. one sdi main form has button and when user click on that button then a overlay semi transparent window will appear and cover the main sdi form and as well as another sdi will come on top of the over lay window.
here is my overlay window code
namespace CSRAssistant
{
public partial class MaskedDialog : Form
{
static MaskedDialog mask;
static Form frmContainer;
private Form dialog;
private UserControl ucDialog;
private MaskedDialog(Form parent, Form dialog)
{
this.dialog = dialog;
this.FormBorderStyle = FormBorderStyle.None;
this.BackColor = System.Drawing.Color.Black;
this.Opacity = 0.50;
this.ShowInTaskbar = false;
this.StartPosition = FormStartPosition.Manual;
this.Size = parent.ClientSize;
this.Location = parent.PointToScreen(System.Drawing.Point.Empty);
parent.Move += AdjustPosition;
parent.SizeChanged += AdjustPosition;
}
private MaskedDialog(Form parent, UserControl ucDialog)
{
this.ucDialog = ucDialog;
this.FormBorderStyle = FormBorderStyle.None;
this.BackColor = System.Drawing.Color.Black;
this.Opacity = 0.50;
this.ShowInTaskbar = false;
this.StartPosition = FormStartPosition.Manual;
this.Size = parent.ClientSize;
this.Location = parent.PointToScreen(System.Drawing.Point.Empty);
parent.Move += AdjustPosition;
parent.SizeChanged += AdjustPosition;
}
private void AdjustPosition(object sender, EventArgs e)
{
Form parent = sender as Form;
this.Location = parent.PointToScreen(System.Drawing.Point.Empty);
this.ClientSize = parent.ClientSize;
}
//
public static DialogResult ShowDialog(Form parent, Form dialog)
{
mask = new MaskedDialog(parent, dialog);
dialog.StartPosition = FormStartPosition.CenterParent;
mask.MdiParent = parent.MdiParent;
mask.Show();
DialogResult result = dialog.ShowDialog(mask);
mask.Close();
return result;
}
public static DialogResult ShowDialog(Form parent, UserControl dialog)
{
mask = new MaskedDialog(parent, dialog);
frmContainer = new Form();
frmContainer.ShowInTaskbar = false;
frmContainer.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
frmContainer.StartPosition = FormStartPosition.CenterParent;
frmContainer.Height = dialog.Height;
frmContainer.Width = dialog.Width;
frmContainer.Controls.Add(dialog);
mask.MdiParent = parent.MdiParent;
mask.Show();
DialogResult result = frmContainer.ShowDialog(mask);
frmContainer.Close();
mask.Close();
return result;
}
public static void CloseDialog()
{
if (frmContainer != null)
{
frmContainer.Close();
}
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// MaskedDialog
//
this.ClientSize = new System.Drawing.Size(284, 262);
this.Name = "MaskedDialog";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MaskedDialog_FormClosing);
this.Load += new System.EventHandler(this.MaskedDialog_Load);
this.ResumeLayout(false);
}
private void MaskedDialog_Load(object sender, EventArgs e)
{
}
private void MaskedDialog_FormClosing(object sender, FormClosingEventArgs e)
{
}
}
}
this way i am calling my overlay when user click on main sdi button.
Form2 f2 = new Form2();
f2.Show();
MaskedDialog.ShowDialog(this, f2);
f2.Dispose();
f2 = null;
when i am running the code and MaskedDialog.ShowDialog(this, f2); is calling then i am getting a error....the error message is Form that is already visible cannot be displayed as a modal dialog box. Set the form's visible property to false before calling showDialog.
so i am not being able to understand where i am making the mistake. anyone can help me to fix the problem. thanks
You're doing this wrong - call mask.ShowDialog(), not mask.Show() and dialog.ShowDialog(). The error message is pretty clear - you can't ShowDialog a form that's already visible - and that's exactly what you're doing in dialog.ShowDialog(mask);.
Or, in a way that probably more closely shows what you want to do, when calling MaskedDialog.ShowDialog, do this:
Form2 f2 = new Form2();
MaskedDialog.ShowDialog(this, f2);
In other words - don't show the form you're going to ShowDialog. It's going to be made visible by the ShowDialog call.
Related
I have a form. I want to make sure that the RegisterForm.cs form comes as childform when the toggle button I added on this form is activated. It works in a different position from the main panel when I call it normally. Whatever I've done, I haven't been able to solve this problem, can you help me with that?
FormMainMenu.cs
public void OpenChildForm(Form childForm)
{
if (currentChildForm != null)
{
//open only form
currentChildForm.Close();
}
currentChildForm = childForm;
childForm.TopLevel = false;
childForm.FormBorderStyle = FormBorderStyle.None;
childForm.Dock = DockStyle.Fill;
panelDesktop.Controls.Add(childForm);
panelDesktop.Tag = childForm;
childForm.BringToFront();
childForm.Show();
lblTitleChildForm.Text = childForm.Text;
}
SozlesmeFormu.cs
public partial class SozlesmeFormu : Form
{
private bool toggleCheck = false;
public SozlesmeFormu()
{
InitializeComponent();
textBox1.Multiline = true;
textBox1.ScrollBars = ScrollBars.Both;
}
private void toggleSozlesme_CheckedChanged(object sender, EventArgs e)
{
toggleCheck = true;
toggleSozlesme.Enabled = false;
toggleSozlesme.OnBackColor = Color.DarkGray;
this.Close();
FormMainMenu formMain = new FormMainMenu();
formMain.OpenChildForm(new RegisterForm());
}
}
Whatever I did, I couldn't call RegisterForm as childform. It's always irrelevant from the main panel.
This question already has answers here:
"Invoke or BeginInvoke cannot be called on a control until the window handle has been created" only occurs the second time form opens
(1 answer)
Invoke or BeginInvoke cannot be called on a control until the window handler has been created
(1 answer)
Closed 1 year ago.
I have Windows Form project that turns one format of csv file into another.
I am trying to show the converting progress while converting.
Converting method throws an exception if converting fails.
When converting fails, I close ConvertingScreen but when I close ConvertingScreen System.InvalidOperationException occurs.
invoke or begininvoke cannot be called on a control until the window handle has been created
It seems it never happens if I add some waits before I close. I have tried checking HandleCreated but it didnt go well. I used to do this kind of jobs by adding only visible label while it is on progress. This is my first time doing such like this way. Is this bad practice? If not how can I fix this? Below is simple code that can describe everything what is happening to me.
// MainForm
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
ConvertingScreen.ShowScreen();
// Converts here and results in failure so throwing an exception
throw new NotImplementedException();
}
catch (Exception)
{
ConvertingScreen.CloseForm();
MessageBox.Show("error");
}
}
}
// ConvertingScreen
public class ConvertingScreen : Form
{
public ConvertingScreen()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
// label1
this.label1.Font = new System.Drawing.Font("MS UI Gothic", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(316, 22);
this.label1.TabIndex = 0;
this.label1.Text = "";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
// ConvertingScreen
this.BackColor = System.Drawing.Color.LightGray;
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
this.ClientSize = new System.Drawing.Size(340, 40);
this.ControlBox = false;
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.MaximizeBox = false;
this.MaximumSize = new System.Drawing.Size(340, 40);
this.MinimizeBox = false;
this.MinimumSize = new System.Drawing.Size(340, 40);
this.Name = "ConvertingScreen";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "ConvertingScreen";
this.TopMost = true;
this.ResumeLayout(false);
}
private Label label1;
private delegate void CloseDelegate();
private delegate void UpdateTextDelegate(string text);
private static ConvertingScreen form;
public static void ShowScreen()
{
if (form != null) return;
form = new ConvertingScreen();
Thread thread = new Thread(new ThreadStart(ConvertingScreen.ShowForm));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
private static void ShowForm()
{
if (form != null) Application.Run(form);
}
public static void CloseForm()
{
form?.Invoke(new CloseDelegate(ConvertingScreen.CloseFormInternal));
}
private static void CloseFormInternal()
{
if (form != null)
{
form.Close();
form = null;
}
}
public static void Update(string text)
{
if (form != null) form.UpdateLabel(text);
}
private void UpdateLabel(string text)
{
if (InvokeRequired)
{
this.Invoke(new UpdateTextDelegate(UpdateLabel), new object[] { text });
return;
}
label1.Text = text;
}
}
I need some helps on sub form control:
I am developing a C# windows form. The form is divided into two panels. The left hand side panel contains several buttons and the right hand side panel is for displaying different forms.
So, when you click the buttons on the left panel, a corresponding form will be displyed on right panel.
E.g. Button1: ShowForm1, Button2: ShowForm2, Button3: ShowForm3
I am able to implement the situation mentioned above. But, I have not idea how to implement the following situation:
ShowForm3 is clicked so Form3 is displayed, then there is a button "ShowForm4" in Form3. If the button is clicked, then the form3 should be closed and Form4 should be shown in the panel.
I want Form4 shows in the panel rather than just a pop-up form (i.e. Form4.show()).
How can I do that? Thanks.
My coding are as follows:
private void MainForm_Load(object sender, EventArgs e)
{
//Master Form
this.btn_show1.Click += new EventHandler(btn_show_Click);
this.btn_show2.Click += new EventHandler(btn_show_Click);
this.btn_show3.Click += new EventHandler(btn_show_Click);
}
void btn_show_Click(object sender, EventArgs e)
{
this.pnl_ShowForms.Controls.Clear();
int tag = Convert.ToInt32( (sender as Button).Tag);
switch (tag)
{
case 1:
Form1 frm1 = new Form1();
frm1.FormBorderStyle = FormBorderStyle.None;
frm1.Dock = DockStyle.Fill;
frm1.WindowState = FormWindowState.Maximized;
frm1.TopLevel = false;
this.pnl_ShowForms.Controls.Add(frm1);
frm1.Show();
break;
case 2:
Form2 frm2 = new Form2();
frm2.FormBorderStyle = FormBorderStyle.None;
frm2.Dock = DockStyle.Bottom;
frm2.WindowState = FormWindowState.Maximized;
frm2.TopLevel = false;
this.pnl_ShowForms.Controls.Add(frm2);
frm2.Show();
break;
case 3:
Form3 frm3 = new Form3();
frm3.FormBorderStyle = FormBorderStyle.None;
frm3.Dock = DockStyle.Fill;
frm3.WindowState = FormWindowState.Maximized;
frm3.TopLevel = false;
this.pnl_ShowForms.Controls.Add(frm3);
frm3.Show();
break;
}
you will get Panel using this.parent. Write following code in Button_Click event of Form3.
void btn_Click(object sender, EventArgs e)
{
Panel mPanel = (Panel)this.Parent;
this.Close();
Form4 frm1 = new Form4();
frm1.FormBorderStyle = FormBorderStyle.None;
frm1.Dock = DockStyle.Fill;
frm1.WindowState = FormWindowState.Maximized;
frm1.TopLevel = false;
mPanel.Controls.Add(frm1);
frm1.Show();
}
I'm working on a legacy WinForms MDI application and have some trouble making the child forms behave as I want.
My objective is to have the child form always maximized (docked).
The problem is, that even if I set MaximizeBox to false the maximize/resize button appears in the MDIs toolstrip and let the user resize (undock) the child form.
The only way to avoid this is to set ControlBox to false but then the close button disappears to (thats not what I want).
I've already tried to use a fixed FormBorderStyle and to maximize the child form when the resize event is fired but none of my approaches worked.
Is there any super secret property I have missed or is it just impossible?
Best Regards & Thanks in advance
Update
I wrote a sleazy method (thanks to #rfresia) for handling my child form, it may help others who run into the same issue:
//All child forms derive from ChildForm
//Parent MDI Form implementation
//...
private void ShowForm(ChildForm form)
{
//Check if an instance of the form already exists
if (Forms.Any(x => x.GetType() == form.GetType()))
{
var f = Forms.First(x => x.GetType() == form.GetType());
f.Focus();
f.WindowState = FormWindowState.Maximized;
}
else
{
//Set the necessary properties (any other properties are set to default values)
form.MdiParent = this;
form.MaximizeBox = false;
form.MinimizeBox = false;
form.WindowState = FormWindowState.Maximized;
Forms.Add(form);
form.Forms = Forms;
form.Show();
form.Focus();
//Lets make it nasty (some forms aren't rendered properly otherwise)
form.WindowState = FormWindowState.Normal;
form.WindowState = FormWindowState.Maximized;
}
}
//...
//ChildForm implementation
//...
public List<Form> Forms { get; set; }
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
Forms.RemoveAll(x => x.GetType() == GetType());
}
protected override void OnResize(EventArgs e)
{
WindowState = FormWindowState.Maximized;
}
The problem wasn't easy to solve, but I accidentally found the answer and it's quite simple; set the windowstate of the child form to Normal by default. Then make sure that you reset the windowstate of the child window AFTER you call the Show() method.
Example:
private void ShowNewForm(object sender, EventArgs e)
{
Form childForm = new Form();
childForm.MdiParent = this;
childForm.Text = "Window " + childFormNumber++;
childForm.Show();
childForm.WindowState = FormWindowState.Maximized;
}
You can override the OnResize of each child form you want to make sure does not minimize. Or create a BaseForm and inherit all children forms from it.
protected override void OnResize(EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
}
Also, you can use X,y coordinates, but OnResize should be enough. Put this in the child form constructor:
this.WindowState = FormWindowState.Maximized;
Point NewLoc = Screen.FromControl(this).WorkingArea.Location;
//Modifiy the location so any toolbars & taskbar can be easily accessed.
NewLoc.X += 1;
NewLoc.Y += 1;
this.Location = NewLoc;
Size NewSize = Screen.FromControl(this).WorkingArea.Size;
//Modifiy the size so any toolbars & taskbar can be easily accessed.
NewSize.Height -= 1;
NewSize.Width -= 1;
this.Size = NewSize;
this.MinimumSize = this.Size;
this.MaximumSize = this.MinimumSize;
I got the code for the X,Y from here:
http://bytes.com/topic/c-sharp/answers/278649-how-do-i-prevent-form-resizing
form1 obj = new form1 ();
obj.MdiParent = MDIGracular.ActiveForm;
obj.StartPosition = FormStartPosition.CenterParent;
obj.WindowState = FormWindowState.Minimized;
obj.Dock = DockStyle.Fill;
obj.Show();
obj.WindowState = FormWindowState.Maximized;
This is how I overcame the same promblem, cannot remember where I found the code.
private const int WM_SYSCOMMAND = 0x112;
private const int SC_MINIMIZE = 0xF020;
private const int SC_MAXIMIZE = 0xF030;
private const int SC_CLOSE = 0xF060;
private const int SC_RESTORE = 0xF120;
protected override void WndProc(ref Message msg)
{
if ((msg.Msg == WM_SYSCOMMAND) &&
(((int)msg.WParam == SC_MINIMIZE) || ((int)msg.WParam == SC_MAXIMIZE) ||
((int)msg.WParam == SC_CLOSE)) || ((int)msg.WParam == SC_RESTORE))
{
//do nothing
} // end if
else
{
base.WndProc(ref msg);
} // end else
}
In my app I found if I put just these two lines in the form loaded event it worked. Thanks sarvjeet for the basic idea. +1 for you
this.WindowState = FormWindowState.Minimized;
this.WindowState = FormWindowState.Maximized;
This question already has answers here:
How to run an "empty" Windows Application that only has a NotifyIcon?
(3 answers)
Closed 6 years ago.
I added notifyIcon to the container and set Visible = true option, but no icon appeared.
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.SuspendLayout();
//
// notifyIcon1
//
this.notifyIcon1.Text = "Manager";
this.notifyIcon1.Visible = true;
//
// Form1
//
this.ClientSize = new System.Drawing.Size(0, 0);
this.ShowInTaskbar = false;
this.Visible = false;
}
I believe you need to add a few more events for this to work, Hope this helps
public Form2()
{
InitializeComponent();
notifyIcon1.Icon = SystemIcons.Asterisk;
notifyIcon1.DoubleClick += new EventHandler(notifyIcon1_DoubleClick);// to bring it back
this.Resize += new EventHandler(Form2_Resize);// to move it to tray
}
void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
Show();
this.BringToFront();
this.WindowState = FormWindowState.Normal;
}
void Form2_Resize(object sender, EventArgs e)
{
if (this.WindowState ==FormWindowState.Minimized)
Hide();
}
notify Icon is shown when form is minimize. Try this
this.WindowState = FormWindowState.Minimized;