10 Replies - 6035 Views - Last Post: 27 January 2010 - 05:38 AM Rate Topic: -----

#1 roaster  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 06-October 09

Does not exist in current context: Error

Posted 26 January 2010 - 10:25 AM

Hi All:

I am working on a winforms app. I am experimenting with dynamic controls and have a SQL database backing the app. Here is the portion of my code that I am having a problem with:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Web;

namespace Real_Estate_Manager
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		   
		}

		private void button1_Click(object sender, EventArgs e)
		{
			btnUpdateProps.Visible = true;
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			// TODO: This line of code loads data into the 'propertyManagerDataSet.Property' table. You can move, or remove it, as needed.
			this.propertyTableAdapter.Fill(this.propertyManagerDataSet.Property);

		}

		public void btnUpdateProps_Click(object sender, EventArgs e)
		{   
		   string[] strPropTypes = new string[] { "New Build", "Renovation", "Rental" };

			ComboBox ddlPurpose = new ComboBox();
			ddlPurpose.Location = new Point(60, 300);
			ddlPurpose.Width = 150;
			ddlPurpose.Text = "Please Specify the Purpose";
			ddlPurpose.Items.AddRange(strPropTypes);
			this.Controls.Add(ddlPurpose);
			ddlPurpose.SelectedValue = new System.EventHandler(PropType);


		}

		public void PropType(object sender, EventArgs e)
		{
			if (ddlPurpose.SelectedValue = "New Build") { 
				
			}
		}
	}
}



My error is in the PropType Event, in the following line of code:
if (ddlPurpose.SelectedValue = "New Build") { 



The error states: "The name 'ddlPurpose' does not exist in the current context"

Basically, what I am trying to do is create an if statement or a switch statement in the PropType event. The condition statement will create more dynamic controls based on the selection of ddlPurpose.

If anyone has any suggestions for this that would be greatly appreciated.

Thank You,
roaster

Is This A Good Question/Topic? 0
  • +

Replies To: Does not exist in current context: Error

#2 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Does not exist in current context: Error

Posted 26 January 2010 - 10:44 AM

put the ddlPurpose declaration as a class variable

heres your first few lines of code:
namespace Real_Estate_Manager
{
	public partial class Form1 : Form
	{
		ComboBox ddlPurpose;
		public Form1()
		{
			ddlPurpose = new ComboBox();
			InitializeComponent();
		   
		}


Was This Post Helpful? 1
  • +
  • -

#3 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Does not exist in current context: Error

Posted 26 January 2010 - 10:46 AM

There are two problems. You need to have your ddlPurpose Combox has a field in the class, not as a variable inside your method. That is why you are having problems there, because it was a local variable and not part of the class the event handler was in.

In this method, you will want to check and make sure that the ddlPurpose combo box has been created before working with it.

		public void PropType(object sender, EventArgs e)
		{
			if (ddlPurpose.SelectedValue != null && ddlPurpose.SelectedValue = "New Build") {
			   
			}
		}



You can do the && part with out an exception being thrown because if the first part evaluates to false the second half will be ignored.
Was This Post Helpful? 1
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Does not exist in current context: Error

Posted 26 January 2010 - 10:49 AM

you can either do what SwiftStriker said, or you can use the "sender" as your control...

		public void PropType(object sender, EventArgs e)
		{
			ComboBox cb = sender as ComboBox;

			// don't forget the double equals sign
			if (cb.SelectedValue == "New Build") { 
				
			}
		}


Was This Post Helpful? 1
  • +
  • -

#5 roaster  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 06-October 09

Re: Does not exist in current context: Error

Posted 26 January 2010 - 01:41 PM

Is there someway that I need to set the ddlPurpose to autopostback, I am not sure if autopostback is the right term for winforms.

I changed the code like you suggested and it all seems to be compiling fine. My problem is that when "New Build" is selected in the ddlPurpose the PropType Event doesn't seem to fire. I know in web development, you would need to postback. What do you do in a Win Form app?

Thanks
roaster
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Does not exist in current context: Error

Posted 26 January 2010 - 01:47 PM

View Postroaster, on 26 Jan, 2010 - 04:41 PM, said:

Is there someway that I need to set the ddlPurpose to autopostback, I am not sure if autopostback is the right term for winforms.

I changed the code like you suggested and it all seems to be compiling fine. My problem is that when "New Build" is selected in the ddlPurpose the PropType Event doesn't seem to fire. I know in web development, you would need to postback. What do you do in a Win Form app?

Thanks
roaster


the "autopostback" is done automatically IF you wire the event...

this is your code

ddlPurpose.SelectedValue = new System.EventHandler(PropType);



which is wrong. you need to do this...

ddlPurpose.SelectedIndexChanged += new EventHandler(PropType);


Was This Post Helpful? 1
  • +
  • -

#7 roaster  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 06-October 09

Re: Does not exist in current context: Error

Posted 26 January 2010 - 01:54 PM

Ok, I made the change here:
 ddlPurpose.SelectedIndexChanged += new EventHandler(PropType);



I now get an error in my PropType event, if statement. Here is the code:
public void PropType(object sender, EventArgs e)
		{
			if (ddlPurpose.SelectedIndexChanged == 0) {



The error says: The event 'System.Windows.Forms.ComboBox.SelectedIndexChanged' can only
appear on the left hand side of += or -=.

Any suggestions?

Thanks,
roaster
Was This Post Helpful? 0
  • +
  • -

#8 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Does not exist in current context: Error

Posted 26 January 2010 - 02:05 PM

The field SelectedIndexChanged is an event. So, your attempt to inspect its value is going to fail.

I believe what you really want is the SelectedIndex property on that control.
Was This Post Helpful? 1
  • +
  • -

#9 roaster  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 06-October 09

Re: Does not exist in current context: Error

Posted 26 January 2010 - 02:08 PM

Awesome, it is working now.
I changed the if statement to:
ddlPurpose.SelectedIndex == 0
and it is working great now.

I want to thank everyone for their help on this.

Thanks,
Roaster
Was This Post Helpful? 0
  • +
  • -

#10 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Does not exist in current context: Error

Posted 26 January 2010 - 02:36 PM

Congratulations on implementing your feature.
Was This Post Helpful? 0
  • +
  • -

#11 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Does not exist in current context: Error

Posted 27 January 2010 - 05:38 AM

View Postroaster, on 26 Jan, 2010 - 05:08 PM, said:

Awesome, it is working now.
I changed the if statement to:
ddlPurpose.SelectedIndex == 0
and it is working great now.

I want to thank everyone for their help on this.

Thanks,
Roaster


I would suggest doing some studying on the differences between methods, properties, and events for controls, how to identify between them, and how to use them.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1