How to access methos of one form from another?
-
For example I'm buiding Find/Search function in new form and I want to be able to search for sting in parent form from this child form (child form have text box where user enter word he is looking for)? Parent form have multi Rich Text Box(es) (in tabs)... menus etc So this is probably simple I tried few things but none works... I guess I'm just doing something wrong. Thanks :)
-
For example I'm buiding Find/Search function in new form and I want to be able to search for sting in parent form from this child form (child form have text box where user enter word he is looking for)? Parent form have multi Rich Text Box(es) (in tabs)... menus etc So this is probably simple I tried few things but none works... I guess I'm just doing something wrong. Thanks :)
If you have a Form (say mainForm) containing text, and you want an interactive search facility, you could create a dialog using a new Form (say findDialog) which has mainForm as parent (so it hides when mainForm hides), and which is shown as modeless dialog (i.e. it can give up focus to other windows in same app). Now mainForm can have button event handlers attached to the buttons of findDialog, read an editTextBox on findDialog, and do whatever is required within mainForm. So the trick is in mainForm attaching event handlers to another form's buttons, but then it did create that other form (findDialog) so it has a reference to it. Hence it boils down to one of the following: - make the Controls on findDialog public (is not what Visual Studio Designer does by default, but there is a "Modifiers" field on the properties panel) - add public properties to get access to the non-public controls you need - add one or more public methods to findDialog to attach an event handler to the non-public controls you need. The choice is yours. Personaly I often create dialogs programmatically (rather than through Designer) and make all controls public. :) -- modified at 14:05 Saturday 6th January, 2007
Luc Pattyn
-
For example I'm buiding Find/Search function in new form and I want to be able to search for sting in parent form from this child form (child form have text box where user enter word he is looking for)? Parent form have multi Rich Text Box(es) (in tabs)... menus etc So this is probably simple I tried few things but none works... I guess I'm just doing something wrong. Thanks :)
Event Listeners are another option. Personally I usually use Dialogs, which has already been said.
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage
-
If you have a Form (say mainForm) containing text, and you want an interactive search facility, you could create a dialog using a new Form (say findDialog) which has mainForm as parent (so it hides when mainForm hides), and which is shown as modeless dialog (i.e. it can give up focus to other windows in same app). Now mainForm can have button event handlers attached to the buttons of findDialog, read an editTextBox on findDialog, and do whatever is required within mainForm. So the trick is in mainForm attaching event handlers to another form's buttons, but then it did create that other form (findDialog) so it has a reference to it. Hence it boils down to one of the following: - make the Controls on findDialog public (is not what Visual Studio Designer does by default, but there is a "Modifiers" field on the properties panel) - add public properties to get access to the non-public controls you need - add one or more public methods to findDialog to attach an event handler to the non-public controls you need. The choice is yours. Personaly I often create dialogs programmatically (rather than through Designer) and make all controls public. :) -- modified at 14:05 Saturday 6th January, 2007
Luc Pattyn
OK I like idea, I did change properties to buttons and textbox in child to public, so I can now access them within main form, but how do I add event handler to button from child form in main form? Sorry for trivial question but I'm new in this. Also does anyone have idea why I can't access richtextbox in main form from child form it's labeled as public also? I can for example access text box in child form from main form, but it's of no use, I need to attach that event so it does that on button click not before or after.
-
OK I like idea, I did change properties to buttons and textbox in child to public, so I can now access them within main form, but how do I add event handler to button from child form in main form? Sorry for trivial question but I'm new in this. Also does anyone have idea why I can't access richtextbox in main form from child form it's labeled as public also? I can for example access text box in child form from main form, but it's of no use, I need to attach that event so it does that on button click not before or after.
No no, the simplest way is the other way around, it is the main form that does everything about the text, including the searching, the findDialog's purpose is to specify and trigger the search, not to perform it. And the architecture would be something like this (incomplete!):
public class mainForm : Form {
FindDialog findDialog; // reference to the find dialog public mainForm() { // constructor // ... } // this could be called by clicking a "Open Find" button on mainForm public void btnOpenFind\_Click(object sender, EventArgs e) { findDialog=new FindDialog(); findDialog.btnFind.Click+=new EventHandler(btnFind\_Click); findDialog.btnClose.Click+=new EventHandler(btnClose\_Click); findDialog.ShowDialog(); } private void btnFind\_Click(object sender, EventArgs e) { // read findDialog.tbSearch.Text // and perform search in the text inside mainForm } private void btnClose\_Click(object sender, EventArgs e) { // do postprocessing if needed (e.g. preserve current settings), // then findDialog.Close(); }
}
public class FindDialog : Form {
public Button btnFind; // will start the search
public Button btnClose; // closes the dialog
public TextBox tbSearch; // holds what to search
}Hope this helps you way forward.
Luc Pattyn
-
No no, the simplest way is the other way around, it is the main form that does everything about the text, including the searching, the findDialog's purpose is to specify and trigger the search, not to perform it. And the architecture would be something like this (incomplete!):
public class mainForm : Form {
FindDialog findDialog; // reference to the find dialog public mainForm() { // constructor // ... } // this could be called by clicking a "Open Find" button on mainForm public void btnOpenFind\_Click(object sender, EventArgs e) { findDialog=new FindDialog(); findDialog.btnFind.Click+=new EventHandler(btnFind\_Click); findDialog.btnClose.Click+=new EventHandler(btnClose\_Click); findDialog.ShowDialog(); } private void btnFind\_Click(object sender, EventArgs e) { // read findDialog.tbSearch.Text // and perform search in the text inside mainForm } private void btnClose\_Click(object sender, EventArgs e) { // do postprocessing if needed (e.g. preserve current settings), // then findDialog.Close(); }
}
public class FindDialog : Form {
public Button btnFind; // will start the search
public Button btnClose; // closes the dialog
public TextBox tbSearch; // holds what to search
}Hope this helps you way forward.
Luc Pattyn
It works thanks, can someone explain what we did here (I understand basic), so we can add event to current object to instance in another object?