Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6706309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:30:19+00:00 2026-05-26T07:30:19+00:00

I have one form calling a method in another form. but the method in

  • 0

I have one form calling a method in another form. but the method in the other form does not work properly.
Form2 calls main:

private void button1_Click(object sender, EventArgs e)
{
    main ma = new main();
    ma.AddType(txtName.Text,txtURL.Text,12);
    this.Close();
}

main: (Adds one row to the xml and reloads the datagrid from xml)

public void AddType(string name, string url, int interval)
{

    string path = Application.StartupPath + @"\sites.xml";
    //create new instance of XmlDocument
    XmlDocument doc = new XmlDocument();
    //load from file
    doc.Load(path);
    //create node and add value
    XmlNode node = doc.CreateNode(XmlNodeType.Element, "site", null);
    node.InnerXml = "<Name>"+name+"</Name><URL>"+url+"</URL><Status></Status><Response-Time></Response-Time><Last-Checked></Last-Checked>";
    //add to elements collection
    doc.DocumentElement.AppendChild(node);
    //save back
    doc.Save(path);
    bwLoadXML.RunWorkerAsync();
}

The bwLoadXML.RunWorkerAsync(); does not show the new xml in the datagrid for some reason.

Edit,
Here is the backgroundWorker:

/////////////////////////////////
        ////Populate Grid from XML
        /////////////////////////////////
        private void bwLoadXML_DoWork(object sender, DoWorkEventArgs e)
        {
            gridPopulate();
        }
        private void gridPopulate()
        {

            DataSet data = new DataSet(); string p = System.IO.Path.Combine(Application.StartupPath, "sites.xml");
            data.ReadXml(p);
            if (this.dataGrid.InvokeRequired)
            {
                this.dataGrid.Invoke(new MethodInvoker(delegate
                {
                    this.dataGrid.DataSource = data;
                    this.dataGrid.DataMember = "site";
                }));
            }
            else
            {
                this.dataGrid.DataSource = data;
                this.dataGrid.DataMember = "site";
            }
            int i = 0;
            foreach (DataGridViewColumn column in this.dataGrid.Columns)
            {
                if (i != 0)
                {
                    if (column.Name == "Name" || column.Name == "Status" || column.Name == "URL" || column.Name == "Response-Time" || column.Name == "Last-Checked")
                    {
                        //column.AutoSizeMode
                        column.Visible = true;
                        //column.Width = (int)(dataGrid.Width * .2) + (column.Name.Length / 2)-9;
                        /*if (column.Name == "URL")
                        {
                            ColumnHeader ch = new ColumnHeader();
                            //ch.
                        }*/
                    }
                    else
                    {
                        column.Visible = false;
                        //dataGrid.Columns[i+1].CellType = new DataGridViewButtonColumn();
                        //dataGrid.Columns[i+1].HeaderCell.
                    }
                }
                i++;
            }
            if (this.dataGrid.InvokeRequired)
            {
                this.dataGrid.Invoke(new MethodInvoker(delegate
                {
                    // If column 3 is the checkbox column, we sit it's resize mode to none:
                    dataGrid.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
                    // Then we set the width:
                    dataGrid.Columns[0].Width = 25;
                    dataGrid.Columns[0].DefaultCellStyle.Padding = System.Windows.Forms.Padding.Empty;
                    // If column 3 is the checkbox column, we sit it's resize mode to none:
                    dataGrid.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                    // Finally we set the rest of the grid to fill or what ever resizing you need:
                    dataGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                }));
            }
            else
            {
                // If column 3 is the checkbox column, we sit it's resize mode to none:
                dataGrid.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
                // Then we set the width:
                dataGrid.Columns[0].Width = 25;
                dataGrid.Columns[0].DefaultCellStyle.Padding = System.Windows.Forms.Padding.Empty;
                // If column 3 is the checkbox column, we sit it's resize mode to none:
                dataGrid.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                // Finally we set the rest of the grid to fill or what ever resizing you need:
                dataGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:30:20+00:00Added an answer on May 26, 2026 at 7:30 am

    Your problem is stemming from the fact that your button click method creates a new, separate Main form and does not interact with the one you expect. Since your new form is no longer referenced as soon as the button click is done, the background worker probably never gets a chance to start. You’ll need to set up and hold a reference to the main form to work with it.

    public class Form2 : ... {
      main ma;
    
      public Form2(main ma) {
        this.ma = ma;
      }
    
      private void Button1_Click(object sender, EventArgs e) {
        this.ma.AddType(txtName.Text, txtUrl.Text, 12);
        this.Close();
      }
    }
    

    And from the main form, when you create the second form and show it you’ll need to pass in the form it expects:

    void DoingSomething() {
      Form2 form = new Form2(this); // <-- this is where you pass in main
      form.ShowDialog();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have WPF Application where I have One main form and other user controls
Ok still learning here. If I have one form calling another form like below
I have a form being displayed by a static method. This static method calls
I'm sure this question has already been asked in one form or another, but
I have an application with one main form. In the form I have one
I have one form with several text fields and one button. When i enter
I have one form in a PHP (5.2.9-1) application that causes IIS (Microsoft-IIS/6.0) to
i have one form which have some input box and some select box. i
so here's the problem I have one form, it outputs search results after submit,
IDE: Microsoft Visual Studio Professional 2008 Language: C# Background: I have one form and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.