Sign in to follow this  
namingway

C# help

Recommended Posts

namingway    114
I have a menu item which I want to load an image from file into a picture box. Also is it possible to add scrollbars to picture boxes or will I need a different component?

Share this post


Link to post
Share on other sites
cignox1    735
Quote:
Original post by namingway
I have a menu item which I want to load an image from file into a picture box.
Also is it possible to add scrollbars to picture boxes or will I need a different component?


I don't understand your first question. About the second one, you can add the picturebox into a panel with autoscroll enabled. That should make scrollbars appear as soon as the picture becomes larger than the containing panel...

EDIT: if you're asking how to ask the user for a file, you need to hasndle the event fired when a click on the menu item is performed, then you must create and show an OpenFileDialog and get the selected file. With the path you can then create a Bitmap with the name as the parameter of the constructor.

Share this post


Link to post
Share on other sites
superpig    1825
Your language is a little unclear. Do you want to:

1) Load the image from the file and display it in the picturebox
2) Save the image from the picturebox to the file

For both options, you begin by hooking up an event to the menu item. In the code for that event, you then create an OpenFileDialog or a SaveFileDialog, use it to get the filename, and then either create a bitmap from that file and set it into the picturebox, or get the image from the picturebox and save it to that file.

You're going to have to ask more specific questions if you want more specific help.

Share this post


Link to post
Share on other sites
namingway    114
Again sorry, I've been in a rush. At this point in time all I'd like to know is how to store the image e.g. .bmp, .png chosen from an open file dialog file and then assign it to a picture box.

Share this post


Link to post
Share on other sites
namingway    114
oh nevermind got it.

A new question, how can I set a limit on the dimensions of the loaded image, say if the image isnt 300x800 then it won't load.

Share this post


Link to post
Share on other sites
namingway    114
simple enough, okay now I have this so far; ofd = openFileDialog ;)

private void loadTilesetToolStripMenuItem_Click(object sender, EventArgs e)
{
ofd.ShowDialog();
_tileset = new Bitmap(ofd.FileName);
if (_tileset.Width == 256 && _tileset.Width == 640)
picTileset.Image = _tileset;
else
{
TilesetError err = new TilesetError();
err.ShowDialog();
}
}



Problem is if the user clicks cancel it brings up the error message as well as if the dimensions arent met, how can I fix it so that if the user cancels the openfile dialog no error will show up?

Thanks for the help so far it is much appreciated.

Share this post


Link to post
Share on other sites
namingway    114
cheers, I have yet another question...
I have dialog for when the user clicks File -> new , this dialog takes in 3 values, how can I transfer the values over to the main form?
I need numHeight, numWidth to be set to the mainForms _columns and _rows properties.

Share this post


Link to post
Share on other sites
JamesLewis    234
Hi,

An easy way would be to create a couple of properties in your class for the file->new dialog which you could check when the user hits OK. If there's something in them then assign them to your form's properties.

So part of the file->new dialog might look like this:

...

// The properties
public int width
{
get{ return int.parse(this.widthTextBox.Text); }
}
public int height
{
get{ return int.parse(this.heightTextBox.Text); }
}


// Handler for OK button
private void OkButtonClicked
{
// Set the dialog result
this.DialogResult = DialogResult.OK;
}
...



And then the handler for file->new in your main form might look like this:


private void FileNewHandler(...)
{
// Create a new fileNew dialog
fileNewDialog d = new fileNewDialog();

// Check if the user hit the OK button
if( d.ShowDialog == DialogResult.OK )
{
// Set the properties (might want to check for empty values here)
this.columns = d.width;
this.rows = d.height;
}
}



You'll need to adapt the above code for your project but something similar to the above should get you going.

Cheers,

James

Share this post


Link to post
Share on other sites
id0001    122
private void loadTilesetToolStripMenuItem_Click(object sender, EventArgs e)
{
ofd.ShowDialog();
_tileset = new Bitmap(ofd.FileName);
if (_tileset.Width == 256 && _tileset.Width == 640)
picTileset.Image = _tileset;
else
{
TilesetError err = new TilesetError();
err.ShowDialog();
}
}



Im not sure about this because nobody posted this bug.

Its just a minor bug btw, you probably already seen it, but when you check for the width of _tileset it always returns false because the widht cannot be 256 AND 640

or am I wrong here?

greetz

ID

Share this post


Link to post
Share on other sites
namingway    114
lol I noticed it when I made a later post, thanks anyway though :)
I have yet another problem, when I open up another dialog for editing the width and height, I'd like to pass the main forms _columns and _rows value into numericUp Downs of the dialog box, that ways users will know how big it is presently

Share this post


Link to post
Share on other sites
JamesLewis    234
Hi,

Do you just want to pass the values from the form to the new dialog box? Could you not just pass the variables into the constructor? Or set a couple of properties before showing the dialog box?

James

Share this post


Link to post
Share on other sites
namingway    114
Okay I have yet another problem now...

I'm trying to play a file with a simple play button however nothing happens.
However when I load the sound file it works fine..
--The following is in the create new map dialog--

private void btnMusic_Click(object sender, EventArgs e)
{
if (ofdSound.ShowDialog() == DialogResult.OK)
{
_music.Open(ofdSound.FileName);
//_music.Play();
}
}


If that play button is uncommented it will play fine.


public string music
{
get { return _music.FileName; }
}



--This is in the main form--

if (newMap.ShowDialog() == DialogResult.OK)
{
this._columns = newMap.width;
this._rows = newMap.height;
this._name = newMap.mapname;
this.picTileset.Image = new Bitmap(newMap.tileset);
this._music = new Sound(newMap.music);
panelMap.Invalidate();
}



private void toolStripbtnPlay_Click(object sender, EventArgs e)
{
this._music.Play();
}



The Sound constructor is
public Sound(string filename)
{
_filename = filename;
}

if there's anythign else you need to see just ask.

Again basically I load an mp3 file and it will play if the _music.Play is uncommented but when I click the play button on the main form it does nothing.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this