I am trying to navigate page, using different useragents. I have a random string generator, a timer that changes it every second (generate new random string every second), and a navigate. My question is, how to use string useragent (which is being changed every second) in another void ?
public static string RandomString(Random r)
{
string s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var sb = new StringBuilder();
int cnt = r.Next(5, 33);
for (int i = 1; i <= cnt; i++)
{
int idx = r.Next(0, s.Length);
sb.Append(s.Substring(idx, 1));
}
return sb.ToString();
}
private void timer2_Tick(object sender, EventArgs e)
{
var r = new Random();
string useragent = RandomString(r);
timer2.Stop();
timer2.Interval = 1;
timer2.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
Navigate("http://example.org",useragent,null);
}
You can use useragent in global scope
private string useragent = string.Empty;
private void timer2_Tick(object sender, EventArgs e)
{
var r = new Random();
useragent = RandomString(r);
timer2.Stop();
timer2.Interval = 1;
timer2.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(useragent))
return;
Navigate("http://example.org", useragent, null);
}
EDIT:
You should generate the string only when it is needed while navigating, so use this
private Random r = new Random();
private void timer1_Tick(object sender, EventArgs e)
{
Navigate("http://example.org", RandomString(r), null);
}
Related
I am writing a c# windows form code to
get the number from button1 and button2 and add them together in a text box but The compiler argues on the convert.toint32(textbox3.text) statement
and also it increases the value of the two variable and three variable how can I keep it constant but increase the value of textbox
and I need a solution?
int Three = 0;
int Two = 0;
//int one = 0;
int sum = 0;
// int sum = 0;
//int dec = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// MessageBox.Show("Enter the teams` name");
}
private void button1_Click(object sender, EventArgs e)
{
//Three += 3;
//textBox3.Text = sum.ToString();
Three += 3;
sum = Convert.ToInt32(textBox3.Text) + Three;
textBox3.Text = sum.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
Two += 2;
sum = Two + Convert.ToInt32(textBox3.Text) + Three;
textBox3.Text =Convert.ToInt32(textBox3.Text) + Two.ToString();
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
textBox3.Text = 0.ToString();
}
`
Change
sum = Convert.ToInt32(textBox3.Text) + Three;
To
sum = Convert.ToInt32(textBox3.Text == "" ? "0" : textBox3.Text) + 3;
Also, remove
private void textBox3_TextChanged(object sender, EventArgs e)
{
textBox3.Text = 0.ToString(); // this
}
because it doesn't make any sense.
Your variables belong to class and they are accessible for initialization in constructor. This can be done in many ways but you need to check if textboxes have values then try to convert it and add it.
private int Two;
private int Three;
private int sum;
public Form1()
{
this.Two = 0;
this.Three = 0;
this.sum = 0;
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// MessageBox.Show("Enter the teams` name");
}
private void button1_Click(object sender, EventArgs e)
{
this.Three += 3;
sum = textBox3.Text != String.Empty ? Convert.ToInt32(textBox3.Text) : 0;
textBox3.Text = Convert.ToString(sum + this.Three);
}
... same for number Two
private void textBox3_TextChanged(object sender, EventArgs e)
{
textBox3.Text = "0";
}
I am new to c# and I know the answer is very simple I just could not find it through searching
I created two buttons the first one generates random values
and the second one is an IF statement inside another button, but I`m getting a red line under 1value1 saying
the name does not exists in current context
private void button3_Click(object sender, EventArgs e)
{
Random b = new Random();
float value = b.Next(50, 100);
}
private void button2_Click(object sender, EventArgs e)
{
if (value < MinValue)
{
textBox18.Text = ("warning");
textBox18.ForeColor = Color.White;
textBox18.BackColor = Color.Red;
}
}
value is defined in the scope of the button3_Click and thus not accessible for the button2_Click. Put it as a variable of the class:
private int _minValue = 50;
private int _maxValue = 100;
private float _value = _maxValue;
private void button3_Click(object sender, EventArgs e)
{
Random b = new Random();
_value = b.Next(_minValue, _maxValue);
}
private void button2_Click(object sender, EventArgs e)
{
if (_value < _minValue)
{
textBox18.Text = ("warning");
textBox18.ForeColor = Color.White;
textBox18.BackColor = Color.Red;
}
}
Set property GenerateMember=True
I am trying to make a simple app that grabs all *.jpg files from a folder, and displays them in PictureBox.
However i'm trying to make Prev/Next buttons to show images one by one. Here's my code for the Next button so far:
string path = #"..\..\Resources\Wallpapers\";
int count = 0;
private void Form1_Load(object sender, EventArgs e)
{
DisplayNextFile(count);
}
private void next_Click(object sender, EventArgs e)
{
DisplayNextFile(count);
}
private void DisplayNextFile(int c)
{
var rand = new Random();
var files = Directory.GetFiles(path, "*.jpg");
var images = new Image[files.Length];
for (int i = c; i < files.Length; ++i)
{
images[i] = Image.FromFile(files[i]);
picBoxMainPreview.Image = images[i];
break;
}
count++;
if (count == files.Length)
count = 0;
}
It works fine, but how can I do it for the Previous button?
Remove count++; from DisplayNextFile.
private void next_Click(object sender, EventArgs e)
{
count = (++count) % files.Length;
DisplayNextFile(count);
}
private void previous_Click(object sender, EventArgs e)
{
count = (count - 1) >= 0 ? count - 1 ? files.Length - 1;
DisplayNextFile(count);
}
Replace the for loop with:
picBoxMainPreview.Image = Image.FromFile(c);
I'm quite new to this things. Actually it is my first work. I want a program that reads random file count from textbox. and it has a button to randomize files from selected path. I need to open files in the listbox.
My problem is when I double click on the listbox, it opens the last file in the list no matter what file I d.clicked. I tried to add lines that put two slash before below. But it also didnt work. What can I do?
public Form1()
{
InitializeComponent();
}
Random r = new Random();
string path1;
DirectoryInfo dif;
FileInfo[] files;
int randomchoose;
//FileInfo[] files2;
//int hoho;
int[] randomcount;
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog hoho = new FolderBrowserDialog();
hoho.ShowNewFolderButton = true;
if (hoho.ShowDialog() == DialogResult.OK)
{
path1 = hoho.SelectedPath;
textBox1.Text = path1;
dif = new DirectoryInfo(path1);
files = dif.GetFiles();
}
}
private void btnrasgele_Click(object sender, EventArgs e)
{
randomcount = new int[Convert.ToInt32(textBox3.Text)];
// int hoho=0;
foreach (int k in randomcount)
{
int pd = files.Length;
randomchoose = r.Next(0, Convert.ToInt32(pd + 1));
listBox1.Items.Add(files[randomchoose]);
//files2[hoho] = files[randomchoose].FullName;
}
}
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
//listBox1.SelectedIndex = hoho;
//Process.Start(files2[hoho].FullName);
Process.Start(files[randomchoose].FullName);
}
You passed in the randomchoose which is fixed at that point, try this instead:
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if(listBox1.SelectedItem != null)
Process.Start(((FileInfo)listBox1.SelectedItem).FullName);
}
So in my very small program I am generating a random percentage and based off that percentage it should display one of two panels. However, it is only ever displaying one.
Here is my code:
Random random = new Random();
private void button1_Click(object sender, EventArgs e)
{
bool button1Clicked = true;
if (button1Clicked == true) { ITIpanel.Visible = true; }
}
private void ITIpanel_Paint(object sender, PaintEventArgs e)
{
ITItimer.Enabled = true;
}
private void ITItimer_Tick(object sender, EventArgs e)
{
double rand = random.NextDouble();
if (rand <= .50) { bluestimPanel.Visible = true; }
if (rand >= .50) { redstimPanel.Visible = true; }
ITItimer.Enabled = false;
}
private void bluestimPanel_Paint(object sender, PaintEventArgs e)
{
Trialtimer.Enabled = true;
}
private void redstimPanel_Paint(object sender, PaintEventArgs e)
{
Trialtimer.Enabled = true;
}
private void Trialtimer_Tick(object sender, EventArgs e)
{
bluestimPanel.Visible = false;
redstimPanel.Visible = false;
Trialtimer.Enabled = false;
ITIpanel.Visible = true;
}
I think what is happening here is that in the TrialTimer_Tick method setting ITIPanel.Visible to true is not causing ITIPanel to repaint and hence ITITimer never restarts.
You can put a breakpoint in the ITITimer_Click method and see if it is ever invoked again after the first invocation.