C# Parent - child forms - c#

I'm having a main window (MainWindow.cs) form which contains a panel (pnlChild), I've created a new control to add my second form (Settings.cs) + controls to the panel:
Settings settings = new Settings() { Dock = DockStyle.Fill, TopLevel = false };
settings.FormBorderStyle = FormBorderStyle.None;
pnlChild.Controls.Add(settings);
settings.Show();
This is working fine, I have the same in my settings form, which also contains a panel "pnlChild", a child form (Appearance.cs) which contains "Appearance"
Settings.Appearance appearance = new Settings.Appearance() { Dock = DockStyle.Fill, TopLevel = false };
appearance.FormBorderStyle = FormBorderStyle.None;
pnlChild.Controls.Add(appearance);
appearance.Show();
Question: How can I change a text/update text to the "Settings" and/or "Main" form, when I press a button in the appearance child form?

Here is a workaround that maybe you can refer to. In this demo, I add textboxes named tbMain and tbSettings in MainWindows and Settings.
First, define a property in MainWindow.cs to access tbMain.
public TextBox TBM
{
get { return tbMain; }
set { tbMain = value; }
}
And define a property in Settings.cs to access tbSettings.
public TextBox TBS
{
get { return tbSettings; }
set { tbSettings = value; }
}
Then we can use Application.OpenForms Property to get the MainWindows and Settings instance. And modify the textbox value through the properties defined before.
private void btInAppearance_Click(object sender, EventArgs e)
{
Settings settings = (Settings)Application.OpenForms["Settings"];
settings.TBS.Text = "TextInSettings";
MainWindow main = (MainWindow)Application.OpenForms["MainWindow"];
main.TBM.Text = "TextInMainWindow";
}

Related

How to programmatically add a Custom Control to a Form and show it?

I'm trying to add a Label to a Windows Form by using another class programmatically. My Label does not appear inside the Form.
I don't know where I'm going wrong.
private void Form1_Load(object sender, EventArgs e)
{
Ticker ticker = new Ticker("ASDF");
ticker.display();
}
public class Ticker : Label
{
string labelText;
Label label = new Label();
public Ticker(string _labelText)
{
labelText = _labelText;
}
public void display()
{
label.Text = labelText;
Controls.Add(label);
}
}
You can make a few changes to your Ticker Custom Control:
You don't need to create a new Label inside your Custom Control: your Control is already a Label, use the this reference to set its properties (see also this keyword (C# Reference)).
The Text is the current Label's Text (this.Text). Store it if you need a copy of it for other reasons (custom painting, usually, so sometimes you need to clear the Text).
Controls is referring to the current class object: it's a Control, so it has a Controls property, which gets the ControlCollection of the child Controls of a Control.
You need to also specify a Point that defines the position of your Custom Control inside its Parent's ClientRectangle.
Even if it's not always required, add a parameter-less Constructor to your Custom Control: if/when it's actually needed, you'll have it already there.
If you don't want to set the Parent Control from the outside, as usual (e.g., var label = new Label(); this.Controls.Add(label);), you need to pass the reference of the Control which will become the Parent Control of your custom Label.
You can the use this reference - a Control type of reference - and add your Label to the Controls collection of the Control reference you receive:
// You want to store a reference to this Control if you need it later...
private Ticker ticker = null;
private void Form1_Load(object sender, EventArgs e)
{
//... or just declare it with: var ticker = new Ticker() if you don't
ticker = new Ticker("The Label's Text");
// [this] of course refers the current class object, Form1
ticker.Display(this, new Point(100, 100));
// Or, display the Label inside a Panel, child of Form1
// Note: if you don't comment the next line, the Label will be moved to panel1
ticker.Display(this.panel1, new Point(10, 50));
}
Here, I'm overloading the Display() method, so it accepts both a Parent reference and a Point value, used to position the Control inside its Parent's Client Area.
The Custom Label also calls BringToFront() on itself, to avoid showing up under some other, already existing, child Control of the new Parent.
public class Ticker : Label
{
public Ticker() : this("ticker") { }
public Ticker(string labelText) => this.Text = labelText;
public void Display(Control parent) => Display(parent, Point.Empty);
public void Display(Control parent, Point position)
{
this.Location = position;
parent.Controls.Add(this);
this.BringToFront();
}
}

How do I add form on tab control in c#

I have a form page with text boxes and data grid view and other forms that contain a tab control. I want to add the first form tab in the second form. I tried to write the code for the form to appear but it is larger than the tab container and doesn't fit. Only half of the form appears.
This is my code:
private void tcMainPage_SelectedIndexChanged(object sender, EventArgs e)
{
if (tcMainPage.SelectedIndex == 0)
{
GTOWN.PrintingPage BookInfo = new PrintingPage();
BookInfo.TopLevel = false;
BookInfo.FormBorderStyle = FormBorderStyle.None;
BookInfo.Dock = DockStyle.Fill;
tpSearch.Controls.Add(BookInfo);
BookInfo.Show();
}
}
this is the form
and that is what appears
Set your main FORM as a Container.
yourForm.IsMdiContainer = true;
Then add the child form to the tabPage:
private void tcMainPage_SelectedIndexChanged(object sender, EventArgs e)
{
if (tcMainPage.SelectedIndex == 0)
{
PrintingPage newFrm = new PrintingPage
{
MdiParent = this,
// This set the form parent as the tabClicked
Parent = tcMainPage.TabPages[0]
};
newFrm.Show();
}
}
my tab form work good in the same code
thank you all my code was correct but the problem was in tab property i deleted the tab and add another one and the code is working now
thank you
I face this issue and I create this if may help
public void addform(TabPage tp, Form f)
{
f.TopLevel = false;
//no border if needed
f.FormBorderStyle = FormBorderStyle.None;
f.AutoScaleMode = AutoScaleMode.Dpi;
if (!tp.Controls.Contains(f))
{
tp.Controls.Add(f);
f.Dock = DockStyle.Fill;
f.Show();
Refresh();
}
Refresh();
}
Forms are top-most objects and cannot be placed inside of other containers.
You may want to refactor your code so that the items on your Form are on a UserControl instead. At that point you can then add that UserControl to both a Form and a TabControl
public UserControl myControl(){ /* copy your current view code here */}
public Form myForm(){
Controls.Add(new myControl());
}
public Form myTabbedForm(){
var tabControl = new TabControl();
var page1 = new TabPage();
page1.Controls.Add(new myControl());
tabControl.TabPages.Add(page1);
this.Controls.Add(tabControl);
}

