Insert text from text boxes and read only after |

Asked

Viewed 44 times

0

Hi, I was wondering how can I do so that the 2 textboxs inserts the ID this way in txt:

txt file.:

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

And then when you open the program again put it like this in the text boxes:

textbox1: 192919
textbox2: 29b9a92

I tried substring, split, but I’m in doubt because the numbers may vary when placing in the textbox, and to catch also do not know how it does because it varies. How can I do it? Thank you.

3 answers

0


It’s a little confusing the question, but to get the value after the |. it is possible to use the split and pass the index.

The split will make a array of strings, but if you have more than one | the 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 of having more than one |. One can use the Substring next to the IndexOf that will pick up 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);
  • How can I save these strings to a txt: string id1 = "id|192919"; string id2 = "id2|29b9a92"; and then pick up with these strings: string value1 = id1.Substring(id1.Indexof("|") + 1); string value2 = id2.Substring(id2.Indexof("|") + 1);

  • At a glance in this tutorial from MS to save the file

  • OK got it, but as I take these id1 and id2 values inside the txt and do the split, that’s what I want, thanks.

  • Do you have any buttons in the form? if yes, inside the click it do string id1 = textbox1.Text; at a glance in that video

0

If your string always follows this standardization the split solves. Use this method:

public static string GetId(string str)
    {
        var str1 = str.Split('|');
        return str1[1];
    }

0

To write in the archive:

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;
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.