Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, so I built a windows form application for a MEWS System, which is used in hospitals to monitor a patients health. So i've built the program and it works fine, now what I want to to do is store that data using XML File steamwriter and be able to read that data using the readwriter. The problem is I dont know how to utilize this in my code. Since im only familiar with console apps im not sure how I would go about using this in a form app.

Heres my code for the program so far:

C#
public partial class frmMews : Form
    {

        Random r = new Random();

        public frmMews()
        {
            InitializeComponent();
            lblResps.BackColor = System.Drawing.Color.Magenta;
            lblHeart.BackColor = System.Drawing.Color.Cyan;
            lblBlood.BackColor = System.Drawing.Color.Red;
            lblConscious.BackColor = System.Drawing.Color.Green;
            lblTemp.BackColor = System.Drawing.Color.Brown;
            lblUrine.BackColor = System.Drawing.Color.Yellow;

        }

        private void frmMews_Load(object sender, EventArgs e)
        {
            // Form Load Event

            tmrResps.Enabled = false;
            tmrResps.Interval = 300;  // milliseconds

            tmrHeart.Enabled = false;
            tmrHeart.Interval = 300;  // milliseconds

            tmrBlood.Enabled = false;
            tmrBlood.Interval = 300;  // milliseconds

            tmrConscious.Enabled = false;
            tmrConscious.Interval = 800;  // milliseconds

            tmrTemp.Enabled = false;
            tmrTemp.Interval = 600;  // milliseconds

            tmrUrine.Enabled = false;
            tmrUrine.Interval = 300;  // milliseconds


        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            // Start Button Event

            tmrResps.Enabled = true;
            tmrResps.Interval = 3000;  // milliseconds

            if (tmrResps.Enabled == true)
            {
                txtRespScore.Text = "1";
            }
            else
            {
               txtRespScore.Text = "3";
               MessageBox.Show("Alert! Patients Respiration Score is 3 or more, urgent medical attention is required");
            }

            //---

            tmrHeart.Enabled = true;
            tmrHeart.Interval = 4000;  // milliseconds

            if (tmrHeart.Enabled == true)
            {
                txtHeartScore.Text = "0";
            }
            else
            {
                txtHeartScore.Text = "3";
                MessageBox.Show("Alert! Patients Heart rate Score is 3 or more, urgent medical attention is required");
            }


            //---

            tmrBlood.Enabled = true;
            tmrBlood.Interval = 3000;  // milliseconds

            if (tmrBlood.Enabled == true)
            {
                txtBloodScore.Text = "1";
            }
            else
            {
                txtBloodScore.Text = "3";
                MessageBox.Show("Alert! Patients Blood level Score is 3 or more, urgent medical attention is required");
            }
            
            //---

            tmrConscious.Enabled = true;
            tmrConscious.Interval = 1000;  // milliseconds


            if (tmrConscious.Enabled == true)
            {
                txtConsciousScore.Text = "1";
            }
            else
            {
                txtConsciousScore.Text = "3";
                MessageBox.Show("Alert! Patients Conscious Score is 3 or more, urgent medical attention is required");
            }

            //----

            tmrTemp.Enabled = true;
            tmrTemp.Interval = 3000;  // milliseconds

            txtTempScore.Text = "2";

            if (tmrTemp.Enabled == true)
            {
                txtTempScore.Text = "2";
            }
            else
            {
                txtTempScore.Text = "3";

                MessageBox.Show("Alert! Patients Temperature Score is 3 or more, urgent medical attention is required");
            }

            //---

            tmrUrine.Enabled = true;
            tmrUrine.Interval = 2000;  // milliseconds

            if (tmrUrine.Enabled == true)
            {
                txtUrineScore.Text = "0";
            }
            else
            {
                txtUrineScore.Text = "3";

                MessageBox.Show("Alert! Patients Urine output Score is 3 or more, urgent medical attention is required");
            }

        }

        private void tmrResps_Tick(object sender, EventArgs e)
        {
            Random r = new Random();

            int x = ( (int)( (r.Next(0, 100) ) / 10) ) * 10;

            //---

            lblResps.Size = new System.Drawing.Size( x, 23);


        }

        private void tmrHeart_Tick(object sender, EventArgs e)
        {
            int x = ((int)((r.Next(0, 100)) / 10)) * 10;

            lblHeart.Size = new System.Drawing.Size(x, 23);

        }

        private void tmrBlood_Tick(object sender, EventArgs e)
        {
            int x = ((int)((r.Next(0, 100)) / 10)) * 10;

            lblBlood.Size = new System.Drawing.Size(x, 23);


        }

        private void tmrConscious_Tick(object sender, EventArgs e)
        {
            int x = ((int)((r.Next(0, 100)) / 10)) * 10;

            lblConscious.Size = new System.Drawing.Size(x, 23);
        }

        private void tmrTemp_Tick(object sender, EventArgs e)
        {
            int x = ((int)((r.Next(0, 100)) / 10)) * 10;

            lblTemp.Size = new System.Drawing.Size(x, 23);

        }

        private void tmrUrine_Tick(object sender, EventArgs e)
        {
            int x = ((int)((r.Next(0, 100)) / 10)) * 10;

            lblUrine.Size = new System.Drawing.Size(x, 23);

        }

        private void btnScore_Click(object sender, EventArgs e)
        {
            txtMewsScore.Text = "5";


            MessageBox.Show("Alert! Patients MEWS Score is 4 or more, urgent medical attention is required");


        }

        }
    }
Posted

1 solution

My best advice is using Data Contract and DataContractSerializer with StreamWriter and StreamReader or whatever stream-related class you may want.

This is the only unobtrusive way of serialization available in .NET (you can use v.3.5 or v.4.0). Also, it can save and restore any arbitrary object graph, not only object tree or single object. This way is also the best in terms of ease-of-use and supportability. For common-use purposes (and also for remoting, WCF) you can nearlt always consider all other ways as obsolete or inferior.

You simply add the attribute [DataContract] to every type involved in the model to be stored/restored; better always use your own name space Web URL through the Namespace attribute parameter (property). Then, apply attribute [DataMember] to every member you want to persist. These members does not even have to be public, or properties; members can be collections, arrays -- virtually no limitation. Apparently, these steps will not modify your class hierarchy, will not modify any behavior -- nothing.

Now, if you want to modify your classes, you should be careful to provide incremental behavior with old files, but only for those members which are part of contract (marked with [DataMember]). After all, if have a contract, you're only responsible for contract items. When you simple add more [DataMember] members, your old data remains usable; new members will automatically assume default values.

Finally, create you contact serializer based on your top-level object's type:

C#
DataContractSerializer ser =
                new DataContractSerializer(typeof(Top));
//...
Top of object graph to store in your stream:
Top top = //...
//...
ser.WriteObject(xmlWriter, top);
//..
Top newGraph = (Top)ser.ReadObject(xmlReader);


<code>ReadObject and WriteObject will work with XmlReader/XmlWriter, XmlDictionaryReader/XmlDictionaryWriter, respectively, and any Stream.

You can find pretty good documentation and samle code in Microsof help:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx and
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datamemberattribute.aspx.

Will it help you? Please try and ask further questions.

Thank you.
 
Share this answer
 
Comments
Espen Harlinn 26-Feb-11 10:55am    
Good reply - a 5
Sergey Alexandrovich Kryukov 26-Feb-11 20:12pm    
Thank you, also good enough to reuse.
--SA
Wendelius 29-Dec-11 17:56pm    
5+
Sergey Alexandrovich Kryukov 29-Dec-11 20:22pm    
Thank you, Mika.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900