Hi Guys,

Currently I have a form that load an XML file and populates Text Boxes with certian Values from that XML. I also have an 'Admin' form which accept new values. What I am wanting to do is the following:

When the Admin screen is loaded from a button on the main form, populated with new credentials and then the 'Apply Changes' button is pressed. The main form to 'refresh' or 're-load' the connection to the XML to pick up the new values.

Code for Main for Load:

{
    public partial class Form1 : Form
    {
        private XmlDocument smtpDetails;
        private XmlElement root;

        public string PATH = @"C:\********\Quickticket\Person.xml";


        public Form1()
        {
            InitializeComponent();
        }

        private void label2_Click(object sender, EventArgs e)
        {
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SplashScreen Splash = new SplashScreen();
            Splash.Show();

            textBox1.Enabled = false;
            compName.Enabled = false;
            comp_tel.Enabled = false;
            ccEmail.Visible = false;
            ccEmailLbl.Visible = false;

            smtpDetails = new XmlDocument();
            smtpDetails.Load(PATH);

any advice or sample could would be great.

Thanks in advance.

Tried Form1.Refresh() that might do the job

Hi Chris, Thanks for your reply.

YEah.. I've tried that but it does not to seem to refresh the connection to the XML. sigh

Form1.Refresh() Just redraws the form and its associated objects on screen.

You will need to rerun smtpDetails.Load(PATH); to reload the XML document.

This will be done in the code block that makes the admin form (dont have that in my inbox either so cant comment on the exact layout, it would go after the .Show() call or .ShowDialog()

Hi Mikey,

Yeah I guessed that it what needed to be done. Below is the Code Block for the Button on the Admin Form.

Code:

       private void srvAcceptChanges_Click(object sender, EventArgs e)
        {

            XmlTextWriter writer = new XmlTextWriter("C:\\Timewade\\Quickticket\\Person.xml", System.Text.Encoding.UTF8);
            writer.WriteStartDocument(true);
            writer.Formatting = Formatting.Indented;
            writer.Indentation = 2;
            createNode("1", "SMTP", "1000", writer);
            writer.WriteEndDocument();
            writer.Close();



            MessageBox.Show("Changes Applied");

            Close();
        }

Wheres the code block on the main form calling the admin one? (assuming there is one)

Hi Mikey,

Its only a Lable object that's called the Admin form. Below is the code.

Code

       private void label5_Click(object sender, EventArgs e)
        {
            About LF = new About();
            LF.Show();
        }

Ok.

Firstly, your admin form does not run modally (ie. the main form is still accessible while the admin form is open), is this the intention? Obviously not sure how the form works/is used for.

Secondly, irritatingly running the form non-modally means my idea for refreshing the XML wont work due to the continued execution of the thread, which is paused with a modal form.

Hi Mikey,

Ah right... well there's my lesson for today already! There is no reason why the main form has to be accessible whilst the admin form is open. It's just the way I did It I guess.

Is there an easy way to change the form to run in 'modal' method??

Use .ShowDialog() instead of .Show(). This will make the form behave similar to a messagebox in terms of nothing else apart from the form can be clicked until the form is closed.

.ShowDialog() also allows you to assign dialog responses to buttons on your form and then do logic based off your form response (used when making custom message boxes etc).

To reload the XML data you will need to rerun the entire of the method that loads the XML I think unless you literally just want to reload the XML file into memory. Which would be for example:

private void label5_Click(object sender, EventArgs e)
        {
            About LF = new About();
            LF.ShowDialog();

            //Written in the browser it may be slightly off
            if (LD.DialogResult == DialogResult.OK)
            {
                smtpDetails = new XmlDocument();
                smtpDetails.Load(PATH);
            }
        }

When running modal forms however you will need to assign the button return for the dialog, this is done using the property DialogResult of the button. The example code is based off of the confirmation button being set to a result of "Ok".

Hi Mikey,

I'll give that a try. Many thanks.

No worries, gimme a shout if you get stuck

Hi Mikey,

Yeah, That don't seem to be working. Maybe a silly question but should'nt the refresh command be on the Admin Control form when someone presses 'Accept Changes'? and not on the main form?

I dont believe so because the XML is being handles on the main form?

Hi Mikey,

Yes you are correct. The XML is loaded on the main form. but I though as the Admin control was on a seperate form. When you click 'Apply Changes' the code stub in that botton could then refresh/load the XML again..

I've attached a Screenshot for you to see..

Because the XML is loaded on the main form we will need to do the refreshing there. You will need to refresh and then reload all the data into the right places however as just reloading the file wont do that.

If it isnt working ensure youve set the dialog result values of the apply changes button to OK if using my code.

commented: great help! +2

Hi Mikey, Thats seems to have worked! thanks for that

I will mark this as resolved now. :)

You might be able to replt to a new thread i'm about to start.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.