This uses bing's web service. I have a single image button and an array of image buttons. The single changes each time it is clicked to the next image in the array. The problem is if I click it and do a new search the array of image buttons does not change.
CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using bing_search.net.bing.api;
using System.Collections;
namespace bing_search
{
public partial class _Default : System.Web.UI.Page
{
static ArrayList images = new ArrayList();
static Image[] imagearry;
static ImageButton[] imgButtnsArray;
static int counter = 0;
int fooBarCount = 0;
int firstLoad = 0;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DoItButton_Click(object sender, EventArgs e)
{
images.Clear();
imagearry = null;
imgButtnsArray = null;
BingService bs = new BingService();
net.bing.api.SearchRequest req = new SearchRequest();
req.AppId = "0B15AB60D625A10059A4A04B68615C5B0D904CA9";
req.Query = SearchBox.Text;
req.Sources = new SourceType[] { SourceType.Image};
req.Market = "en-us";
req.Adult = AdultOption.Off;
req.Image = new ImageRequest();
req.Image.CountSpecified = true;
req.Image.Count = 50;
SearchResponse resp = bs.Search(req);
foreach (ImageResult result in resp.Image.Results)
{
Image im = new Image();
im.ImageUrl = result.MediaUrl;
im.Width = 200;
im.Height = 200;
images.Add(im);
//this.Controls.Add(im);
}
// Image lol = (Image)images[0];
int size = images.Count;
imagearry = new Image[size];
Type typ = typeof(Image);
imagearry = (Image [])images.ToArray(typ);
ImageButton1.ImageUrl = imagearry[0].ImageUrl;
int blaCount = 0;
ArrayList imgButtns = new ArrayList();
foreach (Image ii in images)
{
ImageButton imgb = new ImageButton();
imgb.Width = 200;
imgb.Height = 200;
imgButtns.Add(imgb);
}
size = imgButtns.Count;
imgButtnsArray = (ImageButton[])imgButtns.ToArray(typeof(ImageButton));
foreach (ImageButton iii in imgButtnsArray)
{
imgButtnsArray[fooBarCount].ImageUrl = imagearry[fooBarCount].ImageUrl;
Panel1.Controls.Add(iii);
fooBarCount++;
}
fooBarCount = 0;
counter = 0;
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
counter++;
heightLable.Text = "clicked";
Image tempImage = (Image)imagearry[counter];
ImageButton1.ImageUrl = tempImage.ImageUrl;
foreach (ImageButton iii in imgButtnsArray)
{
imgButtnsArray[fooBarCount].ImageUrl = imagearry[fooBarCount].ImageUrl;
Panel1.Controls.Add(iii);
fooBarCount++;
}
fooBarCount = 0;
counter = 0;
}
}
}
You reset both counters on every click, so its always starts from the same image.
fooBarCount = 0;
counter = 0;
also they are not static, so they reset to 0 anyway on every page load, and show the same image and not change.
If from the other hand the cache is the problem, because I can not know whats the image file name, and maybe this is the issue here, then try something like.
imgButtnsArray[fooBarCount].ImageUrl =
imagearry[fooBarCount].ImageUrl +
"?rnd=" + RandomNumber.ToString();
I changed ImageButton1_Click to this and now it works. thanks for the quick responses. Back to playing with .net
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
counter++;
heightLable.Text = "clicked";
Image tempImage = (Image)imagearry[counter];
ImageButton1.ImageUrl = tempImage.ImageUrl;
//Random RandomNumber = new Random(10000);
foreach (ImageButton iii in imgButtnsArray)
{
Panel1.Controls.Add((Image)imagearry[fooBarCount]);
fooBarCount++;
}
fooBarCount = 0;
counter = 0;
}
Related
I'm new to C#, I need to overwrite label on windows form. I'm generating number of labels as per value of n using loop.
The issue: if I change the value of lines[i] while running the program, the value does get changed but is not updated on windows form (previous value does not gets replaced by new one).
Can any one guide me how can I do that?
Also I'm refreshing the code every second using timer, which is also working fine
This is the part where I create the label and write the value in it, and it is in loop
Label label = new Label();
label.Text = String.Format("{0}", lines[i]);
label.Left = 10;
label.Top = (i + 1) * 25;
this.Controls.Add(label);
here is my complete code id anyone needs to check it:
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.IO;
namespace Admin
{
public partial class Form1 : Form
{
string pathuser = #"//192.168.2.10/Shared-Public/Users.txt";
int check = 0;
public static string usernam;
string[] lines = new String[500];
string[] lines2 = new String[500];
public Form1()
{
InitializeComponent();
}
private void timer_Tick(object sender, EventArgs e)
{
tc = tc + 1;
Console.WriteLine(tc);
int c = 2;
int u = 0;
int us = 0; //for user pass
int z = 0; // for reading values from file
using (StreamReader sr2 = new StreamReader(pathuser))
{
string line;
while ((line = sr2.ReadLine()) != null)
{
lines[us] = line;
us++;
}
}
for (int i = 0; i < us; i++)
{
//Create label
Label label = new Label();
label.Text = String.Format("{0}", lines[i]);
//Position label on screen
label.Left = 10;
label.Top = (i + 1) * 25;
Label label2 = new Label();
label2.Left = 120;
label2.Top = (i + 1) * 25;
string line2;
string path = "//192.168.2.10/Shared-Public/" + lines[i] + DateTime.Now.ToString(" MM-dd-yyyy") + ".txt";
if (!File.Exists(path))
{
lines2[z] = null;
}
else
{
using (StreamReader sr3 = new StreamReader(path))
{
while ((line2 = sr3.ReadLine()) != null)
{
lines2[z] = line2;
z++;
}
}
label2.Text = String.Format("{0}", lines2[0]);
u = z;
z = 0;
}
PictureBox picbox = new PictureBox();
picbox.Location = new Point(240, 25 + (i*25));
picbox.Size = new Size(15, 15);
if (u%2==0)
picbox.BackColor = Color.Green;
else
picbox.BackColor = Color.Red;
u = 0;
this.Controls.Add(label);
this.Controls.Add(label2);
this.Controls.Add(picbox);
}
}
int tc = 0;
private void Form1_Load(object sender, EventArgs e)
{
Timer timer = new Timer();
timer.Interval = (1000);
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
}
}
Give your label's Name a Unique id and Search for it and replace the value:
Label label = new Label();
var someid = new Guid().ToString();
label.Name = someid;
label.Text ="aaaa";
label.Left = 10;
this.Controls.Add(label);
foreach(Control item in this.Controls)
{
if (item.Name == someid)
item.Text = "bbb";
}
I just want to create multiple web control using c# in visual studio. I have written the code for that but it create only once, I think it show the control created at the last in the loop.
Here is the code:
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;
namespace MDMScreenSharing
{
public partial class Form2 : Form
{
private List<Skybound.Gecko.GeckoWebBrowser> geckowebbrouser;
public Form2()
{
InitializeComponent();
this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
this.AutoSize = true;
this.Padding = new Padding(0, 0, 20, 20);
this.StartPosition = FormStartPosition.CenterScreen;
}
private void Form2_Load(object sender, EventArgs e)
{
int inputNumber =5;
geckowebbrouser = new List<Skybound.Gecko.GeckoWebBrowser>();
for (int i = 1; i <= inputNumber; i++)
{
int j = 1;
String wbname = "br" + i;
Skybound.Gecko.GeckoWebBrowser gw = new Skybound.Gecko.GeckoWebBrowser();
gw.Width = 200;
gw.Height = 200;
gw.Parent = panel1;
gw.Name = wbname;
gw.Location = new Point(gw.Width, panel1.Bottom + (i * 30));
gw.Navigate("http://192.168.1.162:8080");
geckowebbrouser.Add(gw);
this.Controls.Add(gw);
j = j*gw.Width;
}
}
}
}
This will show the web control created at the last. I think the program should be more dynamic for that. What am I doing wrong?
The form output, that show only once.
with the new addition of code i have code that one of you provided. the code is
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;
namespace MDMScreenSharing
{
public partial class Form2 : Form
{
private List<Skybound.Gecko.GeckoWebBrowser> geckowebbrouser;
public Form2()
{
InitializeComponent();
// this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
// this.AutoSize = true;
//this.Padding = new Padding(0, 0, 20, 20);
// this.StartPosition = FormStartPosition.CenterScreen;
this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.flowLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
this.flowLayoutPanel1.Size = new System.Drawing.Size(602, 395);
this.flowLayoutPanel1.TabIndex = 0;
}
private void Form2_Load(object sender, EventArgs e)
{
int inputNumber =4;
geckowebbrouser = new List<Skybound.Gecko.GeckoWebBrowser>();
for (int i = 1; i <= inputNumber; i++)
{
String wbname = "br" + i;
Skybound.Gecko.GeckoWebBrowser gw = new Skybound.Gecko.GeckoWebBrowser();
gw.Parent = flowLayoutPanel1;
gw.Width = 200;
gw.Height = 200;
gw.Name = wbname;
gw.Navigate("http://192.168.1.162:8080");
geckowebbrouser.Add(gw);
flowLayoutPanel1.Controls.Add(gw);
}
}
}
}
but the problem in this case only one browser window navigate the page.
like this
as you can see.
I think better approach will be using FlowLoayoutPanel so that you don't have to handle the position of each new control.
Go like this:
Add the FlowLayoutPanel through visual designer from toolbox.
Then on your designer.cs you should automatically have:
//
// flowLayoutPanel1
//
this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.flowLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
this.flowLayoutPanel1.Size = new System.Drawing.Size(602, 395);
this.flowLayoutPanel1.TabIndex = 0;
FormLoad event:
private void Form1_Load ( object sender, EventArgs e )
{
int inputNumber = 5;
geckowebbrouser = new List<Skybound.Gecko.GeckoWebBrowser>();
for ( int i = 1; i <= inputNumber; i++ )
{
int j = 1;
String wbname = "br" + i;
var gw = new Skybound.Gecko.GeckoWebBrowser();
gw.Width = 300;
gw.Height = 300;
gw.Name = wbname;
geckowebbrouser.Add(gw);
flowLayoutPanel1.Controls.Add(gw);
gw.Navigate("http://www.google.com");
}
}
Update
The only difference from the WebBrowser implementation is that you need to call the Navigate method after you've added the control to the panel. Check the updated code above.
I have tested with default Skybound.Gecko.GeckoWebBrowser control too, and it's working just fine too:
I use a .NET Winform version teechart 4.1.2012.1032.
I modified the sample that you supplied. "Extended\Reducing number of points\DownSampling Additions"
But When I zoom in chart, fastline's marks count is not 100 , downSampling.DisplayedPointCount.
How can I resolve it?
private void InitializeChart()
{
this.cursorTool1 = new Steema.TeeChart.Tools.CursorTool();//
this.tChart1.Tools.Add(this.cursorTool1);//
this.cursorTool1.FollowMouse = true;//
this.cursorTool1.Style = Steema.TeeChart.Tools.CursorToolStyles.Vertical;//
this.cursorTool1.Change += new Steema.TeeChart.Tools.CursorChangeEventHandler(this.cursorTool1_Change);//
CreateArrays();
tChart1.Aspect.View3D = false;
tChart1.Zoom.Direction = ZoomDirections.Both;//.Horizontal;//
tChart1.Series.Add(points = new Steema.TeeChart.Styles.Points());
tChart1.Series.Add(fastLine = new Steema.TeeChart.Styles.FastLine());
downSampling = new Steema.TeeChart.Functions.DownSampling(tChart1.Chart);
points.Add(xValues, yValues);
points.Active = false;
downSampling.DisplayedPointCount = 100;
downSampling.Method = Steema.TeeChart.Functions.DownSamplingMethod.MinMaxFirstLast;// Null;
fastLine.TreatNulls = Steema.TeeChart.Styles.TreatNullsStyle.DoNotPaint;
fastLine.DataSource = points;
fastLine.Function = downSampling;
this.tChart1.Axes.Custom.Add(new Steema.TeeChart.Axis(this.tChart1.Chart));//
this.tChart1[1].CustomVertAxis = this.tChart1.Axes.Custom[0];//
this.tChart1[0].CustomVertAxis = this.tChart1.Axes.Custom[0];//
this.fastLine.Marks.Visible = true;//
}
private void CreateArrays()
{
int length = 2600000;
xValues = new Nullable<double>[length];
yValues = new Nullable<double>[length];
Random rnd = new Random();
for (int i = 0; i < length; i++)
{
xValues[i] = i;
yValues[i] = i;
}
}
private void tChart1_Zoomed(object sender, EventArgs e)
{
tChart1[1].CheckDataSource(); //series 1 is the function series
}
The DisplayedPointCount specifies how many points the DownSampling function should paint and displays this number as a maximum number of points. As I explained here, you may need to combine it with Tolerance property to get the results you expect. So, you could do something like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitializeChart();
}
private void InitializeChart()
{
//tChart1.Dock = DockStyle.Fill;
tChart1.Aspect.View3D = false;
tChart1.Zoomed += tChart1_Zoomed;
Steema.TeeChart.Styles.Points points1 = new Steema.TeeChart.Styles.Points(tChart1.Chart);
Random y = new Random();
for (int i = 0; i < 10000; i++)
{
points1.Add(DateTime.Now.AddHours(i), y.Next());
}
points1.XValues.DateTime = true;
points1.Pointer.HorizSize = 1;
points1.Pointer.VertSize = 1;
Steema.TeeChart.Functions.DownSampling downSampling1 = new Steema.TeeChart.Functions.DownSampling(tChart1.Chart);
downSampling1.Method = Steema.TeeChart.Functions.DownSamplingMethod.Average;
downSampling1.Tolerance = 100;
downSampling1.DisplayedPointCount = Convert.ToInt32(downSampling1.Tolerance * 4);
Steema.TeeChart.Styles.Line line1 = new Steema.TeeChart.Styles.Line(tChart1.Chart);
line1.Function = downSampling1;
line1.DataSource = points1;
line1.Marks.Visible = true;
line1.Marks.Style = MarksStyles.PointIndex;
UpdateTitle();
}
void tChart1_Zoomed(object sender, EventArgs e)
{
tChart1[1].CheckDataSource();
UpdateTitle();
}
private void UpdateTitle()
{
tChart1.Header.Text = (tChart1[1].Function as Steema.TeeChart.Functions.DownSampling).DisplayedPointCount.ToString();
}
}
I am initiating me into programming thanks to Stack Overflow.
The game I'm doing in c #, consists of several bees flying around the desktop, which I have to give Click and SCORE is going to increase by a certain time.
To which I did the following:
Create a list PictureBox dynamically (at runtime): OK
Load PictureBox with these GIF images randomly: OK
![Bee Games][1]
In this part I was stuck:
Place these randomly PictureBox (in the bottom of the form).
Make the PictureBox move randomly (more or less marked routes).
each PictureBox Click Event, Change the PictureBox image and hide (visible = false), and increase in SCORE + 1.
I need help please.
My code is as follows:
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.IO;
namespace MiPrimerJuego
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int x = 20;
int y = 600;
List<System.Windows.Forms.PictureBox> objeto = new List<PictureBox>();
for (int i = 0; i < 10; i++, x += 90)
{
PictureBox pBox = new PictureBox();
pBox.Height = 80;
pBox.Width = 50;
pBox.Location = new System.Drawing.Point(x, y);
objeto.Add(pBox);
pBox.SizeMode = PictureBoxSizeMode.StretchImage;
Controls.Add(pBox);
var rand = new Random();
var files = Directory.GetFiles(Application.StartupPath + #"/Images", "*.gif");
pBox.Image = System.Drawing.Bitmap.FromFile(files[rand.Next(files.Length)]);
}
}
}
}
Declare rand and files outside the for-loop and objeto outside the function/void as a Private member variable:
private List<PictureBox> objeto = new List<PictureBox>();
private void button1_Click(object sender, EventArgs e)
{
var files = Directory.GetFiles(Application.StartupPath + #"/Images", "*.gif");
int x = 20;
int y = 600;
var rand = new Random();
for (int i = 0; i < 10; i++)
{
x += 90;
PictureBox pBox = new PictureBox();
pBox.Height = 80;
pBox.Width = 50;
pBox.Location = new System.Drawing.Point(x, y);
objeto.Add(pBox);
pBox.SizeMode = PictureBoxSizeMode.StretchImage;
Controls.Add(pBox);
pBox.Image = System.Drawing.Bitmap.FromFile(files[rand.Next(files.Length)]);
}
}
}
Sorry, I did not explain properly.
What I do is this:
1 Make the PictureBox move randomly from the bottom of the form (more or less marked routes).
2 In the Click event of each PictureBox, Changing the PictureBox image and hide (visible = false), and increase in SCORE + 1.
My code is as follows:
using System;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
using System.IO;
namespaceMiPrimerJuego
{
publicpartialclassForm1 : Form
{
public Form1()
{
InitializeComponent();
}
privatevoid button1_Click(object sender, EventArgs e)
{
int x = 20;
int y = 600;
List<System.Windows.Forms.PictureBox>objeto = newList<PictureBox>();
for (inti = 0; i< 10; i++, x += 90)
{
PictureBoxpBox = newPictureBox();
pBox.Height = 80;
pBox.Width = 50;
pBox.Location = newSystem.Drawing.Point(x, y);
objeto.Add(pBox);
pBox.SizeMode = PictureBoxSizeMode.StretchImage;
Controls.Add(pBox);
var rand = newRandom();
var files = Directory.GetFiles(Application.StartupPath + #"/Images", "*.gif");
pBox.Image = System.Drawing.Bitmap.FromFile(files[rand.Next(files.Length)]);
}
}
}
}
How can I manage that the event LongListSelector_SelectionChanged run first, then Image_MouseLeftButtonDown run after that
int count = 0;
Image LastImage = null, curImage = null;
BitmapImage bi1 = new BitmapImage();
BitmapImage bi2 = new BitmapImage();
int itemIndex;
void getImageLink()
{
for (int i = 0; i < CImageControl.lstImage.Count; i++)
s.Add(CImageControl.lstImage[i].ImageLink);
}
private void lstView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var myItem = ((LongListSelector)sender).SelectedItem as CImage;
itemIndex = ((LongListSelector)sender).ItemsSource.IndexOf(myItem);
//MessageBox.Show(myIndex.ToString());
}
then this will run
private void Image_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
string lastImageSource, lastImageAltSource, curImageSource, curImageAltSource;
count++;
if (count % 2 != 0)
{
LastImage = (Image)sender;
lastImageSource = s[itemIndex];
lastImageAltSource = lastImage.Source.ToString();
}
else
{
curImage = (Image)sender;
curImageSource = s[itemIndex];
curImageAltSource = curImage.Source.ToString();
}
}
ImageAltSouce is the display of the image, and I wanted to replace with ImageSource. But I need itemIndex to find index of images in Longlistselector before I can change any source. Since the event Image_MouseLeftButtonDown occur first, so I cant do anything