none
Best way to handle multiple variables in a string RRS feed

  • Question

  • I'm working on a project that is outside my skill level, and need some advice. Basically I have a WinForm into which users will enter customer data. There are sections for 2 customers (in case of husband/wife, etc) and each section has about 20 objects (labels, textboxes). Some of these are initially not visible, and may or not become visible based on the data added in other areas. Once complete, the user will click one of several buttons, to copy one or the other or both of the customer's info, taking the relevant text from the various form objects and formatting it into a string for the clipboard (the user needs to paste this into the host system's permanent history).

    My first (bad) idea was to declare a public variable for the text property of each object and then use those to build the string when the button is clicked, but if some objects are hidden they are still being added to the string and need to be left out. Also, if the user goes back and edits the form after the button has been clicked, the text properties of the objects obviously get changed,  but the variables aren't re-declared with the new values. Given the sheer number of variables needed, I'm not crazy having so many public variables, but I'm trying to find an alternative that will minimize the amount of code needed, make it more readable, & avoid building the string with "label1.text + textbox1.text + label2.text" etc etc etc., and a bunch of if statements to filter out any hidden fields. Hopefully this makes some kind of sense. Anyone got a good "best practice" for a situation like this?

    Friday, August 30, 2019 10:24 PM

Answers

  • Hi M. Cochran,

    Here is a workaround may help you. You can use a "foreach" to traverse all controls and filter them.

        public Form1()
        {
            InitializeComponent();
            label1.Visible = false;
            label3.Visible = false;
        }
    
        private void btCopy_Click(object sender, EventArgs e)
        {
            string str = String.Empty;
            foreach(Control control in this.Controls)
            {
                if(control.Visible == true && !(control is Button))
                {
                    str += control.Text;
                }
            }
            MessageBox.Show(str);
        }

    Result,

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 2, 2019 3:00 AM
    Moderator

All replies

  • Hello,

    Here are the basics which can easily be expanded upon. The code sample is in the following GitHub repository.

    Imports System.Text
    
    Public Class Form1
        ''' <summary>
        ''' Get visible Label and TextBox controls. Note that the controls
        ''' are not ordered but can be via .OrderBy Lambda extension.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub GetVisibleControls_Click(sender As Object, e As EventArgs) Handles GetVisibleControls.Click
            Dim visibleTextBoxes = TextBoxVisibleList()
    
            If visibleTextBoxes.Count > 0 Then
                Console.WriteLine(String.Join(",", visibleTextBoxes.Select(Function(item) item.Text)))
            End If
    
            Dim visibleLabels = LabelVisibleList()
    
            If visibleLabels.Count > 0 Then
                Console.WriteLine(String.Join(",", visibleLabels.Select(Function(item) item.Text)))
            End If
    
            Dim sb As New StringBuilder
    
            '
            ' Basics of working with conditions
            '
            If visibleTextBoxes.Count > 0 AndAlso visibleLabels.Count > 0 Then
                sb.Append(String.Join(",", visibleTextBoxes.Select(Function(item) item.Text)) & ",")
                sb.Append(String.Join(",", visibleLabels.Select(Function(item) item.Text)))
            End If
    
            If sb.Length > 0 Then
                MessageBox.Show(sb.ToString())
            Else
                MessageBox.Show("Nothing to show")
            End If
    
            '
            ' No assertion, just combine text for each visible control
            '
            Dim textBoxesTextOnly = visibleTextBoxes.Select(Function(item) item.Text).ToArray()
            Dim labelsTextOnly = visibleLabels.Select(Function(item) item.Text).ToArray()
            Dim combined = textBoxesTextOnly.Concat(labelsTextOnly).ToArray()
            Dim results = String.Join(",", combined)
    
            MessageBox.Show(results)
    
        End Sub
    End Class

    Design and run time


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, September 1, 2019 11:06 AM
    Moderator
  • Hi M. Cochran,

    Here is a workaround may help you. You can use a "foreach" to traverse all controls and filter them.

        public Form1()
        {
            InitializeComponent();
            label1.Visible = false;
            label3.Visible = false;
        }
    
        private void btCopy_Click(object sender, EventArgs e)
        {
            string str = String.Empty;
            foreach(Control control in this.Controls)
            {
                if(control.Visible == true && !(control is Button))
                {
                    str += control.Text;
                }
            }
            MessageBox.Show(str);
        }

    Result,

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 2, 2019 3:00 AM
    Moderator
  • Thanks Karen, there's a ton of stuff in here for me to wrap my head around! I'll definitely study this but for now I ended up using a solution very similar to what Kyle had proposed below.
    Wednesday, September 18, 2019 8:47 PM
  • Thanks Kyle, this was basically what I ended up doing, although I added 2 items - an integer variable that I could increment, along with a tag number on the controls I needed to capture. That way I can loop through the controls picking out only specific items & keeping them in order by the tag #'s. 
    Wednesday, September 18, 2019 8:51 PM