Hayd3n

Hey,

I'm just learning C# (using Microsoft Visual C# 2005 Express Edition) and I'm having a bit of trouble with Windows Forms.

Here is what I want to happen.
  1. [frmClientManager] displays xml data in an Listview control
  2. A button on [frmClientManager] opens a new [frmClientEditor] form
  3. a) [frmClientEditor] adds data to the xml Database
    b) [frmClientEditor] refreshes the [frmClientManager] form by running a public function [frmClientManager.Read()]
    c) [frmClientEditor] closes itself

It might be easier to understand from code.

FORM: [frmClientEditor]

private frmClientManager otherForm;
private void btnSaveClient_Click(object sender, EventArgs e)
{
Save();
otherForm.Read();

this.Close();
}


FORM: [frmClientManager]

private void frmClientManager_Load(object sender, EventArgs e)
{
Read();
}
private void btnNewClient_Click(object sender, EventArgs e)
{
NewClient();
}
public void Read()
{
// Code for Reading/Refreshing the xml Database
}
void NewClient()
{
frmClientEditor frmClientEditor = new frmClientEditor();
frmClientEditor.NewClient();
frmClientEditor.Show();
}


So, as I understand it, I need to be able to define the existing parent form [frmClientManager] from a child form [frmClientEditor].

Last year, while attempting a similar project in Visual Basic .NET 2003, I was able to achieve this using "Friend Events".

EQUIVALENT OF: [frmClientEditor]

Friend Event MyRefreshEvent(ByVal sender As System.Object, ByVal e As System.EventArgs)
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
RaiseEvent MyRefreshEvent(Me, New System.EventArgs)
xmlDoc.Save(xmlFile)
RaiseEvent MyRefreshEvent(Me, New System.EventArgs)

Dim frmContacts As New frmContacts
RemoveHandler MyRefreshEvent, AddressOf frmContacts.refreshData_Handler
End Sub


EQUIVALENT OF: [frmClientManager]

Public Sub refreshData_Handler(ByVal sender As System.Object, ByVal e As System.EventArgs)
refreshData()
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim frmAddContact As New frmAddContact
AddHandler frmAddContact.MyRefreshEvent, AddressOf Me.refreshData_Handler
frmAddContact.Show()
End Sub


And on the side, I've got a few niggling random questions:
  • How do you create wizards for your programs, ie: Backup Data Wizard
  • My XML database was created in the solutions explorer, so the actual file sits in the Solution Directory. When my program is deployed, where does it go and how does my application know where to find it Does it sit in C:/Program Files/My App/database.xml
  • Is there a way to get an editable textbox into a ListView Control

Hope thats enough information! All help is much appreciated, Thanks Guys.


Cheers,


Hayden


Re: Visual C# Express Edition Multiple Forms: Running a Function in the Main Form from the Child Form

anubisascends

It sounds like you should be able to add an event handler to your formClientEditor.

On your formClientEditor, set the save button's modifier to public.

Then in the formClientManger add this method:

Code Block

void formClienEditor_SaveClick(Object sender, EventArgs e)

{

Read();

}

Now in the NewClient method, add this after the Show() method:

Code Block
formClientEditor.btnSaveClient.Click += new EventHandler(formClienEditor_SaveClick);

Once this is done, after the save button is clicked, the program will run the save method in the formClientEditor, then run the saveClick method that you created.

Hope this helps.