PictureBox hover over Parameter is not valid issue - c#

I have 3 picture boxes which hold an image from a list string. There is a button which goes through each element in the list string, which in turn updates the 3 picture boxes the picture boxes that hold the 3 images are, picturebox3, 4 and 5. i have added a mouse event that triggers once the mouse is on the image to expand the image in another picturebox which is hidden until the mouse is on top of it. i created 3 extra pictureboxes which are pictureBox6, 7 and 8 to show the enlarged image which relate to picturebox3, 4 and 5. picturebox 6 is related to 3, picturebox 7 is related to 4 and 8 is related to 5.
I have tried to set the picturebox to null after the mouse is off the image however that hasn’t worked.
Below is the code for the image in picturebox 3, 4 and 5 to be updated as it goes through the list it also includes the event triggers.
download_file_names[] = the filename ".jpg"
matching_image_for_Processing[intIndex] = the folder which holds the file
Those variables above do change as i go through the list so i know that works
I have tried to set the picturebox to null after the mouse is off the image however that hasnt worked.
Below is the code for the image in picturebox 3, 4 and 5 to be updated as it goes through the list it also includes the event triggers.
if (download_file_names.ElementAtOrDefault(0) != null)
{
pictureBox3.Image = new Bitmap(Environment.CurrentDirectory + "\\" + "temp_data" + "\\" + matching_image_for_Processing[intIndex] + "\\" + download_file_names[0]);
pictureBox3.MouseHover += new EventHandler((sender2, e2) => pictureBox3_Mouse(sender2, e2, download_file_names[0], matching_image_for_Processing[intIndex]));
pictureBox3.MouseLeave += new EventHandler(pictureBox3_MouseLeave);
}
else
{
pictureBox3.Image = new Bitmap(Environment.CurrentDirectory + "\\" + "NoImage.bmp");
}
if (download_file_names.ElementAtOrDefault(1) != null)
{
pictureBox4.Image = new Bitmap(Environment.CurrentDirectory + "\\" + "temp_data" + "\\" + matching_image_for_Processing[intIndex] + "\\" + download_file_names[1]);
pictureBox4.MouseHover += new EventHandler((sender2, e2) => pictureBox4_Mouse(sender2, e2, download_file_names[1], matching_image_for_Processing[intIndex]));
pictureBox4.MouseLeave += new EventHandler(pictureBox4_MouseLeave);
}
else
{
pictureBox4.Image = new Bitmap(Environment.CurrentDirectory + "\\" + "NoImage.bmp");
}
if (download_file_names.ElementAtOrDefault(2) != null)
{
pictureBox5.Image = new Bitmap(Environment.CurrentDirectory + "\\" + "temp_data" + "\\" + matching_image_for_Processing[intIndex] + "\\" + download_file_names[2]);
pictureBox5.MouseHover += new EventHandler((sender2, e2) => pictureBox5_Mouse(sender2, e2, download_file_names[2], matching_image_for_Processing[intIndex]));
pictureBox5.MouseLeave += new EventHandler(pictureBox5_MouseLeave);
}
else
{
pictureBox5.Image = new Bitmap(Environment.CurrentDirectory + "\\" + "NoImage.bmp");
}
below is the code for the other picturesboxes to display the larger image.
void pictureBox3_Mouse(object sender, EventArgs e, string catching, string catching2)
{
pictureBox6.Visible = true;
pictureBox6.Image = new Bitmap(Environment.CurrentDirectory + "\\" + "temp_data" + "\\" + catching2 + "\\" + catching);
Console.WriteLine("temp_data" + "\\" + catching2 + "\\" + catching);
pictureBox6.Refresh();
}
void pictureBox3_MouseLeave(object sender, EventArgs e)
{
pictureBox6.Visible = false;
pictureBox6.Image = null;
pictureBox6.Refresh();
}
void pictureBox4_Mouse(object sender, EventArgs e, string catching_1, string catching2_1)
{
pictureBox7.Visible = true;
pictureBox7.Image = new Bitmap(Environment.CurrentDirectory + "\\" + "temp_data" + "\\" + catching2_1 + "\\" + catching_1);
pictureBox7.Refresh();
}
void pictureBox4_MouseLeave(object sender, EventArgs e)
{
pictureBox7.Visible = false;
pictureBox7.Image = null;
pictureBox7.Refresh();
}
void pictureBox5_Mouse(object sender, EventArgs e, string catching_2, string catching2_2)
{
pictureBox8.Visible = true;
pictureBox8.Image = new Bitmap(Environment.CurrentDirectory + "\\" + "temp_data" + "\\" + catching2_2 + "\\" + catching_2);
pictureBox8.Refresh();
}
void pictureBox5_MouseLeave(object sender, EventArgs e)
{
pictureBox8.Visible = false;
pictureBox8.Image = null;
pictureBox8.Refresh();
}
I am hoping someone can help me solve this issue, as i go to the next element in the list and hover over the new image i keep getting the error Parameter is not valid. i believe the picturebox is holding the previous variable.