Group related controls on ToolStrip so they are always visible together

I have an application that has a tool strip with related controls set apart by ToolStripSeparaters. It looks something like this:
However, when the window size shrinks, some of the controls get moved to a little drop-down section. Unfortunately, this can split related controls, for example in the screenshot below, the "Filter by ID" label, the associated textbox for the ID, and "Clear Filter" button are no longer shown together.
If controls have to be moved to the drop-down, I'd prefer to have related controls move together. Is there a way to group related controls together on a ToolStrip? Or perhaps a better way of dealing with this sort of scenario?
I tried using the LayoutCompleted event move all of the controls to the overflow area if any of them are in the overflow.
private void toolStrip1_LayoutCompleted(object sender, EventArgs e)
{
var filterGroup = new List<ToolStripItem> { lblFilter, txtFilter, btnClearFilter };
if (filterGroup.Any(x => x.IsOnOverflow))
{
filterGroup.ForEach(x => x.Overflow = ToolStripItemOverflow.Always);
}
}
This seems to work fine, but I haven't found a good way to show them again when the window size is increased. I tried both the Resize and Layout events of the ToolStrip with the following code:
var filterGroup = new List<ToolStripItem> { lblFilter, txtFilter, btnClearFilter };
filterGroup.ForEach(x => x.Overflow = ToolStripItemOverflow.AsNeeded);
You could use a ToolStripControlHost to group a winforms TextBox and Label. E.g.
public class ToolStripLabelTextBox : ToolStripControlHost {
public Label Label { get; private set; }
public TextBox TextBox { get; private set; }
public ToolStripLabelTextBox(String labelText) : base(new FlowLayoutPanel { FlowDirection = FlowDirection.LeftToRight, WrapContents = false, AutoSize = true, AutoSizeMode = AutoSizeMode.GrowAndShrink, Padding = Padding.Empty, Margin = Padding.Empty }) {
Label = new Label { Text = labelText, AutoSize = true, Anchor = AnchorStyles.Top | AnchorStyles.Bottom, TextAlign = System.Drawing.ContentAlignment.MiddleCenter };
TextBox = new TextBox();
FlowLayoutPanel panel = (FlowLayoutPanel) Control;
panel.Controls.Add(Label);
panel.Controls.Add(TextBox);
}
}
Two other options are:
Implement a LayoutEngine that does the grouping you want.
Implement a composite ToolStripItem that displays a label and text box. You can use the ToolStripRadioButtonMenuItem as an example: https://msdn.microsoft.com/en-us/library/vstudio/ms404318%28v=vs.100%29.aspx

Controls added to FlowLayoutPanel using code don't have correct spacing

I have a simple form as an example. I've added some check boxes using designer, and set the margin and padding to 0,0,0,0. This is the form code:
public partial class Frm1 : Form
{
public Frm1()
{
InitializeComponent();
AddCheckBox();
AddCheckBox();
AddCheckBox();
}
public void AddCheckBox()
{
CheckBox cb = new CheckBox();
cb.Text = "AddedFromCode";
cb.Padding = new Padding(0,0,0,0);
cb.Margin = new Padding(0,0,0,0);
flowLayoutPanel1.Controls.Add(cb);
}
}
I would expect the check boxes that are added from designer and from code to appear the same, however, they do not.
How can I get the spacing of the AddedFromCode controls to have 0 spacing?
I don't know why it took me so long to figure this out. One of the properties that designer changes when you add a control to a FlowLayoutPanel is AutoSize = true.
So in code...
cb.AutoSize= true;
solves the problem.

Light box style Model dialog in C# windows form

I want to show jquery lightbox style model dialog in c# windows form. My dialog box will appear from the MDI child form button's Click event.How I do that ? Please help me.
If what you mean is to mask the parent form, then there is no simple way to do this afaik, what I've done is create a transparent form on top of the parent and then open another custom form as a dialog on top of the fake mask window.
something like this, where window is your popup and owner the form that is going to be masked:
Mask = new LayerWindow();
Mask.Show(owner);
window.Show(Mask);
this is what I'm using as a mask:
public class LayerWindow : Form
{
public LayerWindow()
{
FormBorderStyle = FormBorderStyle.None;
StartPosition = FormStartPosition.Manual;
TransparencyKey = Color.Fuchsia;
base.BackColor = Color.Black;
Opacity = 0.50;
ShowInTaskbar = false;
}
public void Show(Control parent)
{
if (parent == null)
throw new ApplicationException("No parent provided");
var container = parent.FindForm();
if (container == null)
throw new ApplicationException("No parent Form found. Make sure that the control is contained in a form before showing a popup.");
Location = PointToScreen(container.Location);
Bounds = container.Bounds;
Owner = container;
Owner.Enabled = false;
base.Show(container);
}
public void Unmask()
{
if (Owner != null)
Owner.Enabled = true;
Hide();
}

Categories