Trials of the Novice Programmer - Part Two

How I code

 

I am coding almost exclusively in C# and my current projects will be using SQLite and DataGridViews

 

I am not interested in learning Windows Presentation Forms (WPF) so this will be strictly WinForms until I start in MONO.

 

One crutch I am using is a separate form with a ListBox on it for debugging and later it will be used for a running list when doing Transactions and Reconciling Statements. It is simply titled frmList.

 



The entire code at this point is not very complex.

using System;

using System.Data;

using System.IO;

using System.Windows.Forms;

 

namespace Family_Finances_SQLite

{

    public partial class frmList : Form

    {

        string myDirectory;

        public frmList()

        {

            InitializeComponent();

            string myName = Environment.UserName;

            myDirectory = @"C:\Users\" + myName + @"\Desktop\Visual Studio Work Zone\";

        }

 

        private string l_FontName = "Microsoft Sans Serif";

        private float l_FontSize = 10;

        private string m_Comment;

        private bool l_FontBold = false;

        private bool l_FontItalic = false;

 

        public string ListFontName

        {

            set

            {

                l_FontName = value;

                listBox1.Items.Add(l_FontName);

                setFont();

            }

            get

            {

                return listBox1.Font.Name;

            }

        }

 

        public float ListFontSize

        {

            set

            {

                l_FontSize = value;

                listBox1.Items.Add(l_FontSize);

                setFont();

            }

            get

            {

                return listBox1.Font.Size;

            }

        }

 

        public bool ListFontBold

        {

            set

            {

                l_FontBold = value;

                setFont();

            }

            get

            {

                return l_FontBold;

            }

        }

 

        public bool ListFontItalic

        {

            set

            {

                l_FontItalic = value;

                setFont();

            }

            get

            {

                return l_FontItalic;

            }

        }

 

        private void setFont()

        {

            if (l_FontBold == true && l_FontItalic == true) { listBox1.Font = new Font(l_FontName, l_FontSize, FontStyle.Italic | FontStyle.Bold); }

            else if (l_FontBold == true && l_FontItalic == false) { listBox1.Font = new Font(l_FontName, l_FontSize, FontStyle.Bold); }

            else if (l_FontBold == false && l_FontItalic == true) { listBox1.Font = new Font(l_FontName, l_FontSize, FontStyle.Italic); }

            else { listBox1.Font = new Font(l_FontName, l_FontSize, FontStyle.Regular); }

           

        }

       

        public string Comment

        {

            set

            {

                m_Comment = value;

                listBox1.Items.Add(m_Comment);

            }

            get { return m_Comment; }

        }

 

 

        private void btnClearList_Click(object sender, EventArgs e)

        {

            listBox1.Items.Clear();

        }

 

        private void btnSaveList_Click(object sender, EventArgs e)

        {

            foreach(string item in listBox1.Items)

            {

                appendLine(item, myDirectory + "The List.txt");

            }

        }

 

        private bool appendLine(string line2Write, string fileName)

 

        {

            StreamWriter tw;

            try

            {

                using (tw = File.AppendText(fileName))

                {

                    tw.WriteLine(line2Write);

                }

            }

            catch (Exception ex)

            {

                DialogResult result = MessageBox.Show("Unable to write to: " + fileName + "\r\n" + ex.ToString() + "\r\n OK to retry", "File Sysytem Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);

                if (result == DialogResult.Cancel)

                {

                    return false;

                }

            }

            return true;

        }

    }

}


The font code is for later as I anticipate wanting to change fonts when it is being used in the app.

 

In the Main form it is used as follows:

namespace Family_Finances_SQLite

{

    public partial class frmMain : Form

    {

        SQLiteConnection m_dbConnection;

        string dbgFile = "debug.txt";

        string myDirectory;

        string programName = "SQL Work Zone";

       frmList showMe = new frmList();   // This creates an instance of the List Form…

 

 

namespace Family_Finances_SQLite

{

    public partial class frmMain : Form

    {

        …

 

        public frmMain()

        {

            InitializeComponent();string myName = Environment.UserName;

            …

            showMe.Show(); // This Displays the Form

        }

 

I use it, at this time, primarily for collecting information to help me understand the effects of changes as I am learning to code.

 

As an example, showMe.Comment = "listFrom Type: " + "C"; Application.DoEvents();       // The Application.DoEvents() ensures that it is displayed in case the next code causes an error. Because an errors will hide the list form it’s a good idea to have two displays and move the form to where it won’t be covered by VS2019

 

There is one other facility I am using because I a use Lists a lot.

 

showList(tblSpec, "\n\rProcessing To:"); // Here I want to see what is in the tblSpec list before an error hides the information. Also I can save and printout the information from the list form.

 

Here’s the code:

        private void showList(List<string> listSrc, string title)

        {

            showMe.Comment = "\r\n" + title;

            int n = 0;

            foreach(string item in listSrc)

            {

                showMe.Comment = n.ToString() + "\t" + item;

                n++;

            }

            showMe.Comment = " ";

            Application.DoEvents();

        }

 

I am also displaying the list indexes so that if my code places the wrong data in an SQLite column I can figure out what I did.

I have found lists very useful, you can move a lot of data in and out of a function using lists.

If you are accessing list contents by index and an error occurs I have found that the system doesn’t show what was the contents of that index position, hence listing the contents before the error can be quite useful… If you Application.DoEvents() immediately after listing. And Visual Studio isn’t covering the form. J

 

Finally, there is something you can do before actually publishing your Solution, you can do a global search and replace for ShowMe à //ShowMe & ShowList à //ShowList  and then uncomment anything you still need.

Questions & Comments myrrhhouseof2[at]gmail.com

You will have to change the [at] to @

MUST Have a subject