Link to home
Start Free TrialLog in
Avatar of Software Engineer
Software Engineer

asked on

C# name does not exist in the current context

C# is telling me that the name does not exist in the current context.

What do I do?
Avatar of Sam Jacobs
Sam Jacobs
Flag of United States of America image

Can you post the code where the error is being generated, and where you declare the name?
Avatar of Software Engineer
Software Engineer

ASKER

Hi Sam:

Thank you, for the quick response!

The code is below, and the error occurred after I added the following line:  

layoutControlItem7.Background = System.Drawing.Color.Red;

I'm trying to have a date field drop-down list display as red, upon opening a purchase order window.  Truthfully, the name "layoutControlItem7" is my best guess at the name of that field.  The app's layout "editor" tells me that that is the name of the field.  But, it's possible that it may simply be the name of the field's label.  Hopefully, it's giving me correct information.  :)


//PO OnLoad//

if (po.IsNew)
{
      po.val_Promised_Ship_Date = Genframe4.Utils.DateTimeExtensions.MinValue;
      po.val_Promised_Date = Genframe4.Utils.DateTimeExtensions.MinValue;
      po.val_Required_Date = Genframe4.Utils.DateTimeExtensions.MinValue;
        layoutControlItem7.Background = System.Drawing.Color.Red;
}

return "";
C# names are case-sensitive … are you sure that you have the case correct?

The dropdown and its label are 2 different fields. If you are on the dropdown in the editor (and not on the label), it should be telling you the correct name.
Again … be careful with the case.
The case is 100% correct.  Yes, I read that C# is case sensitive.

Yes, I right-clicked on the drop-down.  Unfortunately, the app does not pull up its layout editor.  It only chose "copy and paste" selections.

I only get the layout editor if I right-click in the label.  

Ideas?
John,

Are you trying to use the field on a different form than where it's defined?

-Sam
If it's not proprietary, can you post the .cs file containing it?
It is proprietary.  It's a "for sale" app that a client of ours purchased from a software developer that markets the product.

Is there no possibility that the layout editor's name for the label is, also, the name of the field?
Nope … you can't have 2 fields with the same name.
A label usually has a prefix of lbl or something like that (e.g. lblPODate).
Now that you mention it, layoutControlItem7 sounds like it's the name of a container that has the dropdown on it.
So the correct reference should be
layoutControlItem7.ddName.Background = ...
where ddName is the name of the dropdown.
That sounds right.  

Is there a way of deriving the name of the field, even from the other code that I pasted?
Not from the code above.
A dropdown is also called a ComboBox.
Open the .cs file and look for something like:
this.fieldname = new System.Windows.Forms.ComboBox();

fieldname is what you are looking for.
where would the cs file be?
now that I think about it, I think that I need to ask about the original error that I posted here.  the label that my syntax here presumably references should work.  what about just getting that label to work with this code.  that's a possibility isn't it?
Where you got the code above from.
I typed on the code in the app's code editor window.  so, I dont know where the file is.  now that I think about it, I think that I need to ask about the original error that I posted here.  the label that my syntax here presumably references should work.  what about just getting that label to work with this code.  that's a possibility isn't it?
Do you know the name of the label?
according to the app's layout editor, the name that I gave earlier is the name.
ASKER CERTIFIED SOLUTION
Avatar of Sam Jacobs
Sam Jacobs
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try BackColor instead of Background.
Good Morning, All:

I have just today found out from the software vendor that the name of the two drop-down list fields that I need to work with are  po.val_Promised_Ship_Date and po.val_Promised_Date.

I'm still having trouble color coding them, however.

Below is my revised code.  On the lines mentioning the color red, I'm now getting the following error:

'System.DateTime' does not contain a definition for 'Background' and no extension method 'Background' accepting a first argument of type 'System.DateTime' could be found (are you missing a using directive or an assembly reference?)

John

//PO OnLoad//

if (po.IsNew)
{
      po.val_Promised_Ship_Date = Genframe4.Utils.DateTimeExtensions.MinValue;
      po.val_Promised_Date = Genframe4.Utils.DateTimeExtensions.MinValue;
      po.val_Required_Date = Genframe4.Utils.DateTimeExtensions.MinValue;
      po.val_Promised_Ship_Date.Background = System.Drawing.Color.Red;
      po.val_Promised_Date.Background = System.Drawing.Color.Red;
}

return "";
Try BackColor instead of Background.
As Andy mentioned, it should be BackColor - not BackGround.
Actually, I tried BackColor also and got the same error.  I forgot to mention that and came here on the posting to mention it.  Then, I saw your post reminding me about what Andy suggested.

Again, I get the same error.
Also your error message is very odd.  
System.DateTime is not a control for on a form.  That sounds like the underlying variable for storing the data.
Sigh.....did I just get bad information from the software vendor?

Ay, ay, ay!
Not necessarily, depends just what you asked them.

First off have a look at your form in design mode.  Then select the control you want to change the back colour of.  Now look at properties and you should see the name of that control.  That is the name you need in your code.

