Before I used MDI and it worked fine, I could show my ListForm in MainForm. Now that I don't want to use MDI, it didn't work.
Before, with Mdi:
public partial class Le_MainForm : DevExpress.XtraEditors.XtraForm
{
public Le_MainForm()
{
InitializeComponent();
this.IsMdiContainer = true;
this.Name = "MainUSER";
if (Program.IsFA) barButtonItem_OrdList.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
Liste_Ordres f_Liste = new Liste_Ordres();
f_Liste.MdiParent = this;
f_Liste.Show();
}
private void barButtonItem_ListeOrdres_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Close_AllForm();
Liste_Ordres f_Liste = new Liste_Ordres();
f_Liste.MdiParent = this;
f_Liste.Show();
}
private void barButtonItem_CreatOrdreAller_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Close_AllForm();
Program.AllerRetour = "Ordre Aller";
Fiche_Ordre f_Fiche = new Fiche_Ordre();
f_Fiche.MdiParent = this;
f_Fiche.Show();
}
Now, after I eliminated the Mdi //this.IsMdiContainer = true;
and all Forms inherited from MainForm:
public partial class Liste_Ordres : Le_MainForm
{
.....
I cannot show my ListeForm in MainFrom
public partial class Le_MainForm : DevExpress.XtraEditors.XtraForm
{
public Le_MainForm()
{
InitializeComponent();
//this.IsMdiContainer = true;
this.Name = "MainUSER";
if (Program.IsFA) barButtonItem_OrdList.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
Liste_Ordres f_Liste = new Liste_Ordres();
// f_Liste.MdiParent = this;
f_Liste.Show();
}
Someone has any idea ?
If you want your MainForm to be something like a master page, you can use only the MainForm and design all other masks not as Forms but as Controls you put on that MainForm.
Okay, I think I figured a way to open an inheriting form on Initialization.
First of all, in my MainForm I created an integer outside of any functions:
private int a = 1;
Then for my mainform I created a protected virtual on_Load event:
protected virtual void Le_MainForm_Load(object sender, EventArgs e)
{
if (a == 1)
{
Liste_Ordres frm = new Liste_Ordres();
frm.Show();
a = 0;
}
}
Next in my inheriting form, I overrided the on_Load event:
protected override void Form1_Load(object sender, EventArgs e)
{
}
That at least got both forms open without using Mdi (though in a rather round about way), but now another problem remains:
When both forms open, MainForm opens in front of inheriting (no matter if you try using functions BringToFront() and SendToBack()).
Perhaps when I figure that problem out (If I do), then I will edit this answer, but for now this is the end.
Hope this works!
Related
This is my form:
namespace Secretary_1._0
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
This is my class:
namespace Secretary_1._0
{
public partial class Client
{
public static Form1 formCall = new Form1();
public static void Clients_Click(object sender, EventArgs e)
{
formCall.clientPanel.Visible = true;
formCall.clientLabel.Visible = true;
formCall.addClientButton.Visible = true;
formCall.clientListPanel.Visible = true;
formCall.clientListPanel.BringToFront();
formCall.addClientLabel.Visible = false;
formCall.clientInfoPanel.Visible = false;
}
public static void addClientButton_Click(object sender, EventArgs e)
{
formCall.clientPanel.Visible = true;
formCall.addClientLabel.Visible = true;
formCall.clientInfoPanel.Visible = true;
formCall.clientInfoPanel.BringToFront();
formCall.addClientButton.Visible = false;
formCall.clientListPanel.Visible = false;
formCall.clientAddPropertyPanel.Visible = false;
}
}
}
Edited:
My question is how do I call the Button_Click Event from the Client Class?
When the Client button is clicked in Form1 I want to call the events located in the Client Class.
Is this possible? Am I missing something? Ive searched every where but I seem not to understand.
I plan to create a lot of buttons and would like to create a class for certain buttons so that my form1.cs isnt so long.
Any help would be appreciated. Thanks in advance.
Just do this
//Set Access Modifier of that button to public or internal for same namespace
namespace Secretary_1._0
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void StartClient()
{
var client = new Client(this);
client.RequiredMethod(); //Call here method of client
}
}
}
Now
namespace Secretary_1._0
{
public partial class Client
{
public Form1 formCall;
//Constructor
public Client(Form1 form1)
{
formCall = form1;
formCall.someButton.Click += OnSomeButtonClick;
}
public void OnSomeButtonClick(object sender, EventArgs e)
{
//Code here to on form1 button click ...
}
public static void Clients_Click(object sender, EventArgs e)
{
formCall.clientPanel.Visible = true;
formCall.clientLabel.Visible = true;
formCall.addClientButton.Visible = true;
formCall.clientListPanel.Visible = true;
formCall.clientListPanel.BringToFront();
formCall.addClientLabel.Visible = false;
formCall.clientInfoPanel.Visible = false;
}
public static void addClientButton_Click(object sender, EventArgs e)
{
formCall.clientPanel.Visible = true;
formCall.addClientLabel.Visible = true;
formCall.clientInfoPanel.Visible = true;
formCall.clientInfoPanel.BringToFront();
formCall.addClientButton.Visible = false;
formCall.clientListPanel.Visible = false;
formCall.clientAddPropertyPanel.Visible = false;
}
}
}
Edit: Follow this post to resolve issue using event.
1-open Form1.Designer.Cs
2-search about your button generated code
3-the last line in button code must be like that
this.btn_insert.Click += new System.EventHandler(this.button1_Click);
Note : button1 = your button name
4-Replace The last line i just mentioned above and add this one instead
this.btn_insert.Click += new System.EventHandler(this.Clients_Click);
Hope This will Help You :)
i have to mention this process mean passing a method to the delegate(EventHandler) Which necessary to the event to work correctly.
I assume you are using Visual Studio. Go into the solution explorer. Click to expand Form1.cs. Find the Form1 class in the tree and expand it. Click into one of your components there to cause VS to open the code for your Form1.designer.cs. At the bottom of that page should be a list of your components. All of them are private. Change the ones you need from private to internal, and you will have access to them from other classes in your program. Be warned, though, I think you might get some undocumented behavior doing this. But it will let you get at those forms objects from another class in the program.
I've got a problem with subscribing from a form to an event in an user control.
MainForm-Code:
public partial class mainForm : Form
{
public mainForm()
{
InitializeComponent();
UserControl menuView = new mnlib.mnlibControl();
newWindow(menuView);
}
public void newWindow(UserControl control)
{
this.mainPanel.Controls.Clear();
this.mainPanel.Controls.Add(control);
}
mnlibControl.OnLearnClick += new EventHandler(ButtonClick); //Error in this line
protected void ButtonClick(object sender, EventArgs e)
{
//handling..
}
}
UserControl-Code:
public partial class mnlibControl : UserControl
{
public mnlibControl()
{
InitializeComponent();
}
private void btn_beenden_Click(object sender, EventArgs e)
{
Application.Exit();
}
public event EventHandler LearnClick;
private void btn_lernen_Click(object sender, EventArgs e)
{
if (this.LearnClick != null)
this.LearnClick(this, e);
}
}
Now, visual studio marks the "mnlibControl.OnLearnClick ..." line as wrong. "mnlibControl" would not be found, maybe a missing using directive etc. .
All this code and both forms are located in the same project file.
I tried around and googled like hell but just can't find a solution for my problem.
In the UserControl form there is a button - when it's clicket it shall trigger the newWindow method in the mainForm and open up another window.
My source for this solution of my problem is: How do I make an Event in the Usercontrol and Have it Handeled in the Main Form?
There is no OnLearnClick in your component. You need to subscribe to LearnClick. You also need to subscribe in function block. You also should use concrete type (mnlib.mnlibControl), not UserControl:
public mainForm()
{
InitializeComponent();
mnlib.mnlibControl menuView = new mnlib.mnlibControl();
menuView.LearnClick += new EventHandler(ButtonClick);
newWindow(menuView);
}
Your code mnlibControl.OnLearnClick += new EventHandler(ButtonClick); must be within any of functional block (i.e. method, property, ...).
You have to place this line inside an actual method:
mnlibControl.LearnClick += new EventHandler(ButtonClick);
Like this:
public mainForm()
{
InitializeComponent();
UserControl menuView = new mnlib.mnlibControl();
newWindow(menuView);
mnlibControl.OnLearnClick += new EventHandler(ButtonClick);
}
I have a child that pops up to display data.
but when the data changes a new form is created to display the new data.
I want to close the old form, so i don't end up with 5000 forms every time the data changes.
The reason a new form is created is so that in its name the data's ID can be shown.
My Code:
String Pass; // used to get value from class and pass it to next form.
public void ShowNewCompareDiff() //object sender, EventArgs e
{
FormCompareDiff childForm = new FormCompareDiff(Pass);
childForm.MdiParent = MdiParent;
childForm.Text = "Comepare difference ";
//childForm.Close(); //Not working
//childForm = null; //Not working
childForm.Show();
}
private void dataGridViewResult_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
CompareXML Com = new CompareXML();
Pass = Com.Compare(richTextBoxSQL.Text, richTextBoxPrevSQL.Text);
ShowNewCompareDiff();
}
Child form FormCompareDiff:
namespace AuditIT_1
{
public partial class FormCompareDiff : Form
{
String Passed;
public FormCompareDiff(String Pass)
{
Passed = Pass;
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Location = new System.Drawing.Point(836, 0); // Form Shows next to FormSchemaSearch
InitializeComponent();
}
private void FormCompareDiff_Load(object sender, EventArgs e)
{
String Pass = Passed;
CompareXML Com = new CompareXML();
webBrowserDifferences.DocumentText = Com.ResultShow(Pass);
}
}
}
You could convert childForm to a member variable of your class, and then alter your ShowNewCompareDiff method to something like this:
FormCompareDiff childForm;
public void ShowNewCompareDiff()
{
if (childForm != null)
childForm.Dispose(); // Get rid of old form, if exists
childForm = new FormCompareDiff(Pass);
childForm.MdiParent = MdiParent;
...
}
EDIT:
Complete Example
Create a new WindowsFormApplication, add a new Button to the Form, add the following code:
public partial class Form1 : Form
{
Form MyForm;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (MyForm != null)
MyForm.Dispose();
MyForm = new Form() { Text = DateTime.Now.ToString() };
MyForm.Show();
}
}
Test it. You'll see how it works.
You'll want to explicitly keep track of the life of the child form. And use a property instead of a constructor argument so you can update the child instead of having to create a new one. Like this:
FormCompareDiff childForm;
public void ShowNewCompareDiff()
{
if (childForm != null) {
childForm.WindowState = FormWindowState.Normal;
}
else {
childForm = new FormCompareDiff();
childForm.MdiParent = MdiParent;
childForm.FormClosed += delegate { childForm = null; };
}
childForm.Pass = Pass;
childForm.Show();
}
Don't forget to add a public Pass property to FormCompareDiff.
on your ShowNewCompareDiff() method ones you create FormCompareDiff form object keep it as property on the main form, then you can update Text of the same form every time data changes, if that property with child from is null create new one and assign back to it.
EDIT
public void ShowNewCompareDiff(Form formToClose)
{
FormCompareDiff childForm = new FormCompareDiff(Pass);
childForm.MdiParent = MdiParent;
childForm.Text = "Comepare difference ";
childForm.Show();
formToClose.Close(); // close the form you want
}
call this method as
ShowNewCompareDiff(this);
I Made an application. The Main form Name is Form1.
And the other Form is called PoP.
public partial class pops : Form
{
public pops()
{
InitializeComponent();
CenterToScreen();
}
private void pops_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Close();
}
private void lblAdminNo_Click(object sender, EventArgs e)
{
}
}
Make two public properties on popup form and retrieve them from parent form.
string username = string.Empty;
string password = string.Empty;
using (LoginForm form = new LoginForm ())
{
DialogResult result = form.ShowDialog();
if (result == DialogResult.Ok)
{
username = form.Username;
password = form.Password;
}
}
It all depends on from where are you calling the Pop form.
If it is called from the Form1 itself, then the Popform's object itself would provide you the value.
Pop popFrm = new Pop();
if(popFrm.ShowDialog() == Ok)
{
string userName = popFrm.TextBox1.Text;
}
If the Pop is invoked from a different area/part of application, you may have to store it somewhere common to both the forms.
This can be done through events. This approach is particularly useful when data to be posted even when the child form is kept open.
The technique is- From parent form, subscribe to a child from event. Fire the event when child form closes, to send data
----- SAMPLE CODE-----
Note: In the Parent Form add a Button:button1
namespace WindowsFormsApplication2
{
public delegate void PopSaveClickedHandler(String text);
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Pops p = new Pops();
p.PopSaveClicked += new PopSaveClickedHandler(p_PopSaveClicked);//Subscribe
p.ShowDialog();
}
void p_PopSaveClicked(string text)
{
this.Text = text;//you have the value in parent form now, use it appropriately here.
}
}
Note: In the Pops Form add a TextBox:txtUserName and a Button:btnSave
namespace WindowsFormsApplication2
{
public partial class Pops : Form
{
public event PopSaveClickedHandler PopSaveClicked;
public Pops()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
if(PopSaveClicked!=null)
{
this.PopSaveClicked(txtUserName.Text);
}
}
}
}
Summary:
1.Add a delegate(place where it available to both parent and child form) :
public delegate void PopSaveClickedHandler(String text);
2.In form:Pops, Add an event:
public event PopSaveClickedHandler PopSaveClicked;
3.Subscribe to the event in Parent Form:
p.PopSaveClicked += new PopSaveClickedHandler(p_PopSaveClicked);
4.Invoke the event in form:Pops Save Button Click
if(PopSaveClicked!=null)
{
this.PopSaveClicked(txtUserName.Text);
}
You can send data to the form object before you display it. Create a method to call, send the info through the constructor... etc.
I have two Form classes, one of which has a ListBox. I need a setter for the SelectedIndex property of the ListBox, which I want to call from the second Form.
At the moment I am doing the following:
Form 1
public int MyListBoxSelectedIndex
{
set { lsbMyList.SelectedIndex = value; }
}
Form 2
private ControlForm mainForm; // form 1
public AddNewObjForm()
{
InitializeComponent();
mainForm = new ControlForm();
}
public void SomeMethod()
{
mainForm.MyListBoxSelectedIndex = -1;
}
Is this the best way to do this?
Making them Singleton is not a completely bad idea, but personally I would not prefer to do it that way. I'd rather pass the reference of one to another form. Here's an example.
Form1 triggers Form2 to open. Form2 has overloaded constructor which takes calling form as argument and provides its reference to Form2 members. This solves the communication problem. For example I've exposed Label Property as public in Form1 which is modified in Form2.
With this approach you can do communication in different ways.
Download Link for Sample Project
//Your Form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.Show();
}
public string LabelText
{
get { return Lbl.Text; }
set { Lbl.Text = value; }
}
}
//Your Form2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private Form1 mainForm = null;
public Form2(Form callingForm)
{
mainForm = callingForm as Form1;
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.mainForm.LabelText = txtMessage.Text;
}
}
(source: ruchitsurati.net)
(source: ruchitsurati.net)
Access the form's controls like this:
formname.controls[Index]
You can cast as appropriate control type, Example:
DataGridView dgv = (DataGridView) formname.Controls[Index];
I usually use the Singleton Design Pattern for something like this http://en.wikipedia.org/wiki/Singleton_pattern . I'll make the main form that the application is running under the singleton, and then create accessors to forms and controls I want to touch in other areas. The other forms can then either get a pointer to the control they want to modify, or the data in the main part of the application they wish to change.
Another approach is to setup events on the different forms for communicating, and use the main form as a hub of sorts to pass the event messages from one form to another within the application.
It's easy, first you can access the other form like this:
(let's say your other form is Form2)
//in Form 1
Form2 F2 = new Form2();
foreach (Control c in F2.Controls)
if(c.Name == "TextBox1")
c.Text = "hello from Form1";
That's it, you just write in TextBox1 in Form2 from Form1.
If ChildForm wants to access the ParentForm
Pass ParentForm instance to the ChildForm constructor.
public partial class ParentForm: Form
{
public ParentForm()
{
InitializeComponent();
}
public string ParentProperty{get;set;}
private void CreateChild()
{
var childForm = new ChildForm(this);
childForm.Show();
}
}
public partial class ChildForm : Form
{
private ParentForm parentForm;
public ChildForm(ParentForm parent)
{
InitializeComponent();
parentForm = parent;
parentForm.ParentProperty = "Value from Child";
}
}
There is one more way, in case you don't want to loop through "ALL" controls like Joe Dabones suggested.
Make a function in Form2 and call it from Form1.
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void SetIndex(int value)
{
lsbMyList.SelectedIndex = value;
}
}
public partial class Form1 : Form
{
public Form2 frm;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
frm=new Form2();
frm.Show();
}
private void button1_Click(object sender, EventArgs e)
{
frm.SetIndex(Int.Parse(textBox1.Text));
}
}
Here's also another example that does "Find and Highlight". There's a second form (a modal) that opens and contains a textbox to enter some text and then our program finds and highlights the searched text in the RichTextBox (in the calling form). In order to select the RichTextBox element in the calling form, we can use the .Controls.OfType<T>() method:
private void findHltBtn_Click(object sender, EventArgs e)
{
var StrBox = _callingForm.Controls.OfType<RichTextBox>().First(ctrl => ctrl.Name == "richTextBox1");
StrBox.SelectionBackColor = Color.White;
var SearchStr = findTxtBox.Text;
int SearchStrLoc = StrBox.Find(SearchStr);
StrBox.Select(SearchStrLoc, SearchStr.Length);
StrBox.SelectionBackColor = Color.Yellow;
}
Also in the same class (modal's form), to access the calling form use the technique mentioned in the #CuiousGeek's answer:
public partial class FindHltModalForm : Form
{
private Form2 _callingForm = null;
public FindHltModalForm()
{
InitializeComponent();
}
public FindHltModalForm(Form2 CallingForm)
{
_callingForm = CallingForm;
InitializeComponent();
}
//...