Reputation: 79
I was confuse when id like to explode or in C# called split the textbox with new lines. id like to change.
facebook.com
google.com
stacoverflow.com
And that domain can processing by the code belows. I don't think
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;
}
}
}
}
Upvotes: 0
Views: 762
Reputation: 125197
To split TextBox
to lines, you can rely on TextBox.Lines
, for downloading the string from a url, you can rely on DownloadStringTaskAsync
and at last for data-binding to DataGridView
you can rely on List<T>
. Then the code can be simple and clean like following example:
private async void button1_Click(object sender, EventArgs e)
{
var list = new List<MyClass>();
using (var webClient = new System.Net.WebClient())
{
foreach (var url in this.textBox1.Lines)
{
var content = await webClient.DownloadStringTaskAsync(url);
var myClass = Newtonsoft.Json.JsonConvert.DeserializeObject<MyClass>(content);
list.Add(myClass);
}
}
this.dataGridView1.DataSource = list;
}
Upvotes: 1
Reputation: 42444
You want Split
the text in Textbox1 on specific characters, for example linefeeds or carriage return. In your button click you would do this:
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);
}
It looks like you're going to do a lot of work in that click event. Better hand that to a BackgroudWorker
. The Dowork event of the backgroundworker will now loop over the array of strings with a for each:
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;
}
Now in the runworkercompleted event we can assign the datatable to the 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;
}
Putting this all together will give you:
Upvotes: 3