Related

Where should i change the labels visible to true in the code?

private void downloadFile(IEnumerable<string> urls)
{
foreach (var url in urls)
{
_downloadUrls.Enqueue(url);
}
// Starts the download
btnStart.Text = "Downloading...";
btnStart.Enabled = false;
pBarFileProgress.Visible = true;
label2.Visible = true;
label3.Visible = true;
label4.Visible = true;
label7.Visible = true;
DownloadFile();
}
private void DownloadFile()
{
if (_downloadUrls.Any())
{
WebClient client = new WebClient();
client.DownloadProgressChanged += client_DownloadProgressChanged;
client.DownloadFileCompleted += client_DownloadFileCompleted;
url = _downloadUrls.Dequeue();
if (url.Contains("animated") && url.Contains("infra"))
{
string startTag = "animated/";
string endTag = "/infra";
int index = url.IndexOf(startTag);
int index1 = url.IndexOf(endTag);
fname = url.Substring(index + 9, index1 - index - 9);
var countryName = codeToFullNameMap[fname];
downloadDirectory = tbxMainDownloadPath.Text;
downloadDirectory = Path.Combine(downloadDirectory, countryName);
}
else
{
fname = "Tempfile";
downloadDirectory = tbxMainDownloadPath.Text;
}
client.DownloadFileAsync(new Uri(url), downloadDirectory + "\\" + fname + ".gif");
lastDownloadedFile = downloadDirectory + "\\" + fname + ".gif";
label4.Text = downloadDirectory + "\\" + fname + ".gif";
return;
}
// End of the download
label2.Text = "All files have been downloaded";
}
private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error != null)
{
// handle error scenario
throw e.Error;
}
if (e.Cancelled)
{
// handle cancelled scenario
}
if (url.Contains("animated") && url.Contains("infra"))
{
Image img = new Bitmap(lastDownloadedFile);
Image[] frames = GetFramesFromAnimatedGIF(img);
foreach (Image image in frames)
{
countFrames++;
image.Save(downloadDirectory + "\\" + fname + ".gif");
}
}
label2.Text = "Download Complete";
tracker.NewFile();
DownloadFile();
}
void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
tracker.SetProgress(e.BytesReceived, e.TotalBytesToReceive);
pBarFileProgress.Value = (int)(tracker.GetProgress() * 100.0);
label3.Text = e.BytesReceived + "/" + e.TotalBytesToReceive;
label7.Text = tracker.GetBytesPerSecondString();
label2.Text = "Downloading";
}
The start download button
private void btnStart_Click(object sender, EventArgs e)
{
downloadFile(lines);
}
Now I'm changing the labels visible property to true when I click the download button. But then the I see labels for some seconds before it's getting to the progressChanged event and update the labels.
The question is where should I change the visible to true of this labels 2,3,4,7 and that it will change it once to true ?
If you're concerned about showing the labels before you actually set any text to them, you should just make sure to set text to them before you get the progress changed event - e.g.
label3.Text = "0/calculating...";
label7.Text = "0 kbps";
label2.Text = "Preparing for download...";
in your downloadFiles method (before or after setting .Visible to true). On another note - pick better names for your labels! label3 might be better as something like lblTotalDownloaded, or label7 as lblDownloadSpeed, etc.

