Home > Software engineering >  c# How can i do this?
c# How can i do this?

Time:11-21

Sorry i wrote a bad title and a bad example at start. New user :P

If this is possible somehow it would solve a lot of my issues:

Replace   
textbox1.Text = "something;

With those 2 lines

string myString = "textBox1";
myString.Text = "something"

But if you can solve my problem with an other way, please :D

Picture: https://ibb.co/ssW9jm3

Text: I am trying to loop a class properties, and display the properties. E.g. a textBox, would have a Text property. That is easy, solved with something like this (C# Iterate through Class properties)

However, i want to read the input from the UI e.g. a user to be able to write textBox1 or textBox and then i display the properties based on that

Code

        private void simpleButton2_Click(object sender, EventArgs e)
        {

            var txt_test = textBox1; // just to see its properties 


            // Type type1 = textBox1.GetType();


            Type type1 = txtSearchThing.Text.ToString().GetType();  // not correct.
            Type type2 = textBox1.GetType(); // correct data  //  textBox1.AcceptsReturn.GetType();  would also give correct data 



            System.Reflection.PropertyInfo[] properties1 = type1.GetProperties();
            System.Reflection.MemberInfo[] memberArray1 = type1.GetMembers();

            System.Reflection.PropertyInfo[] properties2 = type1.GetProperties();
            System.Reflection.MemberInfo[] memberArray2 = type2.GetMembers();



            Type typeFromStringExpression = Type.GetType("Textbox");
            dgcType1.DataSource = memberArray1;
            dgcType2.DataSource = memberArray2;

            return; // exit function
        }

Edit:

More code: I tried more stuff, but not much luck. (little messy)

    private void simpleButton2_Click(object sender, EventArgs e)
    {

        var txt_test = textBox1; // just to see its properties 


        // Type type1 = textBox1.GetType();


        Type type1 = txtSearchThing.Text.ToString().GetType();  // not correct.
        Type type2 = textBox1.GetType(); // correct data  //  textBox1.AcceptsReturn.GetType();  would also give correct data 



        System.Reflection.PropertyInfo[] properties1 = type1.GetProperties();
        System.Reflection.MemberInfo[] memberArray1 = type1.GetMembers();

        System.Reflection.PropertyInfo[] properties2 = type1.GetProperties();
        System.Reflection.MemberInfo[] memberArray2 = type2.GetMembers();



        Type typeFromStringExpression = Type.GetType("Textbox");
        dgcType1.DataSource = memberArray1;
        dgcType2.DataSource = memberArray2;

        return; // exit function
        var typeName = "System.Windows.Forms.TextBox";// "System.Tuple`2"; // this works
        //var typeName = "Textbox";
        var type = Type.GetType(typeName);
        var generic = type.MakeGenericType(typeof(string), typeof(int));
        Console.WriteLine(generic.FullName); //Outputs the type with the type parameters.


        string strGenericType = "Namespace.ClassName`2[[Namespace1.Type1, Type1DllName],[Namespace2.Type2, Type2DllName]], DllName";
        Type genericType = Type.GetType(strGenericType);
        object instance = Activator.CreateInstance(genericType);


        var aaa = type2.GetMembers();
        dgcType2.DataSource = aaa;
        dgcType1.DataSource = type2;



        // Create a new list of System.Reflection.PropertyInfo 
        List<System.Reflection.PropertyInfo> lst_propInfos = new List<System.Reflection.PropertyInfo>();
        foreach (System.Reflection.PropertyInfo propInfo in type1.GetProperties())
        {
            lst_propInfos.Add(propInfo);
        }
        dgcType1.DataSource = lst_propInfos;

        var string_textBox1 = "textBox1";
        //Type type2 = string_textBox1.GetType(); // This doesn't   ({Name = "String" FullName = "System.String")
        // dgvPropertyInfo.DataSource = type2;


        var textBox = Controls.OfType<TextBox>().FirstOrDefault(x => x.Name == $"textBox{1}");
        var textbox2 = Controls.OfType<TextBox>();



        // TypeInfo
        // List<System.Reflection.PropertyInfo> lst_typeInfos = new List<System.Reflection.TypeInfo>();

        //System.Reflection.TypeInfo[] properties = type1.GetNestedTypes();
        //System.Reflection.TypeInfo




        // Get all the properties in a list (of type System.Reflection.PropertyInfo)  from the object type1 (of type Type)
        System.Reflection.PropertyInfo[] properties = type1.GetProperties();
        //dgv.DataSource = type1;
        //cmbApiReflection.Properties.DataSource = type1;
        // Loop each property, and put items to a combobox
        foreach (System.Reflection.PropertyInfo propInfo in type1.GetProperties())
        {
            lst_propInfos.Add(propInfo);
        }
        //dgvPropertyInfo.DataSource = lst_propInfos;
        cmbApiReflection.Properties.DataSource = lst_propInfos;


        System.Reflection.TypeInfo type_info = typeof(TextBox).GetTypeInfo();
        IEnumerable<PropertyInfo> pList = type_info.DeclaredProperties;
        IEnumerable<MethodInfo> mList = type_info.DeclaredMethods;

        //aaa
        ///    dgvTypeInfo.DataSource = type_info;
        ///    dgvPropertyInfo.DataSource = pList;
        ///    dgvMethodInfo.DataSource = mList;
        ///

        StringBuilder sb = new StringBuilder();

        sb.Append("Properties:");
        foreach (PropertyInfo p in pList)
        {

            sb.Append("\n"   p.DeclaringType.Name   ": "   p.Name);
        }
        sb.Append("\nMethods:");
        foreach (MethodInfo m in mList)
        {
            sb.Append("\n"   m.DeclaringType.Name   ": "   m.Name);
        }

        txtTest.Text = sb.ToString();

        Type type3 = Type.GetType("form1.btnTest");
        Type type4 = Type.GetType("RiotSharp.RiotApi.api");
        Type type5 = Type.GetType("api");

        Type typ211 = Type.GetType("System.String");
    }

CodePudding user response:

This is case sensitive and assumes the control is on the form, not say a panel

string txt1 = "textBox1";
var textbox = Controls.OfType<TextBox>().FirstOrDefault(x => x.Name == txt1);
if (textbox != null)
{
    textbox.Text = "";
}

In a panel or group-box

var tb = Controls.Find(txt1, true);
if (tb.Length >0)
{
    ((TextBox)tb[0]).Text = "";
}

Then for 1-1000

for (int index = 0; index < 1000; index  )
{
    var textbox = Controls.OfType<TextBox>().FirstOrDefault(x => x.Name == $"textBox{index}");
    if (textbox != null)
    {
        textbox.Text = "";
    }
}

Edit

var baseText = "Textbox number ";
for (int index = 0; index < 1000; index  )
{
    var textBox = Controls.OfType<TextBox>().FirstOrDefault(x => x.Name == $"textBox{index}");
    if (textBox != null)
    {
        textBox.Text = $"{baseText}{index}";
    }
}

CodePudding user response:

I believe you want to edit the text in a Textbox. You can do like so:

string txt1 = Textbox1.Text;
txt1 = "(Edited Text)"   txt1;

And to change the text of Textbox at runtime, you can set the Text property of Textbox to that string.

Textbox1.Text = txt1;

Complete code, will then be:

string txt1 = Textbox1.Text;
txt1 = "(Edited Text)"   txt1;
Textbox1.Text = txt1;
  • Related