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).
Related
Note: Form2 is MDI Child Form and I set all Form1's modifiers to Public
my method is not working when i want to change color or text or etc...
For example: There is two forms, Form1 and Form2. In Form2: label1.Click event i did this:
In Form2:
private void label1_MouseClick(object sender, MouseEventArgs e)
{
Form1 f1 = new Form1();
Label name = ((Label)sender);
f1.getInfoLabel(name);
}
Okay, everythings working until here, but in there:
In Form1:
public void getInfoLabel(Label obj)
{
pictureBox1.BackColor = obj.Forecolor; //not working
TextBox1.Text = obj.Text; //not working
MessageBox.Show(obj.Forecolor.ToString()); //working
MessageBox.Show(obj.Text); //working
}
Any help? Please.
Instead of
Form1 f1 = new Form1();
use
Form1 f1 = this.MDIParent as Form1;
if (f1 != null)
{
f1.getinfolabel(sender as Label);
}
As has been pointed out, you are creating a new Form1 instance and interacting with that instead of interacting with the parent form. As long as you are correctly setting MDIParent of Form2 then the above should work.
An alternate is to use:
Form1 f1 = Appliction.OpenForms.OfType<Form1>().FirstOrDefault();
if (f1 != null)
{
f1.getinfolabel(sender as Label);
}
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 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 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