尝试添加代码以在按钮单击时隐藏标签时出现错误。
private void VehiclesBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.vehiclesBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.ABCDataSet);
bindingNavigatorAddNewItem.Visible = true;
vehiclesBindingNavigatorSaveItem.Visible = false;
System.Windows.Forms.MessageBox.Show("Vehicle Saved");
regTextBox.Visible = false;
makeTextBox.Visible = false;
modelTextBox.Visible = false;
last_MileageTextBox.Visible = false;
mOT_Expiry_DateDateTimePicker.Visible = false;
tAX_Expiry_DateDateTimePicker.Visible = false;
retiredTextBox.Visible = false;
regLabel.Visible = false;
}
错误:
来自Designer的错误CS0103名称' regLabel'在当前上下文中不存在
regLabel:
//
// regLabel
//
regLabel.AutoSize = true;
regLabel.Location = new System.Drawing.Point(65, 46);
regLabel.Name = "regLabel";
regLabel.Size = new System.Drawing.Size(30, 13);
regLabel.TabIndex = 2;
regLabel.Text = "Reg:";
表单CS文件:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ABC
{
public partial class NewVehicle : Form
{
public NewVehicle()
{
InitializeComponent();
}
private void VehiclesBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.vehiclesBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.ABCDataSet);
bindingNavigatorAddNewItem.Visible = true;
vehiclesBindingNavigatorSaveItem.Visible = false;
System.Windows.Forms.MessageBox.Show("Vehicle Saved");
regTextBox.Visible = false;
makeTextBox.Visible = false;
modelTextBox.Visible = false;
last_MileageTextBox.Visible = false;
mOT_Expiry_DateDateTimePicker.Visible = false;
tAX_Expiry_DateDateTimePicker.Visible = false;
retiredTextBox.Visible = false;
regLabel.Visible = false;
}
private void NewVehicle_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'ABCDataSet.Vehicles' table. You can move, or remove it, as needed.
this.vehiclesTableAdapter.Fill(this.ABCDataSet.Vehicles);
}
private void Button1_Click(object sender, EventArgs e)
{
this.Validate();
this.vehiclesBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.ABCDataSet);
}
private void CloseNewVehicle_Click(object sender, EventArgs e)
{
this.Close();
}
private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
{
regTextBox.Visible = true;
makeTextBox.Visible = true;
modelTextBox.Visible = true;
last_MileageTextBox.Visible = true;
mOT_Expiry_DateDateTimePicker.Visible = true;
tAX_Expiry_DateDateTimePicker.Visible = true;
retiredTextBox.Visible = true;
regLabel.Visible = true;
bindingNavigatorAddNewItem.Visible = false;
vehiclesBindingSource.AddNew();
retiredTextBox.Text = Properties.Settings.Default.retireddefault;
vehiclesBindingNavigatorSaveItem.Visible = true;
}
private void regTextBox_TextChanged(object sender, EventArgs e)
{
}
private void vehiclesBindingNavigator_RefreshItems(object sender, EventArgs e)
{
}
private void vehiclesBindingSource_CurrentChanged(object sender, EventArgs e)
{
}
}
}
表单设计器CS文件:
using System;
namespace ABC
{
partial class NewVehicle
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Windows.Forms.Label regLabel;
System.Windows.Forms.Label makeLabel;
System.Windows.Forms.Label modelLabel;
System.Windows.Forms.Label last_MileageLabel;
System.Windows.Forms.Label mOT_Expiry_DateLabel;
System.Windows.Forms.Label tAX_Expiry_DateLabel;
System.Windows.Forms.Label retiredLabel;
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(NewVehicle));
this.vehiclesBindingSource = new System.Windows.Forms.BindingSource(this.components);
this.ABCDataSet = new ABC.ABCDataSet();
this.vehiclesTableAdapter = new ABC.ABCDataSetTableAdapters.VehiclesTableAdapter();
this.tableAdapterManager = new ABC.ABCDataSetTableAdapters.TableAdapterManager();
this.bindingNavigatorCountItem = new System.Windows.Forms.ToolStripLabel();
this.vehiclesBindingNavigatorSaveItem = new System.Windows.Forms.ToolStripButton();
this.bindingNavigatorAddNewItem = new System.Windows.Forms.ToolStripButton();
this.vehiclesBindingNavigator = new System.Windows.Forms.BindingNavigator(this.components);
this.regTextBox = new System.Windows.Forms.TextBox();
this.makeTextBox = new System.Windows.Forms.TextBox();
this.modelTextBox = new System.Windows.Forms.TextBox();
this.last_MileageTextBox = new System.Windows.Forms.TextBox();
this.mOT_Expiry_DateDateTimePicker = new System.Windows.Forms.DateTimePicker();
this.tAX_Expiry_DateDateTimePicker = new System.Windows.Forms.DateTimePicker();
this.retiredTextBox = new System.Windows.Forms.TextBox();
regLabel = new System.Windows.Forms.Label();
makeLabel = new System.Windows.Forms.Label();
modelLabel = new System.Windows.Forms.Label();
last_MileageLabel = new System.Windows.Forms.Label();
mOT_Expiry_DateLabel = new System.Windows.Forms.Label();
tAX_Expiry_DateLabel = new System.Windows.Forms.Label();
retiredLabel = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.vehiclesBindingSource)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.ABCDataSet)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.vehiclesBindingNavigator)).BeginInit();
this.vehiclesBindingNavigator.SuspendLayout();
this.SuspendLayout();
//
// regLabel
//
regLabel.AutoSize = true;
regLabel.Location = new System.Drawing.Point(65, 46);
regLabel.Name = "regLabel";
regLabel.Size = new System.Drawing.Size(30, 13);
regLabel.TabIndex = 2;
regLabel.Text = "Reg:";
//
// makeLabel
//
makeLabel.AutoSize = true;
makeLabel.Location = new System.Drawing.Point(58, 72);
makeLabel.Name = "makeLabel";
makeLabel.Size = new System.Drawing.Size(37, 13);
makeLabel.TabIndex = 4;
makeLabel.Text = "Make:";
//
// modelLabel
//
modelLabel.AutoSize = true;
modelLabel.Location = new System.Drawing.Point(56, 98);
modelLabel.Name = "modelLabel";
modelLabel.Size = new System.Drawing.Size(39, 13);
modelLabel.TabIndex = 6;
modelLabel.Text = "Model:";
//
// last_MileageLabel
//
last_MileageLabel.AutoSize = true;
last_MileageLabel.Location = new System.Drawing.Point(25, 124);
last_MileageLabel.Name = "last_MileageLabel";
last_MileageLabel.Size = new System.Drawing.Size(70, 13);
last_MileageLabel.TabIndex = 8;
last_MileageLabel.Text = "Last Mileage:";
//
// mOT_Expiry_DateLabel
//
mOT_Expiry_DateLabel.AutoSize = true;
mOT_Expiry_DateLabel.Location = new System.Drawing.Point(4, 151);
mOT_Expiry_DateLabel.Name = "mOT_Expiry_DateLabel";
mOT_Expiry_DateLabel.Size = new System.Drawing.Size(91, 13);
mOT_Expiry_DateLabel.TabIndex = 9;
mOT_Expiry_DateLabel.Text = "MOT Expiry Date:";
//
// tAX_Expiry_DateLabel
//
tAX_Expiry_DateLabel.AutoSize = true;
tAX_Expiry_DateLabel.Location = new System.Drawing.Point(7, 177);
tAX_Expiry_DateLabel.Name = "tAX_Expiry_DateLabel";
tAX_Expiry_DateLabel.Size = new System.Drawing.Size(88, 13);
tAX_Expiry_DateLabel.TabIndex = 11;
tAX_Expiry_DateLabel.Text = "TAX Expiry Date:";
//
// retiredLabel
//
retiredLabel.AutoSize = true;
retiredLabel.Location = new System.Drawing.Point(51, 202);
retiredLabel.Name = "retiredLabel";
retiredLabel.Size = new System.Drawing.Size(44, 13);
retiredLabel.TabIndex = 13;
retiredLabel.Text = "Retired:";
//
// vehiclesBindingSource
//
this.vehiclesBindingSource.DataMember = "Vehicles";
this.vehiclesBindingSource.DataSource = this.ABCDataSet;
this.vehiclesBindingSource.CurrentChanged += new System.EventHandler(this.vehiclesBindingSource_CurrentChanged);
//
// ABCDataSet
//
this.ABCDataSet.DataSetName = "ABCDataSet";
this.ABCDataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
//
// vehiclesTableAdapter
//
this.vehiclesTableAdapter.ClearBeforeFill = true;
//
// tableAdapterManager
//
this.tableAdapterManager.BackupDataSetBeforeUpdate = false;
this.tableAdapterManager.ConsumablesTableAdapter = null;
this.tableAdapterManager.EmployeesTableAdapter = null;
this.tableAdapterManager.HolidaysTableAdapter = null;
this.tableAdapterManager.ServicesTableAdapter = null;
this.tableAdapterManager.UpdateOrder = ABC.ABCDataSetTableAdapters.TableAdapterManager.UpdateOrderOption.InsertUpdateDelete;
this.tableAdapterManager.VehiclesTableAdapter = this.vehiclesTableAdapter;
//
// bindingNavigatorCountItem
//
this.bindingNavigatorCountItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.None;
this.bindingNavigatorCountItem.Enabled = false;
this.bindingNavigatorCountItem.Name = "bindingNavigatorCountItem";
this.bindingNavigatorCountItem.Size = new System.Drawing.Size(0, 37);
this.bindingNavigatorCountItem.Text = "of {0}";
this.bindingNavigatorCountItem.ToolTipText = "Total number of items";
//
// vehiclesBindingNavigatorSaveItem
//
this.vehiclesBindingNavigatorSaveItem.Image = ((System.Drawing.Image)(resources.GetObject("vehiclesBindingNavigatorSaveItem.Image")));
this.vehiclesBindingNavigatorSaveItem.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None;
this.vehiclesBindingNavigatorSaveItem.Name = "vehiclesBindingNavigatorSaveItem";
this.vehiclesBindingNavigatorSaveItem.Size = new System.Drawing.Size(68, 37);
this.vehiclesBindingNavigatorSaveItem.Text = "Save";
this.vehiclesBindingNavigatorSaveItem.Visible = false;
this.vehiclesBindingNavigatorSaveItem.Click += new System.EventHandler(this.VehiclesBindingNavigatorSaveItem_Click);
//
// bindingNavigatorAddNewItem
//
this.bindingNavigatorAddNewItem.Image = ((System.Drawing.Image)(resources.GetObject("bindingNavigatorAddNewItem.Image")));
this.bindingNavigatorAddNewItem.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None;
this.bindingNavigatorAddNewItem.Name = "bindingNavigatorAddNewItem";
this.bindingNavigatorAddNewItem.RightToLeftAutoMirrorImage = true;
this.bindingNavigatorAddNewItem.Size = new System.Drawing.Size(133, 37);
this.bindingNavigatorAddNewItem.Text = "Add New Vehicle";
this.bindingNavigatorAddNewItem.Click += new System.EventHandler(this.bindingNavigatorAddNewItem_Click);
//
// vehiclesBindingNavigator
//
this.vehiclesBindingNavigator.AddNewItem = null;
this.vehiclesBindingNavigator.BindingSource = this.vehiclesBindingSource;
this.vehiclesBindingNavigator.CountItem = this.bindingNavigatorCountItem;
this.vehiclesBindingNavigator.DeleteItem = null;
this.vehiclesBindingNavigator.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.bindingNavigatorCountItem,
this.bindingNavigatorAddNewItem,
this.vehiclesBindingNavigatorSaveItem});
this.vehiclesBindingNavigator.Location = new System.Drawing.Point(0, 0);
this.vehiclesBindingNavigator.MoveFirstItem = null;
this.vehiclesBindingNavigator.MoveLastItem = null;
this.vehiclesBindingNavigator.MoveNextItem = null;
this.vehiclesBindingNavigator.MovePreviousItem = null;
this.vehiclesBindingNavigator.Name = "vehiclesBindingNavigator";
this.vehiclesBindingNavigator.PositionItem = null;
this.vehiclesBindingNavigator.Size = new System.Drawing.Size(317, 40);
this.vehiclesBindingNavigator.TabIndex = 0;
this.vehiclesBindingNavigator.Text = "bindingNavigator1";
this.vehiclesBindingNavigator.RefreshItems += new System.EventHandler(this.vehiclesBindingNavigator_RefreshItems);
//
// regTextBox
//
this.regTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.vehiclesBindingSource, "Reg", true));
this.regTextBox.Location = new System.Drawing.Point(101, 43);
this.regTextBox.Name = "regTextBox";
this.regTextBox.Size = new System.Drawing.Size(100, 20);
this.regTextBox.TabIndex = 3;
this.regTextBox.Visible = false;
this.regTextBox.TextChanged += new System.EventHandler(this.regTextBox_TextChanged);
//
// makeTextBox
//
this.makeTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.vehiclesBindingSource, "Make", true));
this.makeTextBox.Location = new System.Drawing.Point(101, 69);
this.makeTextBox.Name = "makeTextBox";
this.makeTextBox.Size = new System.Drawing.Size(100, 20);
this.makeTextBox.TabIndex = 5;
this.makeTextBox.Visible = false;
//
// modelTextBox
//
this.modelTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.vehiclesBindingSource, "Model", true));
this.modelTextBox.Location = new System.Drawing.Point(101, 95);
this.modelTextBox.Name = "modelTextBox";
this.modelTextBox.Size = new System.Drawing.Size(100, 20);
this.modelTextBox.TabIndex = 7;
this.modelTextBox.Visible = false;
//
// last_MileageTextBox
//
this.last_MileageTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.vehiclesBindingSource, "Last Mileage", true));
this.last_MileageTextBox.Location = new System.Drawing.Point(101, 121);
this.last_MileageTextBox.Name = "last_MileageTextBox";
this.last_MileageTextBox.Size = new System.Drawing.Size(100, 20);
this.last_MileageTextBox.TabIndex = 9;
this.last_MileageTextBox.Visible = false;
//
// mOT_Expiry_DateDateTimePicker
//
this.mOT_Expiry_DateDateTimePicker.DataBindings.Add(new System.Windows.Forms.Binding("Value", this.vehiclesBindingSource, "MOT Expiry Date", true));
this.mOT_Expiry_DateDateTimePicker.Location = new System.Drawing.Point(101, 147);
this.mOT_Expiry_DateDateTimePicker.Name = "mOT_Expiry_DateDateTimePicker";
this.mOT_Expiry_DateDateTimePicker.Size = new System.Drawing.Size(200, 20);
this.mOT_Expiry_DateDateTimePicker.TabIndex = 10;
this.mOT_Expiry_DateDateTimePicker.Visible = false;
//
// tAX_Expiry_DateDateTimePicker
//
this.tAX_Expiry_DateDateTimePicker.DataBindings.Add(new System.Windows.Forms.Binding("Value", this.vehiclesBindingSource, "TAX Expiry Date", true));
this.tAX_Expiry_DateDateTimePicker.Location = new System.Drawing.Point(101, 173);
this.tAX_Expiry_DateDateTimePicker.Name = "tAX_Expiry_DateDateTimePicker";
this.tAX_Expiry_DateDateTimePicker.Size = new System.Drawing.Size(200, 20);
this.tAX_Expiry_DateDateTimePicker.TabIndex = 12;
this.tAX_Expiry_DateDateTimePicker.Visible = false;
//
// retiredTextBox
//
this.retiredTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.vehiclesBindingSource, "Retired", true));
this.retiredTextBox.Location = new System.Drawing.Point(101, 199);
this.retiredTextBox.Name = "retiredTextBox";
this.retiredTextBox.Size = new System.Drawing.Size(100, 20);
this.retiredTextBox.TabIndex = 14;
this.retiredTextBox.Visible = false;
this.retiredTextBox.TextChanged += new System.EventHandler(this.retiredTextBox_TextChanged);
//
// NewVehicle
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
this.ClientSize = new System.Drawing.Size(317, 232);
this.Controls.Add(retiredLabel);
this.Controls.Add(this.retiredTextBox);
this.Controls.Add(tAX_Expiry_DateLabel);
this.Controls.Add(this.tAX_Expiry_DateDateTimePicker);
this.Controls.Add(mOT_Expiry_DateLabel);
this.Controls.Add(this.mOT_Expiry_DateDateTimePicker);
this.Controls.Add(last_MileageLabel);
this.Controls.Add(this.last_MileageTextBox);
this.Controls.Add(modelLabel);
this.Controls.Add(this.modelTextBox);
this.Controls.Add(makeLabel);
this.Controls.Add(this.makeTextBox);
this.Controls.Add(regLabel);
this.Controls.Add(this.regTextBox);
this.Controls.Add(this.vehiclesBindingNavigator);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "NewVehicle";
this.Text = "Cowan Transport Management System - New Vehicle";
this.Load += new System.EventHandler(this.NewVehicle_Load);
((System.ComponentModel.ISupportInitialize)(this.vehiclesBindingSource)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.ABCDataSet)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.vehiclesBindingNavigator)).EndInit();
this.vehiclesBindingNavigator.ResumeLayout(false);
this.vehiclesBindingNavigator.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
private void retiredTextBox_TextChanged(object sender, EventArgs e)
{
}
private void CloseAmendVehicle_Click(object sender, EventArgs e)
{
}
#endregion
private ABCDataSet ABCDataSet;
private System.Windows.Forms.BindingSource vehiclesBindingSource;
private ABCDataSetTableAdapters.VehiclesTableAdapter vehiclesTableAdapter;
private ABCDataSetTableAdapters.TableAdapterManager tableAdapterManager;
private System.Windows.Forms.ToolStripLabel bindingNavigatorCountItem;
private System.Windows.Forms.ToolStripButton vehiclesBindingNavigatorSaveItem;
private System.Windows.Forms.ToolStripButton bindingNavigatorAddNewItem;
private System.Windows.Forms.BindingNavigator vehiclesBindingNavigator;
private System.Windows.Forms.TextBox regTextBox;
private System.Windows.Forms.TextBox makeTextBox;
private System.Windows.Forms.TextBox modelTextBox;
private System.Windows.Forms.TextBox last_MileageTextBox;
private System.Windows.Forms.DateTimePicker mOT_Expiry_DateDateTimePicker;
private System.Windows.Forms.DateTimePicker tAX_Expiry_DateDateTimePicker;
private System.Windows.Forms.TextBox retiredTextBox;
}
}
答案 0 :(得分:3)
<强>结论强>
然后将标签属性GenerateMember
更改为true。
对于标签,默认情况下通常为true,但如果从“数据源窗口”拖动对象并将其放在表单上,设计人员会将这些标签的GenerateMember
属性设置为false。
More information about GenerateMember
property.
因为您在regLablel
方法中定义并声明了InitializeComponent
。将其定义移到文件末尾的其他定义:
System.Windows.Forms.Label regLabel;
然后在InitializeComponents()
方法内执行此操作:
this.regLabel = new System.Windows.Forms.Label();
之后改变了这个:
regLabel.AutoSize = true;
regLabel.Location = new System.Drawing.Point(65, 46);
regLabel.Name = "regLabel";
regLabel.Size = new System.Drawing.Size(30, 13);
regLabel.TabIndex = 2;
regLabel.Text = "Reg:";
要
this.regLabel.AutoSize = true;
this.regLabel.Location = new System.Drawing.Point(65, 46);
this.regLabel.Name = "regLabel";
this.regLabel.Size = new System.Drawing.Size(30, 13);
this.regLabel.TabIndex = 2;
this.regLabel.Text = "Reg:";
答案 1 :(得分:2)
您为form.designer.cs类发布的代码似乎是(部分?)由手工生成而不是由设计师生成。
System.Windows.Forms.Label regLabel
是InitializeComponent
方法的局部变量,而设计者应将其创建为Form继承类的成员变量。以下控件makeLabel
,modeLabel
等等相同
尝试在InitializeComponent
方法之外手动移动控件声明,作为成员变量,之后......从不手动编辑该文件
答案 2 :(得分:1)
这是因为npm install -g editorconfig
属性在设计时设置为GenerateMember
,因此,不会为控件生成成员字段。
请勿触摸生成的代码,而是在设计模式下打开表单,然后在设计时选择false
。在属性网格中,选择regLabel
属性并将其设置为GenerateMember
。
注意:对于标签,默认情况下通常为true,但如果从true
拖动对象并将其放在表单上,设计人员将为这些标签设置Data Sources Window
属性为假。