c# BackgroundWorker and Treeview in winform

Env: C#, VStudio 2013, 4.5 Framework, Winforms
Goal : Insert, inside a Treeview, the content of a folder (Sub folder + files) so that user can select those needed. Display a progressbar that show the progress of loading the files and folders in the TreeView.
What i've done so far : Everything in my goal but ...
Error: "This BackgroundWorker is currently busy and cannot run multiple tasks concurrently". I get this error sometimes when I go use other apps while my application is running.
My code :
void backgroundWorkerTreeView_DoWork(object sender, DoWorkEventArgs e)
{
var progress = (((float)(int)e.Argument / (float)totalFilesInTN) * 100);
var value = (int)Math.Round((float)progress);
backgroundWorkerTreeView.ReportProgress(value, e.Argument.ToString());
}
void backgroundWorkerTreeView_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
stStripBarMain.Value = e.ProgressPercentage;
toolStripStatusLabelPrct.Text = " Loading " + e.UserState + " of " + totalFilesInTN;
}
void backgroundWorkerTreeView_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//do the code when bgv completes its work
}
private void ListDirectory(TreeView treeView, string path)
{
try
{
treeView.Nodes.Clear();
if (path != "")
{
var rootDirectoryInfo = new DirectoryInfo(path);
treeView.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo, 0));
}
}
catch (Exception e)
{
txtLog.Text = txtLog.Text + "[" + DateTime.Now + "] " + e.Message + "\r\n";
}
}
private TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo, int indice)
{
var directoryNode = new TreeNode(directoryInfo.Name);
foreach (var directory in directoryInfo.GetDirectories())
{
if ((directory.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
{
directoryNode.Nodes.Add(CreateDirectoryNode(directory, indice));
}
}
foreach (var file in directoryInfo.GetFiles())
{
if ((IsNullOrEmpty(ext)) ||
(Array.IndexOf(ext, Path.GetExtension(file.Name.ToLowerInvariant())) >= 0))
{
if ((GetTriggerEvent(file.FullName).Contains(txtParamEvent.Text)) || (txtParamEvent.Text == ""))
{
indice++;
backgroundWorkerTreeView.RunWorkerAsync(indice);
TreeNode newTN = new TreeNode();
newTN.Text = file.Name + #" (" + GetTriggerEvent(file.FullName) + #")";
newTN.Name = file.FullName;
directoryNode.Nodes.Add(newTN);
newTN.Tag = "msg";
}
}
Application.DoEvents();
}
return directoryNode;
}
Thanks for the help
Richard

dynamically create a button

In Visual Studio C# windows form how do I dynamically create a button each time an item is inserted into a listbox ?
When the created button is clicked it has to remove the inserted item from the listbox.
I want to add the button th this:
if (comboBox4.Text != "" && listBox1.Text != "" && comboBox3.Text != "")
{
string ha = listBox1.SelectedItem.ToString();
Clipboard.SetText(comboBox4.Text + "stk " + ha + " i farve " + comboBox3.Text);
listBox2.Items.Add(comboBox4.Text + "stk " + listBox1.SelectedItem.ToString() +
" " + comboBox3.Text);
}
Create a button and add to the form.
Pseudo-code (not the complete solution, only the main login behind it):
public class MyEventArgs: EventArgs
{
public ListBoxValue lbv;
}
...
listBox2.Items.Add(comboBox4.Text + "stk " + listBox1.SelectedItem.ToString() + " " + comboBox3.Text);
Button x = new Button();
x.Text = "whatever";
x.Top = some coordinate;
x.Left = some coordinate;
MyEventArgs me = new MyEventArgs();
me.lbv = inserted lisbox item reference;
x.Click += new ClickHandler(this, me);
myForm.Controls.Add(x);
...
private void ClickHandler(object sender, MyEventArgs e)
{
listbox.Items.Remove(e.lbv);
}

Button click Open richTextBox and display the readfile

I have 2 buttons and I read different files when I click on these buttons. I used the to display the readfile using MsgBox since the files are big, so i want to display it in a richTextBox.
How can I open a richTextBox and display the read file when I click on any one of these buttons???
private void button1_Click(object sender, EventArgs e)
{
DisplayFile(FileSelected);//DisplayFile is the path of the file
var ReadFile = XDocument.Load(FileSelected); //Read the selected file to display
MessageBox.Show("The Selected" + " " + FileSelected + " " + "File Contains :" + "\n " + "\n " + ReadFile);
button1.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
FileInfo file = (FileInfo)comboBox2.SelectedItem;
StreamReader FileRead = new StreamReader(file.FullName);
string FileBuffer = FileRead.ReadToEnd(); //Read the selected file to display
//MessageBox.Show("The Selected" + " " + file + " " +"File Contains :" + "\n " + "\n " + FileBuffer);
// richTextBox1.AppendText("The Selected" + " " + file + " " + "File Contains :" + "\n " + "\n " + FileBuffer);
//richTextBox1.Text = FileBuffer;
}
Is there any other way to do it?
Here is a simple example (code based form design). It's better if you create the form via the GUI designer:
private void button1_Click(object sender, EventArgs e)
{
//test call of the rtBox
ShowRichMessageBox("Test", File.ReadAllText("test.txt"));
}
/// <summary>
/// Shows a Rich Text Message Box
/// </summary>
/// <param name="title">Title of the box</param>
/// <param name="message">Message of the box</param>
private void ShowRichMessageBox(string title, string message)
{
RichTextBox rtbMessage = new RichTextBox();
rtbMessage.Text = message;
rtbMessage.Dock = DockStyle.Fill;
rtbMessage.ReadOnly = true;
rtbMessage.BorderStyle = BorderStyle.None;
Form RichMessageBox = new Form();
RichMessageBox.Text = title;
RichMessageBox.StartPosition = FormStartPosition.CenterScreen;
RichMessageBox.Controls.Add(rtbMessage);
RichMessageBox.ShowDialog();
}

how do I disable the cache on for a single image (a single page)?

ASP.NET Image Caching Problem (How to Disable it?).IE browser load old image from directory C:\Documents and Settings...\Local Settings\Temporary Internet Files
protected void Page_Load(object sender, EventArgs e)
{
MembershipUser user = Membership.GetUser();
imCropped.ImageUrl = (File.Exists(Server.MapPath("..") + #"\Users\" + user.ProviderUserKey.ToString() + #"\" + user.ProviderUserKey.ToString() + ".gif"))? "~/Users/" + user.ProviderUserKey.ToString() + "/" + user.ProviderUserKey.ToString() + ".gif" : "~/Images/thumb.gif";
}
protected void ButtonJcrop_Click(object sender, EventArgs e)
{
...
String mapPath = #"\Users\" + user.ProviderUserKey.ToString() + #"\" + user.ProviderUserKey.ToString() + ".gif";
bmpCropped.Save(Server.MapPath("..") + mapPath);
imCropped.ImageUrl = Request.ApplicationPath + mapPath;
...
}
you can add to the img url aquery string param with just a random numbers :
<img src="http://www.walaa.co.il/t.jpg?c=2342342"/>
it can be like Datetime.Ticks or something else.
ImageUrl = (File.Exists(string.Format("{0}\\Users\\{1}\\{1}.gif?c={2}", Server.MapPath(".."), user.ProviderUserKey.ToString(), DateTime.Now.Ticks))) ? string.Format("~/Users/{0}/{0}.gif?c={1}" , user.ProviderUserKey.ToString(), DateTime.Now.Ticks) : "~/Images/thumb.gif";

Categories