How to Create Popup Form With Same Location of Other Control - c#

I have read about Control.PointToClient or Control.PointToScreen on MSDN.
But how to show another form as same location as TextBoxwhen Button clicked, if I have this control hierarchy ?
+- Form
+--- Panel
+---- Panel
+------ TextBox
+------ Button

Use PointToScreen with an empty point (0, 0) to get the location of the control relative to the screen, then just show the form there (make sure StartPosition is Manual):
Point controlPosition = myTextBox.PointToScreen(Point.Empty);
MyForm newForm = new MyForm();
newForm.Location = controlPosition;
newForm.Show(this);

If you show new form as showdialog you need to use :
newForm.StartPosition = FormStartPosition.Manual;

Related

Parent form's control to be in front of child form (Winforms)

I have a program with a horizontal dropbox with 4 options to choose from in which "sub buttons" will appear when I click on one of these buttons:
Here is the interface:
And what I want to do is that when I click on one of the drop down menu options, I want a childform to spawn on the black area, using this:
private Form activeForm = null;
private void openChildForm(Form childForm)
{
if (activeForm != null)
activeForm.Close();
activeForm = childForm;
childForm.TopLevel = false;
childForm.FormBorderStyle = FormBorderStyle.None;
childForm.Dock = DockStyle.Fill;
MainInterfacePanel.Controls.Add(childForm);
MainInterfacePanel.Tag = childForm;
childForm.BringToFront();
childForm.Show();
}
However, when I do so, the childform does appear normally, but it covers the dropdown menu, preventing me to select other functions:
Here's how it looks like(the childform is still a blank form, thus the whiteness):
I tried solving this issue by using item.bringToFront() on the buttons, but it dosent seem to work.
Basically, I want the Parent Form to stay behind the childform, while having the buttons be infront of the childform, like an overlap. Is there a way to do so?
Also, when I remove the 'childForm.BringToFront()', for whatever reason it spawns behind the interface.
You should have to use Panel in which child form may appear. Simply add Panel from Toolbox and change its Dock property to top and place all your menu items in that Panel and also add new Panel i.e. MainPanel to display forms and than use following code to display form inside the Panel
SubForm objForm=new SubForm();
objForm.TopLevel = false;
MainPanel.Controls.Add(objForm);
objForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
objForm.Dock = DockStyle.Fill;
objForm.Show();

C# Form Size won't not set

i have a panel in a form and i load another form in this. this new loaded form have a picture box and the picture was larger as the main form. i want to scroll in the new form and show the picture. i set the size of the new form in code but in the breakpoint i can see the size show the right settings but if i show the settings in a message it show the false settings.
here the code
Form maleTree = new maleTreeForm(maleTreePanel);
maleTree.TopLevel = false;
maleTreePanel.Controls.Add(maleTree);
Form maletreeForm = maleTreePanel.Controls.Find("maleTreeForm", true).FirstOrDefault() as Form;
PictureBox pictureBox = maleTreePanel.Controls.Find("maletreePictureBox", true).FirstOrDefault() as PictureBox;
maletreeForm.Size = new Size(pictureBox.Width, pictureBox.Height);
MessageBox.Show(maletreeForm.Size.toString());
the picturebox.width show in breakpoint 2300, but in messagebox 1300.
i want to set the full 2300 how i can solve this ?
this is the result
you see in the right the scroll bar is not completly down but the image end

How to open form at toolstrip button position

I have toolstrip menu with some buttons, menu is located in toolstrip container. What I am trying to accomplish is to open new form exactly at specific toolstripbutton location... This is my code. It works unless I move toolstrip menu to bottom or right side of toolstrip container...
private System.Windows.Forms.ToolStripButton rbRunMacro;
private System.Windows.Forms.ToolStrip tsMacroRecorder;
private void rbRunMacro_Click(object sender, EventArgs e)
{
Rectangle rect = this.rbRunMacro.Bounds;
Point location = PointToScreen ( new Point(this.tsMacroRecorder.Location.X + rect.X, this.tsMacroRecorder.Location.Y + rect.Y));
MacroListForm form = new MacroListForm();
form.StartPosition = FormStartPosition.Manual;
form.Location = location ;
form.Show();
}
You should get the location of rbRunMacro relative to the entire screen (see link).
form.Location = this.tsMacroRecorder.PointToScreen(rbRunMacro.Bounds.Location);
The reason we use ToolStrip.PointToScreen is that ToolStripButton does not offer the PointToScreen method. Therefore, we have to use ToolStripButton.Location to get the location of rbRunMacro relative to its parent control (see link).

How to Load Form inside panel other form in win app

I Create a Windows Forms application with C#.
I have a general Form and a panel on it.
I show subForm into this panel with code:
SubForm objForm= SubForm.InstanceForm();
this.IsMdiContainer = true;
objForm.TopLevel = false;
pnlSubSystem.Controls.Add(objForm);
objForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
objForm.Dock = DockStyle.Fill;
objForm.Show();
now I want to show other form on subForm of this panel, But I dont know how to do it.
I think your problem resolved by this code:
SubForm objForm= SubForm.InstanceForm();
objForm.TopLevel = false;
pnlSubSystem.Controls.Add(objForm);
objForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
objForm.Dock = DockStyle.Fill;
objForm.Show();
As I understand, you're very close. To add another form into subform try the same code instead:
pnlSubSystem.Controls.Add(objForm);
use (where objForm2 is the new subForm)
SubForm objForm2 = new SubForm();
objForm.Controls.Add(objForm2);
Since you already got the answer that by removing this.IsMdiContainer = true; your code would run perfectly fine. Because IsMdiContainer property changes the display and behavior of the form to an MDI parent form. When this property is set to true, the form displays a submerged client area. All MDI child forms assigned to the parent form are displayed within its client area.
SubForm objForm= SubForm.InstanceForm();
objForm.TopLevel = false;
pnlSubSystem.Controls.Add(objForm);
objForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
objForm.Dock = DockStyle.Fill;
objForm.Show();
objForm form which will be the template for the child forms. Each time you want to create a new child window to your application, you can create a new instance of this template form and make the first form as its parent form.
//Create a new instance of the MDI child template form
SubForm objForm = new SubForm();
//Set parent form for the child window
objForm.MdiParent=this; // Last ObjForm or something
//Display the child window
objForm.Show();
Another way:
objForm.TopLevel = false;
objForm.Parent = pnlSubSystem;
objForm.Show();
This is my first answer on Stackoverflow.

auto resizing panel on a form

I have two panels on a form. I want one of them, when an user maximizes the form, the panel to be maximized too, depending on the form. I tried and the panel is shown on the entire form,hiding the other panel.
Here is my code:
public MainForm()
{
InitializeComponent();
panel2.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
panel2.Dock = DockStyle.Fill;
TaskControl t = new TaskControl();
int x, y;
x = 0;
y = 0;
t.Location = new Point(x, y);
panel2.Controls.Add(t);
t.BringToFront();
}
I managed to do it. I changed the values of Anchor from Properties. Thank you for help!
It sounds like you want a splitcontainer. With this control you get two panels. Set the "fixedpanel" property to the panel that you want to not resize. The other panel will resize as the form is resized (or maximised)
You should NOT only do so in initialization, but also in Form_Resize event etc. For instance,
you can handle Form.ResizeEnd event, see reference here.

Categories