?
Solved

Changing picture box in C# Part 2

Posted on 2009-04-24
9
Medium Priority
?
2,678 Views
Last Modified: 2013-12-17
Hello,
I have a application that has an animation that runs every 2 minutes to remind users to use the application, this was done in the animation creation of the .gif.  What I want to have happen is when a user clicks the submit button another image comes up for 5 to 10 seconds and then reverts back to the original place image.  
I have followed the instructions from a previous post
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CSharp/Q_24290678.html

however I am getting context errors and such listed
referencing the image.  what am I missing?

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.Collections;
using System.Xml;
using System.ServiceModel;
using System.Net;
 
namespace Trigger_Tracker
{
    public partial class Form1 : Form
    {//form move on click and drag
        bool FormMoving;
        Point initialPoint;
        TriggerTrackerPictureBox frmPicture;
 
        public Form1()
        {//form move on click and drag
            InitializeComponent();
            comboBox1.SelectedIndex = 0;
            FormMoving = false;
 
            frmPicture = new TriggerTrackerPictureBox();
            frmPicture.localForm = this;
            frmPicture.Owner = this;
            frmPicture.Show();
            frmPicture.Width = 68;
            frmPicture.Height = 65;
            SetPositionOfPictureForm();
            // Load the images into memory
            imageMain = Image.FromFile(@"\\AZ2KNAS01\data\AZ-Service-Projects\Andrew's Programs and Data\Trigger Tracker\Trigger Tracker MOD\Trigger Tracker\Resources\AnimatedGecko2.gif");
            imageTemp = Image.FromFile(@"\\AZ2KNAS01\data\AZ-Service-Projects\Andrew's Programs and Data\Trigger Tracker\Trigger Tracker MOD\Trigger Tracker\Resources\havaWINK_ANIMATED.gif");
            // Set the picture box main image here
            TriggerTrackerPictureBox.Image = imageMain;
            // Set the timer interval to 5 seconds, each value 1000 = 1 second.
            timer1.Interval = 5000;
 
        }
 
        private void SetPositionOfPictureForm()
        {
            frmPicture.Top = this.Top + 26;
            frmPicture.Left = this.Left + 87;
        }
 
        private void TriggerTrackerLoad(object sender, EventArgs e)
        {
            // Load the images into memory
            imageMain = Image.FromFile(@"C:\temp\imageMain.jpg");
            imageTemp = Image.FromFile(@"C:\temp\imageTemp.jpg");
            // Set the picture box main image here
            TriggerTrackerPictureBox.Image = imageMain;
            // Set the timer interval to 5 seconds, each value 1000 = 1 second.
            timer1.Interval = 5000;
 
        }
 
        private void PolicyNumberLabel(object sender, EventArgs e)
        {
 
        }
 
        private void TrackerButton(object sender, EventArgs e)
        {
            string listGUID = "A93E1A7E-67D0-4D7D-A4ED-803D7DFE684B";
            string viewGUID = "6B2F3EF2-4B0C-41E1-B87E-0C3185B587DD";
            //string viewGUID2 = "6B2F3EF2-4B0C-41E1-B87E-0C3185B587DD";
 
            int ItemCounter = 1;
            ServiceList.Lists listService = new ServiceList.Lists();
           // RetentionLists.ListsSoapClient listService = new RetentionLists.ListsSoapClient();
 
            //////
           listService.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
           //listService.ChannelFactory.Credentials.Windows.ClientCredential   = System.Net.CredentialCache.DefaultNetworkCredentials;
 
            XmlNode activeItemData = listService.GetListItems(listGUID, viewGUID, null, null, "100", null);
            XmlDocument xDoc = new XmlDocument();
            string tmpString = activeItemData.InnerXml.Replace("\r\r", "");
            xDoc.LoadXml(tmpString);
            XmlNamespaceManager nsManager = new XmlNamespaceManager(xDoc.NameTable);
            nsManager.AddNamespace("z", "#RowsetSchema");
            nsManager.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
 
            XmlNodeList xNode = xDoc.SelectNodes("/rs:data/z:row", nsManager);
 
            foreach (XmlNode tmpNode in xNode)
                ItemCounter++;
 
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.Append("<Method ID='" + ItemCounter + "' Cmd='New'>");
            strBuilder.Append("<Field Name='Attachments'>" + "0" + "</Field>");
            strBuilder.Append("<Field Name='Title'>" + PolicyNumber.Text + "</Field>");
            strBuilder.Append("<Field Name='Reason'>" + comboBox1.Text + "</Field>");
            strBuilder.Append("</Method>");
 
            string strBatch = strBuilder.ToString();
 
            XmlDocument newDoc = new XmlDocument();
            XmlElement newElement = newDoc.CreateElement("Batch");
            newElement.SetAttribute("OnError", "Continue");
            newElement.SetAttribute("ViewName", viewGUID);
            newElement.InnerXml = strBatch;
            
            XmlNode returnNode = listService.UpdateListItems(listGUID, newElement);
 
            this.comboBox1.Text = "Please Select....";
            this.PolicyNumber.Text = "";
            this.PolicyNumber.Mask = "0000000000";
 
            comboBox1.Focus();
            // After the set interval this method is called by the system
            // Disable the timer from triggering again
            TriggerTrackerPictureBox.Enabled = false;
            // Reset the image back to the main image
            TriggerTrackerPictureBox.Image = imageMain;
 
        }
 
