I've got a for loop to read line by line of a stream reader. This works very well. Now I've problems with reading the file of the stream reader. It's a csv file that contains two columns with information. The first one (A) contains a C# element like a textbox or a label (just like "label_1_title"), which is declared in my project. The second one (B) contains a normal simple string like "Hello".

Now I want to convert the string content of column A to the real existing element. Example: I've got "label_1_title" written in column A and that's an element, that exists in my project. Now I want to use this element to (for example) change it's content to the content of the column B of that line.

public void SetSprachpaket(string Sprachpaket)
{
    StreamReader StreamReader = new StreamReader(Sprachpaket, Encoding.UTF8);
    string Inhalt = StreamReader.ReadLine();

    for (int i = 1; Inhalt != null; i++)
    {
        var Items = Inhalt.Split(new Char[] { ';' });

        object Element = Items[0].GetType(); // Convert the string content of Items[1] to the existing object
        Element = Items[1]; // Take this existing object and give it the string content of Items[2]

        Inhalt = StreamReader.ReadLine();
    }
    StreamReader.Close();
}

I hope you can help me. Thanks in advance. Kind regards.

EDIT:

object Element = Items[0].GetType(); // Get (let's say) the string "myString"
Element = Items[1]; // --> myString = ...
有帮助吗?

解决方案 2

Simply by using foreach

foreach(control x in this.Control)
{
    if(x.Name == A)  // For A means the name of the object
    { x.Text = B; }  // For B means the string
}

其他提示

Please don't rely on internal naming within your project. You should have some kind of explicit mapping between elements and the column data.

Dump the CSV file into a Dictionary<string, string> of Column A => Column B. On the element containing all the elements you want to populate, set the DataContext to the dictionary. Define your labels like this:

<Label Content="{Binding [label_1_title]}" />

For more details, see Data Binding Overview. This will look up the dictionary key "label_1_title" and set the content to the value when the label loads.

In case you want to use additional data binding other than the label dictionary, I recommend you use a custom object for the DataContext, that stores the dictionary in a property such as LabelDictionary. In this case the binding should be:

<Label Content="{Binding LabelDictionary[label_1_title]}" />

This is not ideal, you'll need to play around with it, but somewhere in the direction you are looking for I think.

while (Inhalt != null)
{
    switch (Items[0])
    {
        case "label"
            element = new Label();
            element.Text = Items[1];
            break;
        case "textbox"
            element = new Textbox();
            element.Text = Items[1];
            break;
        case "numericUpDown"
            element = new NumericUpDown();
            element.Value = Item[1];
            break;
    }
}

Edit : if you are not wanting to populate the form from scratch, use this.Controls.Find() to find the control with that name.

If the items[0] is the name of the control you can do this to find the Control

this.Controls.Find(items[0])

you might then have to cast it to the appropriate type depending on which property you wish to assign to

control.Text = items[1];

Assuming that the first column is the name of a field in the same class and not the name of a control you could do this

var field = this.GetType().GetField(items[0], 
                                       BindingFlags.Instance 
                                       | BindingFlags.NonPublic);
var control = (Control)field.GetValue(this,null);
control.Text = items[1];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top