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();
}
Related
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.
I set the button visible property to false of Form2. How I will make the button(Form2) visible when I click a button(a button that also opens Form2) from Form1.
I tried this :
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
f2.button1.Visible = true;
f2.button1.Location = new Point(200, 200);
}
Create a method in Form2
public void setButton1Visible(boolean flag){
this.button1.Visible = flag;
}
You cannot access the button directly from Form1. (Actually you can,but it is not right way to solve it.
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
f2.setButton1Visible(true);
}
I think button1 is declared as private. Your code will work if you declare button1 as public.
public System.Windows.Forms.Button button1;
Imagine you control is in form 1. Set corresponding control "modifiers = public" from control property window
Form 1
private void ShowForm2_Click(object sender, EventArgs e)
{
Form2 NewForm = new Form2();
NewForm.Owner = this;
NewForm.Show();
}
In Form 2
private void ChangeProperty_Click(object sender, EventArgs e)
{
(this.Owner as Form1).MyButton.Visible = false;
}
//while doing this Control In Form1 will be hidden :)
Currently Form1 has textBox1 and Form1 has StartPosition = CenterScreen, the textBox1 has textBox1_MouseClick
Code for textBox1_MouseClick
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
}
In Form2 has also StartPosition = CenterScreen when I click textBox1
the Form2 will cover the textBox1.
What I want to happen is that it will not cover the textBox1 when the Form2 will be displayed, it should be displayed under textBox1 its like a tooltip. How can I achieve this?
UPDATED CODE:
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
Form2 frm2 = new Form2();
frm2.StartPosition = FormStartPosition.Manual;
frm2.Location = new Point(this.Location.X + textBox1.Location.X, this.Location.Y + textBox1.Location.Y);
frm2.ShowDialog();
}
private void textBox2_MouseClick(object sender, MouseEventArgs e)
{
Form2 frm2 = new Form2();
frm2.StartPosition = FormStartPosition.Manual;
frm2.Location = new Point(this.Location.X + textBox2.Location.X, this.Location.Y + textBox2.Location.Y);
frm2.ShowDialog();
}
NO TEXTBOX CLICKED:
TEXTBOX1 CLICKED:
TEXTBOX2 CLICKED:
What I have posted before is a generic solution for the long-term. To quickly address the problem at hand, you should simply do this:
frmKeyboard.Location = this.PointToScreen(new Point(txtYourTextBox.Left, txtYourTextBox.Top + txtYourTextBox.Height));
You should better do it with a UserControl instead of a form. Simply set the position of your UserControl to (textbox1.Left, textbox1.Top + textbox1.Height).
You should rather add a custom TextBox class in your project inheriting from standard TextBox and wire its Enter/Leave events, showing/hiding your keyboard control therein and wire its "keypress" event to modify the Text of your custom textbox. This will let you create as many instances of the textbox as you need. You could even make your keyboard control a static member of your custom TextBox to save some resources.
You can also use something like this:
private Point GetPosition()
{
return new Point(this.Location.X + this.textBox1.Location.X, this.Location.Y + this.textBox1.Location.Y);
}
private void button1_Click(object sender, EventArgs e)
{
Form2 fm = new Form2();
fm.Location = this.GetPosition();
fm.ShowDialog();
}
This is not accurate yet. You have to add Form Borderwith to the position. For Form2 set StartPosition = Manual
I'm trying to create one window and on that window I would have a toolbar with different buttons.
When I go to click on one of the buttons It would display something like information about a person or when I click on another button It would display some other information about employees.
How can I do this. Can I make add pages and then insert that page onto a grid or panel when that button calls for it?
Or Should I just make multiple panels and create them all on one window(but if I do this how would it be easy for me to edit each of those panels when they are stacked on one another all in one window). I hope I'm being clear about this, Idk how else to ask this question. Any help is appreciated.
Also how do I dock something so that it resizes itself when maximize or minimize?
One way is to create another form and open it from a button event:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
}
If you want everything in one window, you can create a user control and add it to the first window:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
UserControl1 control = new UserControl1();
control.Dock = DockStyle.Fill;
this.Controls.Add(control);
}
}
Another option is using child forms:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.IsMdiContainer = true;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.MdiParent = this;
form2.Show();
}
}
Create a panel for each button you have. Then:
panelx.Dock = DockStyle.Fill; //this will fill the window.
And put all you want to show for that button on that panel.
When you want to show, say panel2 instead of panel1:
panel1.Hide();
panel2.Show();
As mentioned earlier, this can be achieved using a tab control.
public ParentForm()
{
TabControl tabcontrol1 = new TabControl();
tabcontrol1.Dock = DockStyle.Top;
TabPage tab1 = new TabPage("Form1 Name");
Form1 frm1 = new Form1();
frm1.TopLevel = false;
frm1.Parent = tab1;
frm1.Visible = true;
tabcontrol1.TabPages.Add(tab1);
Form2 frm2 = new Form2();
TabPage tab2 = new TabPage("Form2 Name");
frm2.TopLevel = false;
frm2.Parent = discotab;
frm2.Visible = true;
tabcontrol1.TabPages.Add(discotab);
}
Using "stacked" custom user controls
public ParentForm()
{
InitializeComponent();
MyUserControl1 control1 = new MyUserControl1();
control1.Dock = DockStyle.Bottom;
control1.BringToFront();
}
private void button1_Click(object sender, EventArgs e)
{
MyUserControl2 control2 = new MyUserControl2();
control2.Dock = DockStyle.Bottom;
control2.BringToFront();
}
I implemented a "paged options" dialog box in Windows Forms a while ago. My blog post about it is here: http://www.differentpla.net/content/2004/10/implementing-a-paged-options-dialog (the images are missing, though).
i have created an application in which a menustrip is present in which 2 buttons are there, one for ADD, and another for UPDATE & both controls are in a single form, means a button of add & update is there in a single form, whenever i press add button in menustrip update button will be disabled, and when i press update on menustrip the add button will disable. how to do this? i m doing this by show method but that form is opening multiple times using show().
private void addRecordsToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 f2 = new Form1();
f2.MdiParent = this;
f2.Show();
f2.button1.Enabled = true;
}
private void updateRecordsToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 f2 = new Form1();
f2.MdiParent = this;
f2.Show();
f2.button2.Enabled = true;
f2.button1.Enabled = false;
}
you simply have to use a single form in this case. try using the singleton approach -
http://hashfactor.wordpress.com/2009/03/31/c-winforms-create-a-single-instance-form/
try using .ShowDialog() instead .Show() and no other form will be able to be clicked on until that one closes.
To do that you'll need to have an instance of that Form outside of those methods that you dismply show if the Form has already been created, or create and show it if it has not (this is the singleton pattern). Here's an example:
Form1 f2 = null;
private void addRecordsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (f2 == null)
{
f2 = new Form1();
f2.MdiParent = this;
f2.button1.Enabled = true;
}
f2.Show();
}
private void updateRecordsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (f2 == null)
{
f2.MdiParent = this;
f2.button2.Enabled = true;
f2.button1.Enabled = false;
}
f2.Show();
}
One question on your disabling of the menu items though, how do you plan on re-enabling them after they have been disabled?
just try to check that form is already opened or not by using its Text Property.. if it is opened just focus on that form other wise show that form as normally
private void button1_Click(object sender, EventArgs e)
{
bool IsOpen = false;
foreach (Form f in Application.OpenForms)
{
if (f.Text == "Form1")
{
IsOpen = true;
f.Focus();
break;
}
}
if (IsOpen == false)
{
Form f1 = new Form1();
f1.Show();
}
}
Try This Guys Its Simple