        private void PolicyNumberTextBox(object sender, EventArgs e)
        {
 
        }
 
 
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            
        }
 
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {           
            
        }
 
        private void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
 
        }
 
 
        public void Form1_MouseUp(object sender, MouseEventArgs e)
        {//form move on click and drag
            FormMoving = false;
        }
 
        public void Form1_MouseMove(object sender, MouseEventArgs e)
        {//form move on click and drag
            if (FormMoving)
            {
                if ((Left + e.X - initialPoint.X) <= 0)
                    Left = 0;
                else if ((Right + e.X - initialPoint.X) >= Screen.PrimaryScreen.Bounds.Right)
                    Left = Screen.PrimaryScreen.Bounds.Right - Width;
                else
                    Left = Left + e.X - initialPoint.X;
                if ((Top + e.Y - initialPoint.Y) <= 0)
                    Top = 0;
                else if ((Bottom + e.Y - initialPoint.Y) >= Screen.PrimaryScreen.Bounds.Bottom)
                    Top = Screen.PrimaryScreen.Bounds.Bottom - Height;
                else
                    Top = Top + e.Y - initialPoint.Y;
            }
            SetPositionOfPictureForm();
        }
 
        public void Form1_MouseDown(object sender, MouseEventArgs e)
        {//form move on click and drag
            FormMoving = true;
            initialPoint = new Point(e.X, e.Y);
        }
 
        public void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {//form move on click and drag
            FormMoving = true;
            initialPoint = new Point(e.X, e.Y);
        }
 
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
        }
 
        private void pictureBox1_MouseHover(object sender, EventArgs e)
        {
        }
 
        public void label1_MouseHover(object sender, EventArgs e)
        {
            FormMoving = false;
        }
 
        public void label1_MouseUp(object sender, MouseEventArgs e)
        {
            FormMoving = false;
        }
 
        private void label1_MouseDown(object sender, MouseEventArgs e)
        {
 
        }
 
        private void label1_MouseMove(object sender, MouseEventArgs e)
        {
 
        }
 
        public void Form1_MouseHover(object sender, EventArgs e)
        {
            this.Opacity = 1;
        }
 
        public void Form1_MouseLeave(object sender, EventArgs e)
        {
            if(!PolicyNumber.Focused)
                this.Opacity = .25;
        }
 
        public void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            this.Opacity = 1;
        }
 
        public void pictureBox1_MouseHover_1(object sender, EventArgs e)
        {
            this.Opacity = 1;
        }
 
        public void Form1_Click(object sender, EventArgs e)
        {
            comboBox1.Focus();
        }
 
        private void timer1_Tick(object sender, EventArgs e)
        {
            // After the set interval this method is called by the system
            // Disable the timer from triggering again
            TriggerTrackerPictureBox.Enabled = false;
            // Reset the image back to the main image
            TriggerTrackerPictureBox.Image = imageMain;
 
        }
    }
}

Open in new window

0
Comment
Question by:Andrew Bass
  • 5
  • 4
9 Comments
 
LVL 15

Accepted Solution

by:
oobayly earned 2000 total points
ID: 24227875
What do you mean by "context errors"?

Personally, I'd just do this in a 2nd thread; no need for Timers etc.
BeginInvoke is needed as you're updating the UI from a different thread.
private void button_Click(object sender, EventArgs e){
  System.Threading.Thread td = new System.Threading.Thread(ChangeImage);
  td.Start();
}
 
