文本框拆分和处理C#

时间:2018-01-02 13:17:46

标签: c# .net winforms

当我喜欢爆炸或在C#中使用新行分割文本框时,我感到很困惑。我喜欢改变。

facebook.com
google.com
stacoverflow.com

该域名可以通过以下代码进行处理。我不认为

namespace DomainChecker
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (var webClient = new System.Net.WebClient())
            {
                string url = textBox1.Text;

                //get string from web
                string rawJSON = webClient.DownloadString("https://semanthic.com/api.php?api=masiting&dom=" + url);
                // convert json to the series obj
                Domain Doms = JsonConvert.DeserializeObject<Domain>(rawJSON);
                //
                //Console.WriteLine(Doms.Da);
                DataTable dt = new DataTable();
                dt.Columns.Add("Domain", typeof(string));
                dt.Columns.Add("Page Authority", typeof(string));
                dt.Columns.Add("Domain Authority", typeof(string));
                string P_A = Doms.Pa;
                string D_A = Doms.Da;
                DataRow row = dt.NewRow();
                row[0] = url;
                row[1] = P_A;
                row[2] = D_A;
                dt.Rows.Add(row);
                dataGridView1.DataSource = dt;

            }
        }
    }
}

2 个答案:

答案 0 :(得分:3)

您希望{Textbox1中的Split文本包含特定字符,例如换行符或回车符。在按钮单击中,您将执行此操作:

private void btnLoad_Click(object sender, EventArgs e)
{
    btnLoad.Enabled = false;
    // spilt the text on LF and/or CR values
    // this gives an array of strings
    string[] urls = textBox1.Text
        .Split(
            new char[] { '\r', '\n' }, 
            StringSplitOptions.RemoveEmptyEntries
        );
    // hand the urls to the background worker
    bgwLoader.RunWorkerAsync(urls);
}

看起来您将在该点击事件中做很多工作。最好把它交给BackgroudWorker。后台工作者的Dowork事件现在将循环遍历字符串数组,每个字符串为:

private void bgwLoader_DoWork(object sender, DoWorkEventArgs e)
{
    // initialize the datatable once
    DataTable dt = new DataTable();
    dt.Columns.Add("Domain", typeof(string));
    dt.Columns.Add("Page Authority", typeof(string));
    dt.Columns.Add("Domain Authority", typeof(string));

    // e.Argument holds the array with urls, don't forget to cast it
    foreach (var url in (string[]) e.Argument)
    {
        var webClient = new System.Net.WebClient();

        //get string from web
        string rawJSON = webClient.DownloadString("https://semanthic.com/api.php?api=masiting&dom=" + url);
        Trace.WriteLine(rawJSON);
        // convert json to the series obj
        Domain Doms = JsonConvert.DeserializeObject<Domain>(rawJSON);

        string P_A = Doms.Pa;
        string D_A = Doms.Da;
        DataRow row = dt.NewRow();
        row[0] = url;
        row[1] = P_A;
        row[2] = D_A;
        dt.Rows.Add(row);
    }
    // make sure the Completed event gets our result
    e.Result = dt;
}

现在,在runworkercompleted事件中,我们可以将数据表分配给datagrid:

private void bgwLoader_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // e.Result was set in the last line of the DoWork eventhandler
    dataGridView1.DataSource = (DataTable) e.Result;
    btnLoad.Enabled = true;
}

将所有这些放在一起会给你:

loaded in background

答案 1 :(得分:1)

要将TextBox拆分为行,您可以依赖TextBox.Lines,从网址下载字符串,您可以依赖DownloadStringTaskAsync,最后将数据绑定到{{ 1}}你可以依靠DataGridView。然后代码可以简单而干净,如下例所示:

List<T>