Second.  There are some controls that changing the back colour is not so simple.

Third you can test your code.  Put a label onto the form eg. lblMyLabel.  Then in your code try lblMyLabel.BackColor = System.Color.Red.  Does the label appear with a red background?  Yes then your code is working in principle.
Hi There, Andy:

When I right-click on the field's label, the app displays the Customization window of the form.  That's where I derive "layoutControlItem7".  When I compile this in the app's Code Editor window, I get the error.

When I right-click on the drop-down list field itself, the app does not give me the option of going into the Customization window of the form.   I asked the vendor about this, and they gave me (as you said) the variable name.

The app will not let me add my own field.

I did e-mail the software vendor again to see if I can get any different information.  I guess we'll see.  :)

Thank you, for your help!

John
>When I right-click on the field's label, the app displays the Customization window of the form.

?? I don't understand that.
The "Customization window" that I speak of is similar to the screenshot that you provided.  That's where I found "layoutControlItem7".

But, again, when clicking "Compile" in the app's Code Editor I get "the name does not exist in the current context."
What happens with the following?

if (po.IsNew)
{
   /*   po.val_Promised_Ship_Date = Genframe4.Utils.DateTimeExtensions.MinValue;
      po.val_Promised_Date = Genframe4.Utils.DateTimeExtensions.MinValue;
      po.val_Required_Date = Genframe4.Utils.DateTimeExtensions.MinValue;
      po.val_Promised_Ship_Date.Background = System.Drawing.Color.Red;
      po.val_Promised_Date.Background = System.Drawing.Color.Red;
*/
layoutControlItem7.BackColor = System.Drawing.Color.Red;
}
name does not exist in the current context
Can you post a screenshot of this 'customization window' please
Hi Andy:

It's attached.

John
Customization.PNG
OK, scroll up that listing at the right and do you see anything beginning with Back
Also have a look in the hidden items tab.
Maybe click the button showing A above Z with a down arrow next to it (hopefully to remove the groupings and display things alphabetically)
Hi Andy:

The hidden items tab only gives you the chance to drag fields to that tab that you don't want.  

Yes, there are selections beginning with "Back".  But, none reference a field name or label.

In the screenshot, layoutControlItem7 is referenced.  Again, though, the script editor window of the app gives me the aforementioned error.

John
That listing to the right (I think) is showing what you can modify about the control that is selected.  If there is a BackCOlor what happens if you change it to red or blue or...

Am I correct that this form is something from a third party software that you are able to partially customise and embed in your own app?
OK, seen it is 3rd party.  
Just try to modify things on this customization page.  If it works there then you should be able to do it in code.
I can modify, sure.

But, it does not let me do so through the code.  That's why I'm here.  :)
Please explain in detail just where the code you want to run is in your app.
There's a script editor in the app that allows running code.  But, the code, is not working.  That's why I created this posting here on experts-exchange.
>>There's a script editor in the app that allows running code

Hmmm.  You implied you were running the code in the code for the form where this control was rather earlier (when asked if the control was on another form).
To access a control on a form from elsewhere you need a reference to an instance of that form to specify the control
eg. code like frmSomeOtherForm.lblABC.BackColor = System.Color.Red
????????????
Your code that you have will only work if you run it inside the form the control is on.  You earlier said no to a question is the code being run in another form to that the control is on.  (Probably technically correct answer because the code you have isn't within another form).

if you have something like

class frmXYZ
{
void DoSomething() {}

void DoSomethingElse()
{
  DoSomething();  <<--------- this works
}
}

if however you have

class frmXYZ
{
void DoSomething() {}
}

class pqr
{
void DoSomethingElse()
{
  DoSomething();  <<--------- this fails, typically with the sort of error meassage you have
}
}


You need code like the following
class pqr
{

void DoSomethingElse()
{
frmXYZ obj = new frmXYZ();
  obj.DoSomething();  <<--------- this runs the code in DoSomething with the object frmXYZ
}
}
That's not what I said, at all.  Or, if that's what I said, that's not at all what's reflected in what I have attempted.

I'm chalking this up the fact that it's proprietary software and cannot be fixed except for by those who developed it.
a question asked to you rather earlier by Sam:
John,

Are you trying to use the field on a different form than where it's defined?

-Sam


Your answer:  no


Note as I said your answer is probably technically correct
Don't forget you can modify the colour from the designer.  That does give one hope it can be done from code BUT you probably need to specify which form the control is on.

Try not to forget that the experts here can't read your mind.  What might seem very obvious to you is not necessarily the case.  Telling us things like what the software you are working with is third party plus its name could even result in you getting a useful answer immediately after the question is asked.
What Andy is saying is that all of the experts volunteer their time because we get satisfaction by helping others, and the more information (and screenshots are always most helpful!) we have, the quicker we can assist and get you up and running. When third party products are in use, we are at a disadvantage, because unless they are products with which we already have experience, we need even *more* information.