accessing form controls from the class file - c#

I have one mdi form and many child forms all the child form contains panel control named as panel1 now i want to access panel1 control from my class file for that i write the following code but it is not working.
Class1.cs file :
public partial class FormBase : Form
{
public FormBase()
{
this.Load += new System.EventHandler(this.FormLoad);
//this.Resize += new System.EventHandler(this.FormResize);//error
this.panel1.Resize += new System.EventHandler(this.FormResize);//error as a panel1 is not access
}
protected virtual void FormLoad(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
}
protected virtual void FormResize(object sender, EventArgs e)
{
//error : panel1 control has a red underline
panel1.Left = (this.ClientSize.Width - panel1.Width) / 2;//error as a panel1 is not access
panel1.Top = (this.ClientSize.Height - panel1.Height) / 2;//error as a panel1 is not access
}
}
now my one of the child form
frmAddNewEmployee.cs
public partial class frmAddNewEmployee : FormBase
{
public frmAddNewEmployee()
{ InitializeComponent();
}
protected override void FormLoad(object sender, EventArgs e)
{
base.FormLoad(sender, e);
}
protected override void FormResize(object sender, EventArgs e)
{
base.FormResize(((Panel)sender).Name, e);//error
base.FormLoad(sender, e);//error
}
}
so my question is how to access this panel1 control of the child forms from my Class1.cs file

Related

how to use textbox content from a form to a usercontrol?

i have a form with a panel in it and when a button is presed a usercontrol showes and the panel hides, now i need to hide a button if the textbox in the form1 contains "admin" in it.
this is the form1 code
public partial class Form1 : Form
{
public string a;
public Form1()
{
InitializeComponent();
a = textBox1.Text;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
afterlogin v = new afterlogin();
v.Dock = DockStyle.Fill;
panel1.Controls.Add(v);
v.BringToFront();
}
}
and this is the usercontrol code
public partial class afterlogin : UserControl
{
public afterlogin()
{
InitializeComponent();
Form1 f = new Form1();
if (f.a.Contains("admin"))
{
button1.Hide();
}
}
}
You're creating a new form in the user control, it will not have the same values as the original form you created your user control in.
If you wish to take in values from the form, add a constructor parameter to the "afterlogin" class with text of the textbox, such as:
public afterlogin(string text)
{
InitializeComponent();
if (text.Contains("admin"))
{
button1.Hide();
}
}
and pass the text value to the constructor of the "afterLogin" class:
afterlogin v = new afterlogin(a);
Since Form1 creates the UserControl, just have the Form itself turn on or off the Button?
You can make a method in your UserControl that allows you to change the visibility of the control:
public partial class afterlogin : UserControl
{
public void setButton(bool state)
{
button1.Visible = state;
}
}
Now you can call setButton when you create the UserControl:
private void button1_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
afterlogin v = new afterlogin();
v.Dock = DockStyle.Fill;
panel1.Controls.Add(v);
v.BringToFront();
v.setButton(!textBox1.Text.Contains("admin"));
}

Show form as a ToolTip

