C# coding modify code to toggle between two objects and iterate thru ten.

Currently toggling between two displays, need to be able to go through ten. The textboxGraphics are 1-10 respectivley on the form now and these 10 display textboxGraphix values need to be able to loop. The full source code is below.

            if (m_strDisplayName == textBoxGraphic2.Text)
                m_strDisplayName = textBoxGraphic1.Text;
            else
                m_strDisplayName = textBoxGraphic2.Text;

Open in new window


Actual Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace GraphicDriver
{
    public partial class Form1 : Form
    {
        private string m_strDisplayName = "";
        private int MaxWatchdogTimer = 10000;
        private int m_WatchdogTimeRemaining = 0;

        public Form1()
        {
            InitializeComponent();
            timer1.Tick += new EventHandler(timer1_Tick);

            RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"Software\Lin and Associates\LA_ADVGraphics", true);
            string strDone = "1";
            try
            {
                strDone = rk.GetValue("MenuTrigger_done").ToString();
            }
            catch
            {
                rk.SetValue("MenuTrigger_done", "1");
            }
            rk.Close();

            buttonStart.Enabled = true;
            buttonStop.Enabled = false;
            buttonInvoke.Enabled = true;
        }

        void timer1_Tick(object sender, EventArgs e)
        {
            labelMessage.Text = "";
            RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"Software\Lin and Associates\LA_ADVGraphics", true);
            if (checkBoxDone.Checked)
            {
                if (rk.GetValue("MenuTrigger_done").ToString() == "0")
                {
                    labelMessage.Text = "MenuTrigger_done is 0";
                    rk.Close();
                    return;
                }
            }
            if (m_strDisplayName == textBoxGraphic2.Text)
                m_strDisplayName = textBoxGraphic1.Text;
            else
                m_strDisplayName = textBoxGraphic2.Text;
            //G.g_Tracer.Trace(10, "Writing to registry MenuDisplay: " + sDisplayName);
            rk.SetValue("MenuDisplay", m_strDisplayName);
            //G.g_Tracer.Trace(10, "Writing to registry MenuScreenLocation: " + sWindowName);
            rk.SetValue("MenuScreenLocation", "1");
            string strTrigger = "0";
            try
            {
                strTrigger = rk.GetValue("MenuTrigger").ToString();
                if (strTrigger == "0")
                    strTrigger = "1";
                else
                    strTrigger = "0";
            }
            catch
            {
            }
            //G.g_Tracer.Trace(10, "Writing to registry MenuDisplayInvokeTime: " + DateTime.Now.ToString());
            rk.SetValue("MenuDisplayInvokeTime", DateTime.Now.ToString());
            //G.g_Tracer.Trace(10, "Writing to registry MenuTrigger: " + strTrigger);
            if (checkBoxDone.Checked)
            {
                rk.SetValue("MenuTrigger_done", "0");
            }
            rk.SetValue("MenuTrigger", strTrigger);

            rk.Close();
            labelMessage.Text = "Invoke display " + m_strDisplayName;
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"Software\Lin and Associates\LA_ADVGraphics", true);
            rk.SetValue("MenuTrigger_done", "1");
            rk.Close();
            timer1.Interval = int.Parse(textBoxTimer.Text);
            timer1.Enabled = true;

            buttonStart.Enabled = false;
            buttonStop.Enabled = true;
            buttonInvoke.Enabled = false;

        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            buttonStart.Enabled = true;
            buttonStop.Enabled = false;
            buttonInvoke.Enabled = true;
        }

        private void buttonInvoke_Click(object sender, EventArgs e)
        {
            labelMessage.Text = "";
            RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"Software\Lin and Associates\LA_ADVGraphics", true);
            if (checkBoxDone.Checked)
            {
                if (rk.GetValue("MenuTrigger_done").ToString() == "0")
                {
                    labelMessage.Text = "MenuTrigger_done is 0";
                    rk.Close();
                    return;
                }
            }
            if (m_strDisplayName == textBoxGraphic2.Text)
                m_strDisplayName = textBoxGraphic1.Text;
            else
                m_strDisplayName = textBoxGraphic2.Text;
            //G.g_Tracer.Trace(10, "Writing to registry MenuDisplay: " + sDisplayName);
            rk.SetValue("MenuDisplay", m_strDisplayName);
            //G.g_Tracer.Trace(10, "Writing to registry MenuScreenLocation: " + sWindowName);
            rk.SetValue("MenuScreenLocation", "1");
            string strTrigger = "0";
            try
            {
                strTrigger = rk.GetValue("MenuTrigger").ToString();
                if (strTrigger == "0")
                    strTrigger = "1";
                else
                    strTrigger = "0";
            }
            catch
            {
            }
            //G.g_Tracer.Trace(10, "Writing to registry MenuDisplayInvokeTime: " + DateTime.Now.ToString());
            rk.SetValue("MenuDisplayInvokeTime", DateTime.Now.ToString());
            //G.g_Tracer.Trace(10, "Writing to registry MenuTrigger: " + strTrigger);
            if (checkBoxDone.Checked)
            {
                rk.SetValue("MenuTrigger_done", "0");
            }
            rk.SetValue("MenuTrigger", strTrigger);

            rk.Close();
            labelMessage.Text = "Invoke display " + m_strDisplayName;
        }
    }
}

Open in new window

tconingfordAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Robert SchuttSoftware EngineerCommented:
Try this instead of your current if/else block:
            List<TextBox> lstTxt = this.Controls.OfType<TextBox>().Where<TextBox>(t => t.Name.StartsWith("textBoxGraphic") && !string.IsNullOrEmpty(t.Text)).OrderBy<TextBox,int>(t => int.Parse(t.Name.Substring(14))).ToList();
            int idx = string.IsNullOrEmpty(m_strDisplayName) ? 0 : (lstTxt.IndexOf(lstTxt.FirstOrDefault<TextBox>(t => t.Text == m_strDisplayName)) + 1) % lstTxt.Count;
            m_strDisplayName = lstTxt[idx].Text;

Open in new window

If it works for you, you can make it a bit more efficient by moving the variable declaration to your class members and the initialization to the Form constructor, assuming the texts in the text boxes doesn't change.

EDIT: PS: probably in general, but especially if the list is static, it would normally be done by just having an index incremented, not by looking for the current string each time.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Robert SchuttSoftware EngineerCommented:
Just to show you a more general way to do this.

Add this to your member variables:
int m_intGraphicIndex = 0;

Open in new window

and then use this code instead of the original if/else block:
            m_strDisplayName = this.Controls[String.Format("textBoxGraphic{}", ++m_intGraphicIndex)].Text;
            m_intGraphicIndex %= 10;

Open in new window

But for this to work the textboxes really need to be numbered sequentially 1 thru 10, whereas the previous code would still work if there were missing or empty ones.
If you can share a bit more regarding your setup maybe a combination of the 2 is the best in your situation.
0
tconingfordAuthor Commented:
thanks for the help. I will implement both and see which works best. Your description of what it was doing was excellent to. Best Regards, Tony
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.