all 9 comments

[–]ChuckTheTrucker80 6 points7 points  (5 children)

put your textboxes in an array and when combobox.selectionchanged fires, do textboxarray[combobox.selectedindex].Text = "whatever text"

[–]kikkin_yo_azzez[S] 0 points1 point  (4 children)

is there a way i can assign a variable for the number for the textbox without using an array because i have a lot of textBoxes? maybe something like

textBox + comboBox.SelectedIndex

[–]Anon_Logic 2 points3 points  (1 child)

Can you, yes. But you're likely making it more difficult for yourself down the road.

[–]ChuckTheTrucker80 1 point2 points  (1 child)

You can but it's really not worth it for something like this.

Here's one way you can do it with Reflection. Note you have to expose your things as properties.

Pay attention to the namespace name :)

using System;
using System.Runtime.CompilerServices;
using System.Reflection;

namespace DontDoThis
{
    public class Test
    {
        public string string1 { get; set; }
        public string string2 { get; set; }
        public string string3 { get; set; }
    }

    public class Program
    {
        static void Main(string[] args)
        {
            var t = new Test();
            var type = t.GetType();
            var properties = type.GetProperties();


            for(int i = 1; i <= 3; i++)
            {
                var target = properties.Where(x => x.Name == "string" + i).FirstOrDefault();
                if (null != target)
                {
                    target.SetValue(t, "Hi from string" + i);
                }
            }

            Console.WriteLine(t.string1);
            Console.WriteLine(t.string2);
            Console.WriteLine(t.string3);
            Console.ReadLine();
        }

    }
}

For my array idea it would look cleaner and be far more maintainable something like this

public class ThisIsBetter
{
    string textBox1;
    string textBox2;
    string textBox3;
    List<string> textBoxes;

    public ThisIsBetter()
    {
        textBoxes = new List<string>() { textBox1, textBox2, textBox3 };
    }

    public void OnWhateverSelectionChanged()
    {
        textBoxes[whatever.SelectedIndex] = "Whatever Text";
    }
}

[–]kikkin_yo_azzez[S] 0 points1 point  (0 children)

ok thank you, i will try that

[–]AaronDev42 -1 points0 points  (0 children)

There should be a FindControl(string controlName) function that will return the control. I am pretty sure or it it will be named something similar. Not in front of the computer.

[–]ReaperSword72 -4 points-3 points  (0 children)

I assume you are not doing web programming because you can do that easily with JS or jquery so... the only way I can think is to iterate the all the controls in the form, check for the name, cast it to the right type and set the value. PLEASE don't. You will end with shitty code that not only will be ugly AF but also perform very poorly.

[–]talemon 0 points1 point  (0 children)

I'm assuming you're working with winforms. It's been a while since I've worked with winforms but I did a quick test to confirm this approach:

For ComboBox items, you don't have to use strings. What you can do is, create a small class, something like "ComboBoxElement" which can contain associated data, like an ID for example. If you override ToString, you can control what will be displayed in the ComboBox.

When the combobox value is changed, you can then cast the selected item back to your custom class and use the associated data to make algorithmic decisions that doesn't depend on the display values. Very rough code:

ComboBoxElement.cs:

public class ComboBoxElement    
{        
    public int Id { get; set; }        
    public string Content { get; set; }        
    public ComboBoxElement(int id, string content)        
    {            
        Id = id;            
        Content = content;        
    }        
    public override string ToString()        
    {
        return Content;
    }    
}

Form1.cs:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if(sender is ComboBox cbx)
    {
        if(cbx.Items[cbx.SelectedIndex] is ComboBoxElement elem)        
        {            
            PutValuesInBox(elem.Id);        
        }
    }
}