I have created one user control with some things on it, and I need to know if it's possible in my form1 click in one button and that button open my usercontrol but not inside the form1.
I want to see the usercontrol separated from the form1, so if the user want to close the usercontrol he will close it and can keep the from1, or if the user want's to minimize the form1 and keep the usercontrol in the screen.
i have tested with this
UC lauchUC = new UC(person);
lauchUC.Show();
but that don't show nothing, and also tested with this:
UC lauchUC = new UC(person);
this.Controls.Add(lauchUC);
but it appears in the form
can someone help me or telling me if it's possible show it separated from the form?
You could pass an instance of your UserControl to the constructor of the Form. In this constructor, you can add it to it's Controls. Just create a new Form and alter it's constructor.
The (container) Form:
public partial class Form1 : Form
{
public Form1(UserControl control)
{
InitializeComponent();
this.Controls.Add(control);
}
}
How to open it.
public void ButtonClick(object sender, EventArgs e)
{
var myControl = new MyUserControl();
var form = new Form1(myControl);
form.Show();
}
You can Place it in a Window and call Window.ShowDialog.
private void Button1_Click(object sender, EventArgs e)
{
Window window = new Window
{
Title = "My User Control Dialog",
Content = new UC(person)
};
window.ShowDialog();
}
Related
How to get userControl2 from button click on userControl1 into panel2?
Pressing the button New shows userControl1 control on Panel1, but I want to show usercontrol2 by pressing userControl1 button to panel2 userControl2. I couldn't do it.
private void btnNewDay_Click(object sender, EventArgs e) {
frmMain main = new frmMain();
main.panel2.Controls.Clear();
userControls.ucNewDay newDay = new userControls.ucNewDay();
main.panel2.Controls.Add(newDay);
main.userControlcontrol = true;
}
You're creating a new form with new frmMain();. What you need is to get a handle of the current form. You can do that by simply using the this keyword, but this is even optional and you don't have to use anything. However, in a user control, this refers to the control. In this case use the Parent property.
Change the first line:
frmMain main = new frmMain();
to:
frmMain main = (frmMain)this.Parent;
Alternatively, you can use the FindForm() method:
frmMain main = (frmMain)this.FindForm();
I have a MainForm which has a Textbox and a Button in it. Then I have a second form with a single button. On program start, the MainForm is initialized and when I click the button, the second form shows up (ShowDialog()) still keeping open the MainForm.
So I have these two forms opened next to each other. The thing I want is, that when I click the button, the button will send a string to the MainForm. MainForm will take the text and display it in it's textbox. But I want to make the change happen immediately - without hiding and showing the MainForm again. Sort of like refresh it, when the button on the second form is pressed.
How can I do that?
Note: It's important to have the text, which is send to the MainForm, have declared in the second form. (In my program, the text is dynamically being changed on the second form level)
Try sending the TextBox to the constructor of the second form and the second form, when you give click the button, change the Text property of the TextBox and it will appear as if it will be updated as they are referring to the same place.
public partial class Form1 : Form
{
public Form1(TextBox txt)
{
InitializeComponent();
this.txt = txt;
}
//variable
TextBox txt = null;
private void button1_Click(object sender, EventArgs e)
{
txt.Text = "Your text";
}
}
If I correctly understand, you need create a property in the winform.
ex:
public partial class frmLogin : Form
{
public bool LoggedIn
{
get { return loggedIn; }
}
public frmLogin()
{
InitializeComponent();
}
}
// Now, in your forms, you can do.
frmLogin frm = new frmLogin ();
frm.ShowDialog();
var value = frm.LoggedIn;
I'm creating a Windows application. On the Form I have 3 Buttons that will become user configurable from a Setup Menu. I have created a Second Form as a Popup where the user can add the relevent information for each button. When they Click "Done" on this Popup form I want to update the Button Text on Form 1.
public string ButtonVNC1Text
{
get
{
return btn1VNC.Text;
}
set
{
this.btn1VNC.Text = value;
}
}
Then on Form 2 when the Done Button is pressed I have the Following Code.
private void btn1VNCSetup_Click(object sender, EventArgs e)
{
//Collect Entered Data
VNCVars.VNC1Description = txtVNC1Des.Text;
//Update Button Text
BespakHMI main = new BespakHMI();
main.ButtonVNC1Text = txtVNC1Des.Text;
//Save the Data that has been entered into the Setup Fields for VNC1/2/3
SaveXML.SaveData();
this.Close(); // closes the Form2 instance.
}
But when Form 2 Close's the Text hasnt been updated. If I add a button on Form 1 and do the Following then the text does change.
private void button1_Click(object sender, EventArgs e)
{
ButtonVNC1Text = VNCVars.VNC1Description;
}
Thanks in advance.....
Here
// Update Button Text
BespakHMI main = new BespakHMI();
main.ButtonVNC1Text = txtVNC1Des.Text;
you are not updating the existing BespakHMI form, but a new invisible instance.
One way to resolve is to find the existing form like this
var main = Application.OpenForms.OfType<BespakHMI>().FirstOrDefault();
if (main != null)
{
main.ButtonVNC1Text = txtVNC1Des.Text;
// ...
}
try to pass form1 in the constructor of form 2
step1 :
instead of
Form2 form2 = new Form2();
use
Form2 form2 = new Form2(this);//You give to form2 the 'address' of form1 by using 'this'
step2 :
write the following in Form2:
Form1 creator = null;
public Form2(Form1 form1)
{
creator = form1;//the 'this aka address of form1' is saved in the creator variable
}
step 3:
use
creator.btn1VNC.Text = "hello there";// sets btn1VNC in the original form to "hello there" in form 2, using the address of form 2.
I know this is going to sound a little confusing but here it goes. So i have this parent form and when I click a button a new child form shows up(note that my parent form its still open). What i want is when i press a button from my child form i want is a new parent form to show up and to close the parent form that was already opening from the beginning. I hope this doesnt sound confusing. I try playing around with it but nothing seems to work
I have something like this on my parent form
Form2 loging = new Form2();
loging.ShowDialog();
on my child form
Form1 loging = new Form1();
loging.Close()
loging.ShowDialog();
this.Close();
Based on your comments to Mitch, it sounds like you need to rebind data on your main form after you close the child form. This is a much better approach than closing/reopening the main form.
In short, you cannot change a window's parent, and you cannot change whether a window is modal. Destroy the child, close the parent, open a new parent, show a new child. Alternatively, if the child window need not be modal, create the child with Form.Show() and then do something like the following in the child form:
parentForm.Close();
Form newParent = new NewParentForm();
newParent.Show();
this.BringToFront();
MFC used to be able to fake being modal, but it did so by using a custom window procedure - not something particularly easy to do in C#.
Based on your comment to Mitch, this what you should do:
In your parent form, create a static ListView object which point to you customer list
public partial class Form1 : Form
{
public static ListView lsvCustomer;
public Form1()
{
InitializeComponent();
// this will allow access from outside the form
lsvCustomer = this.listView1;
}
private void button1_Click(object sender, EventArgs e)
{
frmInput f = new frmInput();
f.ShowDialog(this);
}
}
Then in your child form, you update the list directly from your child form as below :
public partial class frmInput : Form
{
public frmInput()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//update user input to customer list in parent form
Form1.lsvCustomer.Items.Add(textBox1.Text);
}
}
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