Insert text from textboxes and read only after |

0

Hello, I wanted to know how I can do so that the 2 textboxes inserts the IDs this way in txt:

file.txt:

id|192919 <---- textbox1
id2|29b9a92 <---- textbox2

And then when you open the program again puts it like this in the textbox:

textbox1: 192919
textbox2: 29b9a92

I tried substring, split, but I'm doubtful because the numbers can vary when putting in the textbox, and to pick up I also do not know how it does because it varies. How can I do it? Thank you.

    
asked by anonymous 30.05.2018 / 16:24

3 answers

0

It's a bit confusing to ask, but to get the value after | . you can use split and pass index.

The split will make a array of strings , but if it has more than one | array will have a higher index.

string id1 = "id|192919";
string id2 = "id2|29b9a92";

string valor1 = id1.Split('|')[1];
string valor2 = id2.Split('|')[1];

If you have the possibility to have more than one | . You can use Substring next to IndexOf that will get the index of the first |

string id1 = "id|19291|9";
string id2 = "id2|29b9a9|2";

string valor1 = id1.Substring(id1.IndexOf("|") + 1);
string valor2 = id2.Substring(id2.IndexOf("|") + 1);
    
30.05.2018 / 16:36
0

If your string always follows this standardization, split resolves. Use this method:

public static string GetId(string str)
    {
        var str1 = str.Split('|');
        return str1[1];
    }
    
30.05.2018 / 16:45
0

To write to the file:

System.IO.File.WriteAllLines("arquivo.txt", new string[] 
{
    string.Format("{0}|{1}", textbox1.ID, textbox1.Text),
    string.Format("{0}|{1}", textbox2.ID, textbox2.Text),
});

To read:

string[] linhas = System.IO.File.ReadAllLines("arquivo.txt");
foreach (string linha in linhas)
{
    string[] separadores = new string[1] { "|" };
    string[] tokens = linha.Split(separadores);
    if (token.Length == 2)
    {
        string id = token[0];
        string valor = token[1];

        Func<string, System.Windows.Forms.Control, System.Windows.Forms.Control> getControle = (id, controle) =>
        {
            if (!string.IsNullOrEmpty(id))
            {
                if (controle.ID == id)
                {
                    return controle;
                }

                if (controle.Controls != null && controle.Controls.Any())
                {
                    foreach (var c in controle.Controls)
                    {
                        if (c.ID == id)
                        {
                            return c;
                        }

                        System.Windows.Forms.Control child;
                        child = getControle(c, id);
                        return child;
                    }
                }
            }

            return null;
        }

        System.Windows.Forms.TextBox textbox = getControle(id, <seuform>) as System.Windows.Forms.TextBox;
        if (textbox != null)
        {
            textbox.Text = valor;
        }
    }
}
    
30.05.2018 / 16:49