[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 419
  • Last Modified:

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

0
tconingford
Asked:
tconingford
  • 2
1 Solution
 
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
 
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

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now