C# need help

Started by
3 comments, last by Sijmen 19 years, 6 months ago
The idea is to check if the user entered a name. If he didn't, alert the user that he didn't do so. Currently it's giving me this error... \MainForm.cs(94,7): error CS0122: 'System.Windows.Forms.Control.text' is inaccessible due to its protection level

using System;
using System.Drawing;
using System.Windows.Forms;

class MyForm : Form
{
	Label lblName, lblAge, lblRace;
	TextBox tbName, tbAge;
	ComboBox cbRace;
	Button btnSubmit;
	
	public static void Main(string[] args)
	{
		Application.Run(new MyForm());
	}
	
	public MyForm()
	{
		#region Form Properties
		this.Text = "C# Programming";
		this.Name = "MainForm";
		this.StartPosition = FormStartPosition.CenterScreen;
		this.Size = new System.Drawing.Size(800,600);
		this.BackColor = System.Drawing.Color.AliceBlue;
		#endregion
		
		#region Label: lblName
		this.lblName = new Label();
		lblName.Text = "Name:";
		lblName.Name = "lblName";
		lblName.Location = new System.Drawing.Point(10,10);
		lblName.ClientSize = new System.Drawing.Size(40, 15);
			
		// TextBox: tbName
		this.tbName = new TextBox();
		tbName.Name = "tbName";
		tbName.Location = new System.Drawing.Point(65,7);
		tbName.Size = new System.Drawing.Size(100,25);
		#endregion
		
		#region Label: lblAge
		lblAge = new Label();
		lblAge.Text = "Age:";
		lblAge.Name = "lblAge";
		lblAge.Location = new System.Drawing.Point(10,40);
		lblAge.ClientSize = new System.Drawing.Size(40,15);
		
		// TextBox: tbAge
		tbAge = new TextBox();
		tbAge.Name = "tbAge";
		tbAge.Location = new System.Drawing.Point(65,40);
		tbAge.Size = new System.Drawing.Size(100,45);
		#endregion
		
		#region Label: lblRace
		lblRace = new Label();
		lblRace.Text = "Race:";
		lblRace.Name = "lblRace";
		lblRace.Location = new System.Drawing.Point(10,70);
		lblRace.ClientSize = new System.Drawing.Size(40,15);
		
		// ComboBox: cbRace
		cbRace = new ComboBox();
		cbRace.Location = new System.Drawing.Point(65, 70);
		cbRace.Size = new System.Drawing.Size(100, 15);
		cbRace.DropDownStyle = ComboBoxStyle.DropDownList;
		cbRace.Items.Add("Human");
		cbRace.Items.Add("Dark Elf");
		cbRace.Items.Add("Wood Elf");
		#endregion
		
		this.btnSubmit = new Button();
		btnSubmit.Text = "Submit";
		btnSubmit.BackColor = System.Drawing.Color.LightGreen;
		btnSubmit.Location = new System.Drawing.Point(170, 70);
		btnSubmit.Size = new System.Drawing.Size(100, 21);
		btnSubmit.Click += new System.EventHandler(this.btnSubmitClick);
			
		#region Controls
		this.Controls.Add(this.lblName);
		this.Controls.Add(this.tbName);
		this.Controls.Add(this.lblAge);
		this.Controls.Add(this.tbAge);
		this.Controls.Add(this.lblRace);
		this.Controls.Add(this.cbRace);
		this.Controls.Add(this.btnSubmit);
		#endregion
		
	}
	
	void btnSubmitClick(object sender, System.EventArgs e)
	{
		if (tbName.text == "")
		{
			MessageBox.Show("You must enter a name.", "Character Creation", 
			               MessageBoxButtons.OK);
		}
	}
	
}


Advertisement
The field 'text' is the private or protected backer of the 'Text' property. You need the property, thus 'Text'. Watch the uppercase T!

Change
if (tbName.text == "")

to
if (tbName.Text == "")
.text is a private member. Use .Text, the public property.

Edit: 30 seconds!
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
hehe thats interesting. thanks.
Looking at your recent topics, have you made a switch from C++/Win32 to .NET? If so, and also if not so, good luck with .NET :)

This topic is closed to new replies.

Advertisement