private void ChangeImage(){
  // Change the image
  TriggerTrackerPictureBox.BeginInvoke(new MethodInvoker(delegate{
    TriggerTrackerPictureBox.Image = imageTemp;
  }));
 
  // Sleep for 5 seconds
  System.Threading.Thread.Current.Sleep(5000);
 
  // Change the image back
  TriggerTrackerPictureBox.BeginInvoke(new MethodInvoker(delegate{
    TriggerTrackerPictureBox.Image = imageMain;
  }));
}

Open in new window

0
 
LVL 1

Author Comment

by:Andrew Bass
ID: 24228246
Is there any changes in properties or controls that need to be added in this way?
0
 
LVL 15

Expert Comment

by:oobayly
ID: 24229991
Can't see any changes that need to be made, other than the fact it would make the timer redundant. Does the multithreaded version fit your needs and work the way expected?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Andrew Bass
ID: 24230316
I pasted the code but I am having some troubles getting it to react without errors.  It seems to dislike the word image and when i replace it with the reference it gives me a context error.  
0
 
LVL 15

Expert Comment

by:oobayly
ID: 24231462
What do you mean by a "Context error", is it a compile time error, or does it occur at runtime. What's the error message.
When talking about errors in .net, it's best to post the Exception type (ie. ArgumentNullException) and the exception message. This way it's possible for us to determine what is causing the error.
0
 
LVL 1

Author Comment

by:Andrew Bass
ID: 24242983
thanks oobayly I have a busy morning so I will post that error later today.   Thank you for your help
0
 
LVL 1

Author Comment

by:Andrew Bass
ID: 24244308
Errors Received

Error     1          The name 'imageMain' does not exist in the current context           \\AZ2KNAS01\data\AZ-Service-Projects\Andrew's Programs and Data\Trigger Tracker\Trigger Tracker MOD\Trigger Tracker\Form1.cs          35         13            Trigger Tracker

Error     2          The name 'imageTemp' does not exist in the current context          \\AZ2KNAS01\data\AZ-Service-Projects\Andrew's Programs and Data\Trigger Tracker\Trigger Tracker MOD\Trigger Tracker\Form1.cs          36         13            Trigger Tracker

Error     3          'Trigger_Tracker.TriggerTrackerPictureBox' does not contain a definition for 'Image'   \\AZ2KNAS01\data\AZ-Service-Projects\Andrew's Programs and Data\Trigger Tracker\Trigger Tracker MOD\Trigger Tracker\Form1.cs           38         38            Trigger Tracker

Error     4          The name 'imageMain' does not exist in the current context           \\AZ2KNAS01\data\AZ-Service-Projects\Andrew's Programs and Data\Trigger Tracker\Trigger Tracker MOD\Trigger Tracker\Form1.cs          38         46            Trigger Tracker

Error     5          The name 'imageMain' does not exist in the current context           \\AZ2KNAS01\data\AZ-Service-Projects\Andrew's Programs and Data\Trigger Tracker\Trigger Tracker MOD\Trigger Tracker\Form1.cs          53         13            Trigger Tracker

Error     6          The name 'imageTemp' does not exist in the current context          \\AZ2KNAS01\data\AZ-Service-Projects\Andrew's Programs and Data\Trigger Tracker\Trigger Tracker MOD\Trigger Tracker\Form1.cs          54         13            Trigger Tracker

Error     7          'Trigger_Tracker.TriggerTrackerPictureBox' does not contain a definition for 'Image'   \\AZ2KNAS01\data\AZ-Service-Projects\Andrew's Programs and Data\Trigger Tracker\Trigger Tracker MOD\Trigger Tracker\Form1.cs           56         38            Trigger Tracker

Error     8          The name 'imageMain' does not exist in the current context           \\AZ2KNAS01\data\AZ-Service-Projects\Andrew's Programs and Data\Trigger Tracker\Trigger Tracker MOD\Trigger Tracker\Form1.cs          56         46            Trigger Tracker

 
0
 
LVL 15

Expert Comment

by:oobayly
ID: 24261444
Sorry, been manic in the office.
I'd assumed that imageTemp & imageMain have been defined in the class. Also I'd assumed from the name that TriggerTrackerPictureBox is a PictureBox control, but as far as I can tell it's a type.
0
 
LVL 1

Author Closing Comment

by:Andrew Bass
ID: 31574341
Thanks!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question