I'm trying to build an application that goes to a certain website (from a textbox) x number of times (also from a textbox) through a proxy. What I'm having trouble with is getting the proxy list into the program and using it in the webBrowser control. Here's what I have so far. Any help is appreciated. Thanks.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Permissions;
using Microsoft.Win32;
using System.Diagnostics;
using System.Threading;
using System.IO;
using System.Net;
namespace LetsBot
{
public partial class TrafficBot : Form
{
public TrafficBot()
{
InitializeComponent();
}
private void btnClear_Click(object sender, EventArgs e)
{
this.txtURL.Clear();
this.txtProxy.Clear();
this.txtURL.Focus();
}
private void btnStart_Click(object sender, EventArgs e)
{
this.lblBrowsing.Show();
this.btnStart.Enabled = false;
int n = Convert.ToInt32(this.txtNumVisits.Text);
for (int i = 0; i < n; i++)
{
string url = this.txtURL.Text;
this.webBotBrowser.Navigate(url);
while (webBotBrowser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
string PageText = this.webBotBrowser.DocumentText.ToString();
Thread.Sleep(5000);
}
}
private void btnStop_Click(object sender, EventArgs e)
{
this.btnStart.Enabled = true;
this.webBotBrowser.Stop();
this.txtURL.Focus();
}
private void btnExit_Click(object sender, EventArgs e)
{
DialogResult dialogResult = MessageBox.Show("Exit Traffic Bot?", "Don't Let Me Go!", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
Application.Exit();
}
else if (dialogResult == DialogResult.No)
{
}
}
private void webBotBrowser_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
{
if (e.CurrentProgress > 0)
{
ProgressBar.Value = (int)(e.CurrentProgress / e.MaximumProgress * 100);
}
}
private List<string> getProxyListFromText(string input)
{
List<string> list = new List<string>();
StreamReader reader = new StreamReader(input);
string item = "";
while (item != null)
{
item = reader.ReadLine();
if (item != null)
{
list.Add(item);
}
}
reader.Close();
return list;
for (int i = 0; i < listBox1.Items.Count; i++)
{
object url;
WebClient wc;
url = this.txtURL.Text;
wc = new WebClient();
//This should come from the proxy list
foreach (string prox in getProxyListFromText("Proxies.txt"))
{
wc.Proxy = new WebProxy(prox);
var page = wc.DownloadString(url.ToString());
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(page);
var pplname = doc.DocumentNode.SelectNodes("/html/body/div[3]/div/div[2]/div[2]/div/div[4]/p");
}
}
}
}
}
Related
I am trying to get the cell value in a string from the selected cell in the "Document Number" column in a DataGridView. My code to retrieve data from SharePoint and populate the DataGridView works fine, but I seem to be caught in a loop there? I cannot execute any other methods after populating the DataGridView . I can select a new search term and execute the Button1_Click event again successfully, but I cannot get any other methods to execute?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;
namespace BuyersForeverFriend
{
public partial class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
string userSearch = textBox1.Text;
button1.Enabled = true;
}
private void button1_Click(object sender, EventArgs e)
{
using (ClientContext ctx = new ClientContext("https://teamsites.nafta.fcagroup.com/sites/PSOAPPS/iws/"))
{
var userInput = textBox1.Text;
Web web = ctx.Web;
List list = web.Lists.GetById(new Guid("61ef6657-eff7-42cb-99e1-8afd590334ec"));
var q = new CamlQuery() { ViewXml = "<View><Query><Where><Contains><FieldRef Name='Item_x0020_Description' /><Value Type='Note'>" + userInput + "</Value></Contains></Where></Query><ViewFields><FieldRef Name='Title' /><FieldRef Name='Topic_x002d_' /><FieldRef Name='Item_x0020_Description' /></ViewFields><QueryOptions /></View>" };
var r = list.GetItems(q);
ctx.Load(r);
ctx.ExecuteQuery();
if (r.Count != 0)
{
var searchResults = new DataTable();
searchResults.Columns.AddRange(new[]
{
new DataColumn("ID"), new DataColumn("Document Number"), new DataColumn("Item Description"), new DataColumn("Topic")});
foreach (var oListItem in r)
{
searchResults.Rows.Add(oListItem["ID"], oListItem["Title"], oListItem["Item_x0020_Description"],
oListItem["Topic_x002d_"]);
}
if (dataGridView1 != null)
{
dataGridView1.DataSource = searchResults;
dataGridView1.Refresh();
dataGridView1.Columns[2].DefaultCellStyle.Format = "dd'/'MM'/'yyyy";
var dataGridViewColumn = dataGridView1.Columns["ID"];
if (dataGridViewColumn != null)
dataGridViewColumn.Visible = false;
}
else
MessageBox.Show("after else statement");
}
}
MessageBox.Show("after sharepoint if statement");
return;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show("a cell was clicked");
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
MessageBox.Show("starting routine");
if (dataGridView1.SelectedCells.Count > 0)
{
int selectedrowindex = dataGridView1.SelectedCells[0].RowIndex;
DataGridViewRow selectedRow = dataGridView1.Rows[selectedrowindex];
string a = Convert.ToString(selectedRow.Cells["Document Number"].Value);
MessageBox.Show(a);
textBox2.Text = ("The var =" + a);
}
else MessageBox.Show("if did not end up true");
}
}
}
I failed to initialize these, after adding them to the form method...poof everything works! :
public Form1()
{
InitializeComponent();
dataGridView1.CellClick += dataGridView1_CellClick;
dataGridView1.SelectionChanged += dataGridView1_SelectionChanged;
}
I'm using Web browser in my form which navigates to multiple pages. I want to take screenshot of Last page once loaded completely. I've coded taking screen shot inside the webBrowser1_DocumentCompleted. However it is taking screenshot before page getting loaded. I'm confused where I can take screenshot. I found LoadCompleted event in google but not sure how to use. Please help me out.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing.Imaging;
namespace CC
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
string url = "http://www.lll.com";
webBrowser1.Navigate(new Uri(url));
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
catch (Exception ex)
{
}
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string AbsoluteURL = e.Url.AbsolutePath.ToString();
int count = 0;
switch (AbsoluteURL)
{
case "Page1":
var loginControl = webBrowser1.Document.GetElementById("user");
var passwordControl = webBrowser1.Document.GetElementById("password");
var btn = webBrowser1.Document.GetElementById("Submit");
if (loginControl != null)
loginControl.SetAttribute("value", "XXX");
if (passwordControl != null)
passwordControl.SetAttribute("value", "YYY");
var elems = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement elem in elems)
{
if (elem.GetAttribute("tabindex") == "3")
{
elem.InvokeMember("click");
}
}
break;
case"Page2": // THIS IS NOT WORKING. TAKING SCREEN SHOT BEFORE PAGE GETTING LOADED FULLY. NEED HELP HERE
using (Bitmap bitmap = new Bitmap(webBrowser1.Width, webBrowser1.Height, PixelFormat.Format24bppRgb))
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(
PointToScreen(webBrowser1.Location),
new Point(0, 0),
webBrowser1.Size);
bitmap.Save(#"C:\123.bmp");
}
break;
default:
Console.WriteLine("Better try again");
break;
}
}
}
}
}
Try this one.
protected void Capture(object sender, EventArgs e)
{
string url = "www.google.com";
Thread thread = new Thread(delegate()
{
using (WebBrowser browser = new WebBrowser())
{
browser.ScrollBarsEnabled = false;
browser.AllowNavigation = true;
browser.Navigate(url);
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
while (browser.ReadyState != WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = sender as WebBrowser;
//do what do you want
}
In my experience, navigation and document loading are distinct from having the document be actually rendered. I would look at ReadyState https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowserreadystate%28v=vs.110%29.aspx to determine of the document is actually rendered and not just loaded.
I set a List have 2 elements to the dataGridView's DataSource, but it's always have one row, the RowCount property is 1. This is so strange
My code below have some Entity class name which I must explain.
HoaDon is Order
ChiTietHoaDon is OrderDetail
SanPham is Product
Code:
using GymFitnessOlympic.Controller;
using GymFitnessOlympic.Models;
using GymFitnessOlympic.Models.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GymFitnessOlympic.View.ActForm
{
public partial class FrmBanHang2 : Form
{
List<SanPham> allSanPham;
List<ChiTietHoaDon> allChiTiet = new List<ChiTietHoaDon>();
HoaDon hoaDon = new HoaDon();
public FrmBanHang2()
{
InitializeComponent();
int phongID = Login.GetPhongHienTai().MaPhongTap;
allSanPham = SanPhamController.GetList(phongID);
dgrChiTiet.AutoGenerateColumns = false;
dgrChiTiet.AutoGenerateColumns = false;
loadListSanPham(allSanPham);
}
void loadListSanPham(List<SanPham> li)
{
lbSanPham.DataSource = li;
}
void updateTable()
{
dgrChiTiet.DataSource = allChiTiet;
int n = dgrChiTiet.RowCount;
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (spnQuantity.Value <= 0)
{
dxErrorProvider1.SetError(spnQuantity, "Quantity must be greater than 0");
spnQuantity.Focus();
return;
}
int gia;
if (!int.TryParse(txtPrice.Text, out gia) || (gia % 500 != 0) || gia <= 0)
{
txtPrice.Focus();
dxErrorProvider1.SetError(txtPrice, "Invalid Price");
return;
}
if (lbSanPham.SelectedItem != null)
{
var sp = (SanPham)lbSanPham.SelectedItem;
int soLuong = int.Parse(spnQuantity.Text);
ChiTietHoaDon c = new ChiTietHoaDon()
{
Gia = gia,
MaSanPham = sp.MaSanPham,
SoLuong = soLuong,
SanPham = sp
};
var old = allChiTiet.FirstOrDefault(c1 => c1.MaSanPham == c.MaSanPham);
if (old != null)
{
old.SoLuong += c.SoLuong;
}
else
{
allChiTiet.Add(c);
}
}
updateTable();
}
private void lbSanPham_SelectedIndexChanged(object sender, EventArgs e)
{
if (lbSanPham.SelectedItem != null)
{
var sp = (SanPham)lbSanPham.SelectedItem;
txtTenSanPham.Text
= sp.TenSanPham;
txtPrice.Text = sp.Gia.ToString();
spnQuantity.Text = "1";
}
}
}
}
This is because you are using List<ChiTietHoaDon> as data source of your datagridview and it doesn't notify changes so the grid doesn't show new items.
You should use BindingList<ChiTietHoaDon> instead.
But if you want to continue using List<ChiTietHoaDon>, to update the grid you can first set dgrChiTiet.DataSource=null; and then dgrChiTiet.DataSource = allChiTiet;
I am trying to make a MP3 player with playlist functionality, I have googled everywhere, tried examples, but nothing works. Where the code freezes up at is when p[ListBox1.SelectedIndex + 1]; is called, and I have no idea why it locks up the program. I have tried making an array and selecting the next song through that, but that also didn't work.
I think this is the easiest way to do this. But it locks up the program for some reason.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Collections;
namespace MusicPlayer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string[] f, p;
ArrayList playlist = new ArrayList();
private int current_index = 0;
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
if(openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK){
f = openFileDialog1.SafeFileNames;
p = openFileDialog1.FileNames;
for (int i = 0; i < f.Length; i++)
{
listBox1.Items.Add(f[i]);
}
foreach (string d in open.FileNames)
{
listBox1.Items.Add(d);
}
}
}
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
axWindowsMediaPlayer1.URL = p[listBox1.SelectedIndex];
}
private void Form1_Load(object sender, EventArgs e)
{
axWindowsMediaPlayer1.PlayStateChange += new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(MediaPlayer_PlayStateChange);
}
public void MediaPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
switch (axWindowsMediaPlayer1.playState)
{
// Locks up here
case WMPLib.WMPPlayState.wmppsReady:
axWindowsMediaPlayer1.URL = p[listBox1.SelectedIndex + 1];
break;
case WMPLib.WMPPlayState.wmppsStopped:
axWindowsMediaPlayer1.URL = p[listBox1.SelectedIndex + 1];
break;
default:
break;
}
}
}
}
EDIT:
I updated my code and this is what it looks like:
public void MediaPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
switch (axWindowsMediaPlayer1.playState)
{
case WMPLib.WMPPlayState.wmppsReady:
axWindowsMediaPlayer1.BeginInvoke(new Action(change_song));
break;
case WMPLib.WMPPlayState.wmppsStopped:
axWindowsMediaPlayer1.BeginInvoke(new Action(change_song));
break;
default:
break;
}
}
void change_song()
{
axWindowsMediaPlayer1.URL = p[listBox1.SelectedIndex + 1];
}
But when I run this, it gets stuck at "Media Changing" and eventually does nothing.
This question already has an answer here:
Function call only works when MessageBox.Show() is included?
(1 answer)
Closed 8 years ago.
I try to make simple audio player,
then I try it.
After end of song, the player stopped.
I want to set it automatically and select randomized, at the playlist.
I use URL and a ListBox as playlist..
This is the code snippet at autoplay part:
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 8)
{
Random rnd = new Random();
int nowPlayIndex = rnd.Next(listURLPlayers.Count);
axWindowsMediaPlayer1.URL = listURLPlayers[nowPlayIndex];
axWindowsMediaPlayer1.Ctlenabled = true;
axWindowsMediaPlayer1.Ctlcontrols.play();
listAudio.SelectedIndex = nowPlayIndex;
}
}
But I try it then the URL changed, but not played automatically.
What is wrong with my code?
https://github.com/mudzakkir/MP3Player.git
Please help.
Ok it is now working..
I should use Timer..
My code is now like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Mp3Player
{
public partial class MediaPlayer : Form
{
string HomeDir;
string[] sPlaylists, sURLs;
List<string> listURLPlayers = new List<string>();
public MediaPlayer()
{
InitializeComponent();
HomeDir = Directory.GetCurrentDirectory();
string[] playlist = File.ReadAllLines("playlist.txt");
foreach (string fileText in playlist)
{
listAudio.Items.Add(fileText);
}
string[] playlistUrl = File.ReadAllLines("playlistURL.txt");
foreach (string fileText in playlistUrl)
{
listURLPlayers.Add(fileText);
}
}
private void btnOpen_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
sPlaylists = openFileDialog1.SafeFileNames;
sURLs = openFileDialog1.FileNames;
for (int i = 0; i < sPlaylists.Length; i++)
{
listAudio.Items.Add(sPlaylists[i]);
}
for (int i = 0; i < sURLs.Length; i++)
{
listURLPlayers.Add(sURLs[i]);
}
System.IO.StreamWriter SaveFile = new System.IO.StreamWriter("playlist.txt");
foreach (var item in listAudio.Items)
{
SaveFile.WriteLine(item);
}
SaveFile.Close();
SaveFile = new System.IO.StreamWriter("playlistURL.txt");
foreach (var item in listURLPlayers)
{
SaveFile.WriteLine(item);
}
SaveFile.Close();
}
}
private void listAudio_MouseDoubleClick(object sender, MouseEventArgs e)
{
axWindowsMediaPlayer1.URL = listURLPlayers[listAudio.SelectedIndex];
}
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 8)
{
timer1.Enabled = true;
}
}
private void listAudio_SelectedIndexChanged(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = listURLPlayers[listAudio.SelectedIndex];
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
Random rnd = new Random();
int nowPlayIndex = rnd.Next(listURLPlayers.Count);
axWindowsMediaPlayer1.URL = listURLPlayers[nowPlayIndex];
listAudio.SelectedIndex = nowPlayIndex;
}
}
}
Thank You