How do I handle right arrow button keyup event to cycle to next image at full screen?

  • Thread starter Thread starter SlenderMan564
  • Start date Start date
S

SlenderMan564

Guest
Hi,

I am trying to display the next image from my imagelist while on my frmFullScreen form by handling the keyup event if the user hits the right arrow key except that I don't know how to display the next image.

The issue is frmFullScreen in frmFullScreen_keyUp. So how can I select the next image and display it, please? I'm not sure how to go about it.

BTW my ImageData class just holds the properties and a constructor where intCounter is being set to 0.

Here is the code for frmMain. Sorry for it's length is it too much? Should I shorten it in future?

using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web.UI;
using System.Collections.Generic;
using FullScreen;
using Data;

namespace PictureViewer
{
public partial class frmMain : Form
{

frmFullScreen _objFrmFullScreen = new frmFullScreen();
ImageData _objImgData = new ImageData();

//Displays larger instance of selected image in picture box.
private void lstImages_SelectedIndexChanged(object sender, EventArgs e)
{
//FOR i is less than the first image.
for (int i = 0; i < lstImages.SelectedItems.Count; i++)
{
//GET filename from listview and store in index.
_objImgData.strFileName = lstImages.SelectedItems.Text;
//Create larger instance of image.
pictureBox1.Image = Image.FromFile(_objImgData.strFileName);
//Fill panel to the width and height of picture box.
panel1.AutoScrollMinSize = new Size(pictureBox1.Image.Width, pictureBox1.Image.Height);
_objImgData.intImageIndex = lstImages.SelectedIndices[0];
loadImageMetaData();
}
}

private void mnuOpen_Click(object sender, EventArgs e)
{
loadImageList();
_objImgData.intImageIndex = 0;
}

private void btnNext_Click(object sender, EventArgs e)
{
try
{
if (pictureBox1.Image != null)
{
if (_objImgData.intImageIndex < StaticImageList.Instance.GlobalImageList.Images.Count)
{
lstImages.Items[_objImgData.intImageIndex].Selected = false;
_objImgData.intImageIndex++;
lstImages.Items[_objImgData.intImageIndex].Selected = true;
lstImages.Select();
}
}

else
{
MessageBox.Show(_objImgData.strMessage);
}
}
catch (Exception ex)
{
Debug.WriteLine("ERROR: " + ex.Message);
}

finally
{
if (_objImgData.intImageIndex >= StaticImageList.Instance.GlobalImageList.Images.Count)
{
_objImgData.intImageIndex--;
}
}
}

private void btnFullScreen_Click(object sender, EventArgs e)
{
//IF picturebox contains an image.
if (pictureBox1.Image != null)
{
_objFrmFullScreen._objImageData = _objImgData;
_objFrmFullScreen.ShowDialog();
}
else
{
MessageBox.Show(_objImgData.strMessage);
}
}

public void loadImageList()
{
_objImgData.intCounter= 0;
StaticImageList.Instance.GlobalImageList.Images.Clear();
lstImages.Clear();

oFD1.InitialDirectory = "C:\\";
oFD1.Title = "Open an Image File";
oFD1.Filter = "JPEGS|*.jpg|GIFS|*.gif|PNGS|*.png|BMPS|*.bmp";

//Open Dialog Box.
var oldResults = oFD1.ShowDialog();

if (oldResults == DialogResult.Cancel)
{
return;
}

//GET amount of filenames.
int num_of_files = oFD1.FileNames.Length;
//Temp array i created to solve the error.
//Store filenames in string array.
List<string> arryFilePaths = new List<string>(num_of_files);

foreach (string single_file in oFD1.FileNames)
{
arryFilePaths.Add(single_file);
StaticImageList.Instance.GlobalImageList.Images.Add(single_file, Image.FromFile(single_file));
_objImgData.intCounter++;
}
//BIND image list to listview.
lstImages.LargeImageList = StaticImageList.Instance.GlobalImageList;


for (int i = 0; i < _objImgData.intCounter; i++)
{
//DISPLAY filename and image from image index param.
lstImages.Items.Add(arryFilePaths, i);
}
}
}
}

Here is my code for frmFullScreen:

using Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;


namespace FullScreen
{
public partial class frmFullScreen: Form
{
public Data.ImageData _objImageData = new Data.ImageData();
bool _flag = true;

private void frmFullScreen_Load(object sender, EventArgs e)
{
GoFullscreen(_flag);
pictureBox1.ImageLocation = _objImageData.strFileName;
}

private void frmFullScreen_keyUp(object sender, KeyEventArgs e)
{
try
{
if (e.KeyData == Keys.Escape)
{
DialogResult = DialogResult.Cancel;
Close();
}

else if (e.KeyData == Keys.Right)
{

if (pictureBox1.Image != null)
{
if (_objImageData.intImageIndex < StaticImageList.Instance.GlobalImageList.Images.Count)
{
_objImageData.intImageIndex++;
GoFullscreen(_flag);
}
}
}
else if(pictureBox1.Image == null)
{
MessageBox.Show(_objImageData.strMessage);
}
}

catch (Exception ex)
{
Debug.WriteLine("ERROR: " + ex.Message);
}
}

#region Methods

/// <summary>
/// Hides everything and displays black background.
/// Low coupled and highly cohesive.
/// </summary>
/// <param name="fullscreen"></param>
private void GoFullscreen(bool fullscreen)
{
if (fullscreen)
{
this.WindowState = System.Windows.Forms.FormWindowState.Normal;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
this.BackColor = System.Drawing.Color.Black;
}
else
{
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
}
}
}
}

Continue reading...
 
Back
Top