Windows Develop Bookmark and Share   
 index > Windows Forms General > Windows Form color does not changes ,on checking radio_button
 

Windows Form color does not changes ,on checking radio_button

Coding Skill : Programmed in C++ , Knows OOPs concept.
VC# Skill:
Just started VC# coding , Novice
IDE : Microsoft Visual Studio 2005.
Code: C-Sharp , C#

I'm working on windows Forms ,on Radio_button Click code , I put the code

        private void radioButton1_CheckedChanged_1(object sender, EventArgs e)
{
//colorDialog1.Color = Color.LightSkyBlue;
obj_RoughPad.BackColor = Color.LightSkyBlue;
}
//obj_RoughPad is the forum object
//and I simply apply color to it's BackColor property

Compiler generates this Error:
Error "An object reference is required for the nonstatic field, method, or property 'System.Windows.Forms.Control.BackColor.get' "


Kindly give suggestion that why this is happening
let me know if you need more details.

  • Edited byAbaf Thursday, September 24, 2009 4:12 PM
  •  
Abaf  Thursday, September 24, 2009 3:49 PM

Post the definition of obj_RoughPad.

Also, double click on the error and make sure you've posted the code that the IDE takes you to when you double click the error.

Lastly, if there are other errors, post them too.


Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki �LinkedIn �ForumsBrowser
David M Morton  Thursday, September 24, 2009 3:51 PM
Thanks for Rapid Flash response...


  1. I have put the code in obj_RoughPad definition

here is the form1.cs code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class obj_RoughPad : Form
{
public obj_RoughPad()
{
InitializeComponent();
}

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{

}

private void newToolStripMenuItem_Click(object sender, EventArgs e)
{

}

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{

}

private void radioButton1_CheckedChanged_1(object sender, EventArgs e)
{ //thats the code which i wrote
//colorDialog1.Color = Color.LightSkyBlue;
obj_RoughPad.BackColor = Color.LightSkyBlue;
}

private void obj_RoughPad_Load(object sender, EventArgs e)
{

}
}
}
Compiler generates this Error:
Error "An object reference is required for the nonstatic field, method, or property 'System.Windows.Forms.Control.BackColor.get' "
Abaf  Thursday, September 24, 2009 3:58 PM
Try "this.BackColor = Color.LightSkyBlue"

obj_RoughPad is the class name, not the name of the instance. obj_RoughPad would work only if BackColor was a static property, which it's not. It's instance, so you need an instace.

The instance you're going to be working with from within obj_RoughPad's code should be the current instance, which is referenced in C# by using the "this" keyword.

By the way, research C# naming conventions. There's no need to have "obj_" preceding the class name. Call it "RoughPad".
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki �LinkedIn �ForumsBrowser
David M Morton  Thursday, September 24, 2009 4:01 PM
  1. Try "this. BackColor = Color.LightSkyBlue"
  2. obj_RoughPad is the class name, not the name of the instance. obj_RoughPad would work only if BackColor was a static property, which it's not. It's instance, so you need an instace.
  3. The instance you're going to be working with from within obj_RoughPad's code should be the current instance, which is referenced in C# by using the "this" keyword.
  4. By the way, research C# naming conventions. There's no need to have "obj_" preceding the class name. Call it "RoughPad".

  1. I have actually tried this before ,but it changes the BackColor of radio_button no use ,I want to change the color of background of form.
  2. Didn't get what you mean...?
  3. I know the this keyword ,already used that.
  4. Sure , I will check it out , MSDN
Abaf  Thursday, September 24, 2009 4:10 PM
"this.BackColor = Color.LightSkyBlue" in the context that you're in changes the background color of the radio button only? That makes no sense. "this" in the context in which you're in is the form, not the radio button.

Now, if you had done "this.radioButton1.BackColor = Color.LightSkyBlue" that would be a different matter.

2. This isan OOP concept. It's the difference between a class definition and an instance of the class.

3. Good. :)

4. OK. :)
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki �LinkedIn �ForumsBrowser
David M Morton  Thursday, September 24, 2009 4:25 PM
"this.BackColor = Color.LightSkyBlue" in the context that you're in changes the background color of the radio button only? That makes no sense. "this" in the context in which you're in is the form, not the radio button.

Now, if you had done "this.radioButton1.BackColor = Color.LightSkyBlue" that would be a different matter.

2. This isan OOP concept. It's the difference between a class definition and an instance of the class.

3. Good. :)

4. OK. :)
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki �LinkedIn �ForumsBrowser
  1. But that's what was happening ,so I make a new project file ,and then put two radio_buttons in in it ,both changing the from1.BackColor into different color ,it worked perfectly fine ,why my previous project did'nt worked with same code ,it's Strange...!
  2. Now, if you had done "this.radioButton1.BackColor = Color.LightSkyBlue" that would be a different matter. , I haven't done that , I Suppose you read my code in the posting :-)
Abaf  Thursday, September 24, 2009 4:43 PM

in Your code You have two subs...
1. private void radioButton1_CheckedChanged(object sender, EventArgs e) - with no code
2. private void radioButton1_CheckedChanged_1(object sender, EventArgs e) - with your code...
are You sure Your program rises the second one t.e. radioButton1_CheckedChanged_1
put the breakpoint in that and run the application...

Lucifer L.  Saturday, September 26, 2009 7:51 PM

You can use google to search for other answers

Custom Search

More Threads

• Error "Window class name is not valid."
• PageSetupDialog does not keep PaperSize
• Can't anyone help me with this? - "..timeout period elapsed prior to obtaining a connection from the pool..."
• IExtenderProvider not applying to all components
• managing databases...
• DropDownList, Autocomplete, Tab gives unexpected behavior
• Get multiple selected filenames from Window Explorer
• Keep deleted records in DataGrid
• clear combobox issue
• text box maxlength issue.