Solved

ERROR: The name 'textbox1' does not exist in the current context

Posted on 2008-10-25
13
6,755 Views
Last Modified: 2013-12-17
I ma trying this simple  program in Visual Studio 2005, however I get this error...here is my code:

public static void main(String[] args)
        {
            int txtbox1;
            string[] w = new string[3];
            w[0] = "Hello";
            w[1] = ",";
            w[3] = "world";
            for (int i = 0; i < 3; i++)
                textbox1.Text = w.ToString();
        }
0
Comment
Question by:GlobaLevel
  • 4
  • 3
  • 2
  • +3
13 Comments
 
LVL 3

Accepted Solution

by:
TechSinger earned 150 total points
ID: 22805370
Your line inside the loop is - textbox1.Text = w.ToString();

Are you sure you don't want to line to be - textbox1.Text = w[i];

Since an element of w is already a string, it shouldn't need to be converted to one.
0
 
LVL 13

Assisted Solution

by:sm394
sm394 earned 200 total points
ID: 22805403
try tht
public static void main(String[] args)
        {
            string txtbox1;
            string[] w = new string[3];
            w[0] = "Hello";
            w[1] = ",";
            w[3] = "world";
            for (int i = 0; i < 3; i++)
                textbox1= w.ToString();
        }
0
 
LVL 13

Assisted Solution

by:sm394
sm394 earned 200 total points
ID: 22805410
public static void main(String[] args)
        {
            string txtbox1;
            string[] w = new string[3];
            w[0] = "Hello";
            w[1] = ",";
            w[3] = "world";
            for (int i = 0; i < 3; i++)
                textbox1+= w[i].ToString();
        }
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 3

Assisted Solution

by:TechSinger
TechSinger earned 150 total points
ID: 22806131
I didn't notice the line - int txtbox1; in your code.  I was just assuming that txtbox1 was the name of a Text control you had on a form.  The fact that you were trying to change the value of the .Text property brought me to that conclusion.

Is txtbox1 the name of a control you have?  If so, you don't need the line - int txtbox1;, because adding it your your form already gives it a definition.
0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 100 total points
ID: 22806146
'textbox1' is not defined anywhere in the static function Main
you have defined  int txtbox1; the variables are different

can we try to know what you are trying to do here so that we can help you better
0
 
LVL 13

Assisted Solution

by:sm394
sm394 earned 200 total points
ID: 22806901
yes ragi0017  even i mixed up the two textbox1  and  txtbox1
0
 
LVL 3

Assisted Solution

by:TechSinger
TechSinger earned 150 total points
ID: 22807693
Yep, I guess I got them mixed up too.  I still the whole problem is that you are trying to convert a String array to a String, but you are using a loop, which would indicate you are trying to show each element.  I would make the following small modification, as I mentioned in my first comment.

        {
            int txtbox1;
            string[] w = new string[3];
            w[0] = "Hello";
            w[1] = ",";
            w[3] = "world";
            for (int i = 0; i < 3; i++)
                textbox1.Text = w[i];
        }

Open in new window

0
 
LVL 13

Assisted Solution

by:sm394
sm394 earned 200 total points
ID: 22807727
TechSinger  i guess purpose of loop might be  
textbox1.Text += w[i];
so textbox1.Text will be like  "Hello , world"

0
 
LVL 4

Assisted Solution

by:inox
inox earned 50 total points
ID: 22808377
it seems you have three to four problems in your code:
Your question: the textbox (if it is a textbox control which indeed has a Text property) is most likely named Textbox1 not textbox1.
Textboxcontrols are placed on a Windows-Form or on a Web-Form but:
Webforms don't have a main function.
If it is a Windows-Foms program, the code should be in the Forms code.
int txtbox1isn't used at all,
w[3]  is indexed out of range,
w.ToString(); is generally possible but likely does not return what you expect.
what was intended?
0
 
LVL 10

Author Comment

by:GlobaLevel
ID: 22818574
I tried the snippet of code provided it still thru me any error:

Error      1      An object reference is required for the nonstatic field, method, or property
Error      2      Cannot convert type 'string' to 'System.Windows.Forms.TextBox'      
Error      3      Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox'      C:\Visual Studio 2005 10-08\WindowsApplication1\Form1.cs      34      28      WindowsApplication1

public static void main(String[] args)
        {
            string txtbox1;
            string[] w = new string[3];
            w[0] = "Hello";
            w[1] = ",";
            w[3] = "world";
            for (int i = 0; i < 3; i++)
                textBox1+= w[i].ToString();
        }



I tried the following but an error:
Class, struct, or interface method must have a return type

Here is my 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 Form1 : Form
    {
       
        public Form1()
        {
            InitializeComponent();
        }

        private  void button1_Click(object sender, EventArgs e)
        {
           
        }
       

        public static main(String[] args)
        {
           
            string[] w = new string[3];
            w[0] = "Hello";
            w[1] = ",";
            w[3] = "world";
            for (int i = 0; i < 3; i++)
                return textBox1 = w[i].ToString;
               
        }
    }



}


On the form there is a textbox called: textBox1
There is a button called: button1

The goal is to write a simple program that will print out hello, world through an array
0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 100 total points
ID: 22818866
you cannot access class non static members/variables from a static method
best way will be to do that from the button click event

move your code from main to button click

            string[] w = new string[3];
            w[0] = "Hello";
            w[1] = ",";
            w[3] = " world";
            for (int i = 0; i < 3; i++)
                return textBox1.Text += w[i].ToString;
0
 
LVL 10

Author Closing Comment

by:GlobaLevel
ID: 31510017
Thanks!
0
 

Expert Comment

by:HottTheMoople
ID: 36013654
I had this error "The name 'textBoxFind' does not exist in the current context" was caused because I inadvertently toggled textBoxFind.GenerateMember to false in the Properties wind in VS at some point.  Switched it back to true.  Hope that helps anyone else looking at similar error.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now