I need to display a Form as a ToolTip of an UserControl. When the mouse is over the UserControl, the Form have to be show, and when the mouse leave that UserControl, the Form have to be hide.
I've overrided these events, in my UserControl class :
public partial class TreatedMetricsDisplay : UserControl
{
private TreatedMetricsWindow _treatedMetricsWindow;
public TreatedMetricsDisplay()
{
InitializeComponent();
_treatedMetricsWindow = new TreatedMetricsWindow ();
}
protected override void OnMouseHover(EventArgs e)
{
base.OnMouseHover (e);
this._treatedMetricsWindow.Show();
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave (e);
this._treatedMetricsWindow.Close ();
}
}
No exceptions, but events are not called.
Showing the Form will steal the Focus, It will get activated. So, it isn't a good idea to show Form as ToolTip. It will not behave as one would expect.
You need to use ToolStripDropDown combined with ToolStripControlHost. Which makes it possible to show any control as tooltip(not exactly).
public partial class MainForm : Form
{
private ToolStripDropDown dropDown = new ToolStripDropDown();
public MainForm()
{
InitializeComponent();
dropDown.Items.Add(new ToolStripControlHost(new ToolTipUserControl() { Size = new Size(200, 200) }));
}
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
dropDown.Show(MousePosition);
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
dropDown.Hide();
}
}
ToolTipUserControl could be any Control which you want to show as tooltip.
Here is a code sample that might help:
private void ToolTipControl_Load(object sender, EventArgs e)
{
AttachHandlers(this);
}
private void AttachHandlers(Control currentControl)
{
foreach (Control control in currentControl.Controls)
{
control.MouseHover += GenericMouseHover;
control.MouseLeave += GenericMouseLeave;
if (control.Controls.Count != 0)
{
AttachHandlers(control);
}
}
}
void GenericMouseLeave(object sender, EventArgs e)
{
// no need to hide it if there was no form created in first place
if(_form != null && _form.Visible)
Form.Hide();
}
private void GenericMouseHover(object sender, EventArgs e)
{
Form.Location = this.PointToClient(Cursor.Position);
Form.Show();
}
ToolTipForm _form;
private ToolTipForm Form
{
get
{
if (_form == null)
{
_form = new ToolTipForm();
}
return _form;
}
}

Custom controls and event handling?

I have a windows custom control with a picturebox and a button on it's default panel. This windows custom control will be added to a windows form. There is another windows form called form2. when the user double clicks on the custom control, it should load form2. in designer when i double clicked the custom control on the form, it creates a load() event for that custom control. But i need double click event, how this can be done?
here is the graphical view of what happens
Here is code in the control
[DefaultEvent("DoubleClick")]
public partial class cntrlImageLoader : UserControl
{
public cntrlImageLoader()
{
InitializeComponent();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = Environment.SpecialFolder.MyPictures.ToString();
if (ofd.ShowDialog() == DialogResult.OK)
{
pbImage.Image = Image.FromFile(ofd.FileName);
}
}
private void pbImage_Click(object sender, EventArgs e)
{
this.cntrlImageLoader_DoubleClick(pbImage, e);
}
private void cntrlImageLoader_Load(object sender, EventArgs e)
{
}
private void cntrlImageLoader_DoubleClick(object sender, EventArgs e)
{
}
}
Here is the calling code on form1
private void cntrImLdrFront_DoubleClick(object sender, EventArgs e)
{
//this.cntrImLdrFront.pbImage.DoubleClick += new EventHandler(pbImage_DoubleClick);
}
FrmImageViewer f; // this is form2
private void pbImage_DoubleClick(object sender, EventArgs e)
{
f= new FrmImageViewer();
f.MdiParent = this.MdiParent;
f.Show();
}
Assign the attribute [DefaultEvent("DoubleClick")] at the class declaration.
[DefaultEvent("DoubleClick")]
public partial class MyControl : UserControl
{
}
This will creates event which you have set by default on double click of control at design time where you have placed your user control.
EDITED:
[DefaultEvent("LoadPicture")]
public partial class cntrlImageLoader : UserControl
{
public delegate void LoadPictureEventHandler(object sender, LoadPictureEventArgs e);
public event LoadPictureEventHandler LoadPicture;
private void pbImage_DoubleClick(object sender, EventArgs e)
{
if (LoadPicture != null)
{
LoadPictureEventArgs ev = new LoadPictureEventArgs();
LoadPicture(this, ev);
if (ev.Picture != null)
{
pbImage.Image = ev.Picture;
}
}
}
}
Create another class and give that class name to LoadPictureEventArgs
public class LoadPictureEventArgs : EventArgs
{
public Image Picture {get; set;}
public LoadPictureEventArgs(Image _picture)
{
Picture = _picture
}
public LoadPictureEventArgs()
: base()
{
}
}
HOW TO USE IT?
//FORM1
private void cntrImLdrFron_LoadPicture(object sender, LoadPictureEventArgs e)
{
Image img = null;
//LOAD YOUR IMAGE HERE
e.Picture = img;
}

How to reuse event

