Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Trouble in visual studio express c# (newbie)


  • Please log in to reply
3 replies to this topic

#1 McBeeff

McBeeff

  • Members
  • 11 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:california
  • Local time:11:33 PM

Posted 21 January 2013 - 02:47 PM

I am very newb at this but I am having trouble with some basic coding

private void ShowPictureButton_Click(object sender, EventArgs e)
{
// Show the Open File dialog. if the user clicks ok load the
// picture that the user chose.
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
PictureBox.Load(OpenFileDialog.FileName);
}
}
private void Colorbutton_Click(object sender, EventArgs e)
{
// Show the color dialog. if the user clicks OK, change the
// picture box control's backround to the color the user chose
if (colorDialog1.ShowDialog() == DialogResult.OK)
PictureBox.BackColor = colorDialog1.Color;

Error 1 An object reference is required for the non-static field, method, or property 'System.Windows.Forms.PictureBox.Load(string)' C:\Users\jeff\Documents\Visual Studio 2012
\Projects\PictureViewer\PictureViewer\Form1.cs 56 17 PictureViewer

Error 2 An object reference is required for the non-static field, method, or property 'System.Windows.Forms.FileDialog.FileName.get' C:\Users\jeff\Documents\Visual Studio 2012\Projects\PictureViewer\PictureViewer\Form1.cs 56 33 PictureViewer

Error 3 An object reference is required for the non-static field, method, or property 'System.Windows.Forms.Control.BackColor.get' C:\Users\jeff\Documents\Visual Studio 2012\Projects\PictureViewer\PictureViewer\Form1.cs 64 17 PictureViewer


Any suggestions on this?

BC AdBot (Login to Remove)

 


#2 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:09:33 PM

Posted 21 January 2013 - 02:57 PM

PictureBox, FileDialog, and Control are classes, not objects. Your form could have 10 picture boxes on it -- how would PictureBox.Load(OpenFileDialog.FileName); know which PictureBox to use?

You need to reference the specific instances of PictureBox, FileDialog, and Control you are talking about. So your code should look something like:

private void ShowPictureButton_Click(object sender, EventArgs e)
{
    // Show the Open File dialog. if the user clicks ok load the
    // picture that the user chose.
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        // Was: PictureBox.Load(OpenFileDialog.FileName);
        pictureBox1.Load(openFileDialog1.FileName);
    }
}

private void Colorbutton_Click(object sender, EventArgs e)
{
    // Show the color dialog. if the user clicks OK, change the
    // picture box control's backround to the color the user chose
    if (colorDialog1.ShowDialog() == DialogResult.OK)
    {
        // Was PictureBox.BackColor = colorDialog1.Color;
        pictureBox1.BackColor = colorDialog1.Color;
    }
}

Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#3 McBeeff

McBeeff
  • Topic Starter

  • Members
  • 11 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:california
  • Local time:11:33 PM

Posted 21 January 2013 - 03:27 PM

Thank you for that quick response! I entered your correction in the code but now I'm getting another error that says PictureBox1 and OpenFileDialog1 does not exist in the current context.

Could this be a problem with the version of visual studio express that I am using?


Thank You again for your help!

Edited by McBeeff, 21 January 2013 - 03:31 PM.


#4 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:09:33 PM

Posted 21 January 2013 - 04:43 PM

No. You need to replace those names I used with whatever you actually called those items. It might be "fooPhotoBox" instead of "pictureBox1" -- I just guessed at the names you used.

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users