Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
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
 
Old August 12th, 2008, 04:23 AM
Authorized User
 
Join Date: Aug 2008
Posts: 38
Thanks: 1
Thanked 0 Times in 0 Posts
Default

is your suggestion similar to this...

//form2

private void RefreshContacts()
        {
            Form1 x = new Form1();
            x.listBox1.BeginUpdate();
            x.listBox1.Items.Add(new Kontacts(oContact.FirstName, oContact.LastName, m2()));
            x.listBox1.EndUpdate();
        }

//button event
  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();
                        RefreshContacts();
                     }
                    else
                    {
                        oContact.AddPicture("C:\\face.gif");
                        oContact.Save();
                        this.Hide();
                        Form1 form1 = (Form1)Application.OpenForms["Form1"];
                        form1.Show();
                        form1.Activate();

                        RefreshContacts();
                    }

                  }

                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);
            }

        }

private void Form2_Load(object sender, System.EventArgs e)
        {

            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;
            AddContact(strFirst, strLast, strJobTitle, strCompany, strAddress, strContacts, strEmailAdd);
            RefreshContacts();

        }
//form2 designer
   this.btnSave.Click += new System.EventHandler(this.btnSave_Click);

I tried running this but nothing changed...


 
Old August 12th, 2008, 04:33 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

Lets reiterate what the main goal here is.. We do not want Form2 to have ANY knowledge of Form1, since Form1 is the host form. Therefore, there should be no references at all to Form1 in Form2.

All Form2 should do is raise the event.

In Form1 you need the code that refreshes the list of contacts. This means Form1 is completely managing itself, and simply refreshing content when it is informed of changes by the system (i.e. the event that Form2 raises).

I hope this makes the end goal clearer for you. I strongly suggest you step back and stop trying to "hack" it by moving bits of code around. Understand what the code is doing before touching it again..

Rob
http://cantgrokwontgrok.blogspot.com
 
Old August 12th, 2008, 07:42 PM
Authorized User
 
Join Date: Aug 2008
Posts: 38
Thanks: 1
Thanked 0 Times in 0 Posts
Default

can you post some related articles...like mdi applications similar to this..

 
Old August 12th, 2008, 09:47 PM
Authorized User
 
Join Date: Aug 2008
Posts: 38
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I solved the problem...I read more topics on event handling and delegates...and i realized what you said...i called an event handler and called RefreshContacts method with Items.Clear command before it reloaded the contact pictures...

private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                //this.Hide();
                outlookBar1.Hide();

                Form2 x = new Form2();
                x.btnSave.Click += new System.EventHandler(RefreshContacts);



Thanks a lot! and more power!

 
Old August 12th, 2008, 11:55 PM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

Happy to be of help :)

You hit the nail on the head there, sometimes the problem can be so simple, provided you understand the mechanics of whats going on..

Its always good to make sure you understand the basics perfectly (loops, switch, events, delegates, try/catch etc).

Good luck, if you need anything else, please ask :)

Rob
http://cantgrokwontgrok.blogspot.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Altering a listbox from another form Firestorm9 C# 2005 1 July 9th, 2007 06:12 AM
How to Update a Listbox on Different Form boxwalah C# 2 February 24th, 2006 12:22 PM
Refresh form while updating linked form mfletch Access 0 February 26th, 2004 10:32 PM
form / listbox question nkrukovsky Access VBA 2 November 4th, 2003 09:16 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.