 |
| C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

August 11th, 2008, 03:52 AM
|
|
Authorized User
|
|
Join Date: Aug 2008
Posts: 38
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
updating listbox content from a sub form
I am new to C#...I am creating a program that has 2 forms...Inside form1, there's a panel, which has form2...I want data in form2 to be displayed in listbox which is in form1...when i click the form2's save button....form2 in panel will hide....then the one running in the backdrop is form1 with the listbox...how can i update the data in listbox in form1,without using Form1 x=new Form1(); x.Show(); (or how can i activate form1 with updated listbox)...Thanks in advance...
|
|

August 11th, 2008, 04:32 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Hi there,
OK I am struggling to picture what you actually have here, since you are saying that you have Form1, which has a Panel control, which contains Form2. But then later say that you are instantiating new copies of Form1? (Implying it is a seperate Window?)
I would recommend that if you want to group the controls that are on Form2 together, then create a UserControl. This will enable you to treat the group of controls as an individual control (you will need to add a small amount of code to raise events from the control (such as the "Save" event you mentioned.
You would then have Form1, which contains a UserControl, which is composed of the Contents of Form2 (lets call it "ctlForm2".
You can then control the display of these by:
Code:
ctlForm2.Visible = false;
As well as hook up to events like:
Code:
ctlForm2.Save += new EventHandler(ctlForm2_Click);
To do things like:
Code:
private void ctlForm2_Click(object sender, EventArgs e)
{
ctlForm2.Visible = false;
}
I hope this helps point you in the right direction..
Rob
http://cantgrokwontgrok.blogspot.com
|
|

August 11th, 2008, 11:47 PM
|
|
Authorized User
|
|
Join Date: Aug 2008
Posts: 38
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
What i want to achieve is to have a form inside a form...form1 is the main form...form2 is in a panel which is inside form1...but whenever i close form2...nothing seems to happen with the control in form1(listbox)...this listbox should have been updated with data coming from form2...I want this idea because i don't want to see 2 separate forms in my program...Everytime i close the inner form (form2) i want some data to reflect in a listbox which in the outer form(form1), but when i click save or close nothing is updated in the listbox.. .Thanks again...
|
|

August 12th, 2008, 12:05 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
|
|
So we are talking about a MDI application (where there is a host form that can contain one of more child forms, like MS Word, Photoshop, whatever)?
Or, you can have the second form not shown in the task bar? You need to be more clear about how you are implementing the "form in a form".
In either case, you could add a handler to the Close event for the second form, when that event fires, you can then do the processing on the main form.
I personally would actually raise a event from the business logic layer to say "Data Updated" and then have all the forms automatically refresh.
I.e.
Form 2 "Save" Clicked > Sends Data to BLL to Save > BLL Sends Data to DAL to Store > Data Stored Confirmation to BLL > BLL Raises Event "Saved" > Form1 and Form2 Event Handlers Run Refreshing their Data.
Hope this leads you in the right direction..
Rob
http://cantgrokwontgrok.blogspot.com
|
|

August 12th, 2008, 01:07 AM
|
|
Authorized User
|
|
Join Date: Aug 2008
Posts: 38
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Sorry for so many questions...
Yes, this is about MDI application...my MainForm(form1) has 2 controls listbox and a panel in that panel i can call a usercontrol or a secondary form(form2)...but since i don't want to create another floating form i decided to use a panel...Whatever data i input in form2 is saved well..i can actually retrieve those data in other means..but my only problem is that when i close form2 some data are not loaded to the listbox...i tried creating handler (save) that will update the listbox like BeginUpdate() and EndUpdate()..and i also tried Refresh method to refresh the MainForm but none seemed to work...Thanks again and again..
|
|

August 12th, 2008, 02:00 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Hey, no problem, this is a forum, its about questions, answers and discussion ;)
Can you paste some snippets of the code you have? I personally vote that the event-driven model is the way to go, provides a lot more flexibility.. I am sure some of the other great guys on here would be happy to offer their input if they thought otherwise!
Lets have a look at what you are doing and see if we can help from there :)
Rob
http://cantgrokwontgrok.blogspot.com
|
|

August 12th, 2008, 02:23 AM
|
|
Authorized User
|
|
Join Date: Aug 2008
Posts: 38
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
MainForm(Form1)
//MainForms initial state..All outlook contact pictures are placed in //a listbox
private void Form1_Load(object sender, EventArgs e)
{
// oApp = new Outlook.ApplicationClass();
oNs = oApp.GetNamespace("MAPI");
contacts = oNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFo lderContacts);
try
{
for (i = 1; i < contacts.Items.Count + 1; i++)
{
// Get a contact
string x = " ";
contact = (Outlook.ContactItem)contacts.Items[i];
if ((contact.FirstName != null) && (contact.FirstName.Trim().Length > 0) && contact.HasPicture)
{
string fn = contact.FirstName;
string ln = contact.LastName;
x = GetContactPicturePath(contact, System.IO.Path.GetTempPath());
listBox1.Items.Add(new Kontacts(fn, ln, @x));
CleanupContactPictures(System.IO.Path.GetTempPath( ));
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//this one will place form2 in a panel once add menu item is clicked
private void addToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
outlookBar1.Hide();
Form2 x = new Form2();
x.TopLevel = false;
x.Focus();
x.BringToFront();
panel1.Controls.Add(x);
x.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
// this one in form2
private void btnSave_Click(object sender, EventArgs e)
{
try
{
if (!AccessContacts(txtFirst.Text.Trim(), txtLast.Text.Trim()))
{
strFirst = txtFirst.Text + " ";
strLast = txtLast.Text;
string strJobTitle = txtJobTitle.Text;
string strCompany = txtCompany.Text;
string strAddress = txtCompany.Text;
string strContacts = txtContacts.Text;
string strEmailAdd = txtEmailAdd.Text;
lblName.Text = strFirst + strLast;
lblNumber.Text = strContacts;
oContact.FirstName = txtFirst.Text;
oContact.LastName = txtLast.Text;
oContact.JobTitle = txtJobTitle.Text;
oContact.CompanyName = txtCompany.Text;
oContact.BusinessAddress = txtAddress.Text;
oContact.MobileTelephoneNumber = txtContacts.Text;
oContact.Email1Address = txtEmailAdd.Text;
oContact.FileAs = oContact.LastName + ", " + oContact.FirstName;
if (oContact.HasPicture == false)
{
string file = m2();
oContact.AddPicture(file);
oContact.Save();
this.Hide(); //it will hide form2
Form1 form1 = (Form1)Application.OpenForms["Form1"];
form1.Show(); //Problem on this area..I //can't issue a command that will refresh or repopulate the listbox
form1.Activate();// //in MainForm...i tried //BeginUpdate, refresh, etc..Nothing happend..
}
}
else
{
MessageBox.Show("Name already exists");
txtFirst.Enabled = false;
txtLast.Enabled = false;
txtJobTitle.Enabled = false;
txtContacts.Enabled = false;
txtAddress.Enabled = false;
txtCompany.Enabled = false;
txtContacts.Enabled = false;
txtEmailAdd.Enabled = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
|
|

August 12th, 2008, 02:30 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Ah ha, yes thanks for posting that helps bring things in to context a bit more..
The quickest (albeit not so elegant) solution here would be to:
- Add an event at form level to Form2 called "ContactsSaved" for instance.
- When you instantiate Form2 from within Form1, add the event handler (Form2.ContactsSaved += new EventHandler etc. etc.)
- In the event handler you have just defined, add the code to refresh the contacts list (I recommend you extract the code in the Load handler and place it in its own method, call it once on Load, and once again in the event handler).
I hope this helps, if any is unclear then please ask.
Also, on a side note, I recommend you rename your forms :) e.g. Form1 = "ContactsList" Form2 = "EditContacts" or something ;)
Rob
http://cantgrokwontgrok.blogspot.com
|
|

August 12th, 2008, 02:47 AM
|
|
Authorized User
|
|
Join Date: Aug 2008
Posts: 38
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
I'm quite confused with this statement..
- In the event handler you have just defined, add the code to refresh the contacts list (I recommend you extract the code in the Load handler and place it in its own method, call it once on Load, and once again in the event handler).
|
|

August 12th, 2008, 02:53 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Sorry for the confusion!
- Take the code that is in the Load handler that refreshes the contacts list on Form1. Put that in a new mathod called "RefreshContactsList" or something.
- In the event handler that you create for the ContactsSaved event (e.g. Form2_ContactsSaved) place a call that the RefreshContactsList method.
This will then end up something like:
Code:
Form_Load
{
// Your code here..
// Replace the contacts list code with
RefreshContactsList();
}
// other code
void Form2_ContactsSaved(object sender, EventArgs e)
{
RefreshContactsList();
}
Does that make things clear for you?
Rob
http://cantgrokwontgrok.blogspot.com
|
|
 |