I am creating one c# project. In this project I have one mdi form and many child forms. All the child forms contains one panel named as panel1.
Now when child form opens i use the following code in all child form
all child forms' load event contains the following line.
this.WindowState = FormWindowState.Maximized;
and all child forms' resize event contains the following line.
panel1.Left = (this.ClientSize.Width - panel1.Width) / 2;
panel1.Top = (this.ClientSize.Height - panel1.Height) / 2;
so my question is if possible that the above code i write only once so i donot write this code in all the child forms load and resize event.
Yes it is possible. You have to pull up common functionality into base class event handlers, and then invoke them from child ones:
public partial class BaseForm : Form
{
public BaseForm()
{
InitializeComponent();
this.Load += new System.EventHandler(this.FormLoad);
this.Resize += new System.EventHandler(this.FormResize);
}
protected virtual void FormLoad(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
}
protected virtual void FormResize(object sender, EventArgs e)
{
panel1.Left = (this.ClientSize.Width - panel1.Width) / 2;
panel1.Top = (this.ClientSize.Height - panel1.Height) / 2;
}
...
}
public class DerivedForm : BaseForm
{
protected override void FormLoad(object sender, EventArgs e)
{
base.FormLoad(sender, e);
// specific code goes here
}
protected override void FormResize(object sender, EventArgs e)
{
base.FormResize(sender, e);
// specific code goes here
}
...
}
Create a base class and derive every child class from it.
Like this:
public class FormBase : Form
{
public FormBase()
{
this.WindowState = FormWindowState.Maximized;
// put more generic code here
}
... // or here
}
public class YourMdiChild : FormBase
{
... // all code specific for that child
}
I think the best way is to do it before opening the child form!
public class ChildForm : Form
{
public void doTheSizing()
{
// make it maximize...
// your sizing of pannel...
// etc...
}
}
public partial class ParentForm : Form
{
public ParentForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ChildForm childForm01 = new ChildForm();
childForm01.doTheSizing();
// now show the child window using Show() or ShowDialog().
childForm01.ShowDialog();
}
}

Show/hide, BringToFront/SendToBack a panel on Parent form when a Child Form on the MDI Parent Form closes or appears

I need to hide a panel on the Parent Form when a Child Form on an MDI Parent Form closes & show back the panel on the Parent form when the Child Form is closed.
Currently am using SendtoBack() to show the Child Form infront of the Panel which is on the Parent Form , but when i close the Child Form, then the Panel doesn't appears back, even if i use :
BringtoFront()
OR
Panel1.Visible=true
public partial class CHILD : Form
{
private void CHILD_Load(object sender, EventArgs e)
{
this.FormClosed += new FormClosedEventHandler(CHILD_FormClosed);
}
void CHILD_FormClosed(object sender, FormClosedEventArgs e)
{
PARENTForm P = new PARENTForm();
P.panel1.BringToFront();
P.panel1.Visible = true;
}
}
public partial class Form1 : Form
{
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
CHILD P = new CHILD();
P.Showg();
P.MdiParent = this;
P.BringToFront();
panel1.SendToBack();
panel1.Visible = false;
}
}
THIS ISN'T WORKING....PLEASE HELP..!
You creating new parent form in child form. You need to pass parent form object to child form and then use it to show/hide panel and set panel Modifiers property to public.
For example...
Parent form:
public partial class ParentForm : Form
{
public ParentForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
panel1.Visible = false;
ChildForm childForm = new ChildForm();
childForm.MdiParent = this;
childForm.Show();
}
}
Child form:
public partial class ChildForm : Form
{
public ChildForm()
{
InitializeComponent();
}
private void Child_FormClosed(object sender, FormClosedEventArgs e)
{
ParentForm parentForm = (ParentForm)this.MdiParent;
parentForm.panel1.Visible = true;
}
}
Use this Code
in Parent Form
private void MainMenu_ChildForm_Click(object sender, EventArgs e)
{
ChildForm frm = new ChildForm();
frm.MdiParent = this;
ShowHideMainPanel(frm);
frm.Show();
}
void ShowHideMainPanel(Form frm)
{
frm.FormClosed += new FormClosedEventHandler(Form_Closed);
panel1.Visible = false;
}
void Form_Closed(object sender, FormClosedEventArgs e)
{
panel1.Visible = true;
}
by assigning close event for child to show panel after its closing

Categories

Resources