我正在尝试为我正在处理的项目创建自己的错误窗口。当我显示我的错误窗口时,我无法将错误消息和用户消息传递给错误窗口,因为在我创建的类中无法看到“ErrorMessage.Text”。
我进入了表单设计器生成的代码,并尝试使TextBox保持静态,但这只会破坏事物。我可以将TextBox设为public / static,以便我可以从另一种形式更改它吗?
修改
好的,有关更多信息......
我创建了自己的表单。它被称为“formErrorWindow”。我需要显示我使用其他表单中的消息集预先设计的表单。我能做到这一点的唯一方法是,如果我在窗体设计器区域中为窗体创建一个Function,并使用“this.errorMsg.text = error”设置变量。我能看到该功能的唯一方法是将其设置为静态。如果我将函数设置为静态,当我尝试将“this.errorMsg.Text = error”时,我得到此错误:非静态字段,方法或属性需要对象引用。
以下是我的尝试:
namespace LCR_ShepherdStaffupdater_1._0
{
partial class formErrorWindow
{
/// <summary>
/// Required designer variable.
/// </summary>
public 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>
// ********* HERE IS THE FUNCTION THAT IVE ADDED BELOW. THIS WOULD WORK BUT.... *********
public static void showError(string errorTitle, string usrMsg, string errorMsg)
{
formErrorWindow errorWindow = new formErrorWindow();
errorMsgItem.Text = errorMsg;
errorTitleItem.Text = "Error! : " + errorTitle;
usrMsgItem.Text = usrMsg;
errorWindow.ShowDialog();
}
// ********* HERE IS THE FUNCTION THAT IVE ADDED ABOVE. THIS WOULD WORK BUT.... *********
// ********* I get an error: "An object reference is required for the non-static field, method, or property." *********
public void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(formErrorWindow));
this.usrMsgItem = new System.Windows.Forms.TextBox();
this.errorTitleItem = new System.Windows.Forms.Label();
this.errorMsgItem = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.label2 = new System.Windows.Forms.Label();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// usrMsgItem
//
this.usrMsgItem.Enabled = false;
this.usrMsgItem.Location = new System.Drawing.Point(13, 37);
this.usrMsgItem.Multiline = true;
this.usrMsgItem.Name = "usrMsgItem";
this.usrMsgItem.Size = new System.Drawing.Size(334, 81);
this.usrMsgItem.TabIndex = 0;
this.usrMsgItem.Text = "Undefined";
//
// errorTitleItem
//
this.errorTitleItem.AutoSize = true;
this.errorTitleItem.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.errorTitleItem.ForeColor = System.Drawing.Color.Red;
this.errorTitleItem.Location = new System.Drawing.Point(12, 9);
this.errorTitleItem.Name = "errorTitleItem";
this.errorTitleItem.Size = new System.Drawing.Size(152, 20);
this.errorTitleItem.TabIndex = 1;
this.errorTitleItem.Text = "Error! : Undefined";
//
// errorMsgItem
//
this.errorMsgItem.Enabled = false;
this.errorMsgItem.Location = new System.Drawing.Point(0, 21);
this.errorMsgItem.Multiline = true;
this.errorMsgItem.Name = "errorMsgItem";
this.errorMsgItem.Size = new System.Drawing.Size(329, 101);
this.errorMsgItem.TabIndex = 2;
this.errorMsgItem.Text = "Undefined";
//
// button1
//
this.button1.Location = new System.Drawing.Point(272, 256);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 3;
this.button1.Text = "Continue";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// panel1
//
this.panel1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(128)))));
this.panel1.Controls.Add(this.label2);
this.panel1.Controls.Add(this.errorMsgItem);
this.panel1.Location = new System.Drawing.Point(12, 124);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(335, 126);
this.panel1.TabIndex = 4;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label2.Location = new System.Drawing.Point(68, 1);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(189, 17);
this.label2.TabIndex = 3;
this.label2.Text = "Technical Error Message";
//
// formErrorWindow
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
this.ClientSize = new System.Drawing.Size(359, 290);
this.Controls.Add(this.panel1);
this.Controls.Add(this.button1);
this.Controls.Add(this.errorTitleItem);
this.Controls.Add(this.usrMsgItem);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "formErrorWindow";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Error!";
this.VisibleChanged += new System.EventHandler(this.formErrorWindow_VisibleChanged);
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label label2;
public System.Windows.Forms.TextBox usrMsgItem;
public System.Windows.Forms.Label errorTitleItem;
public System.Windows.Forms.TextBox errorMsgItem;
}
}
寻找我上面添加的功能。我如何编译并执行我想要它做的事情没有我不断得到的错误:非静态字段,方法或属性需要对象引用。
答案 0 :(得分:6)
我只是将消息作为构造函数参数传递。
MyMessageBox messageBox = new MyMessageBox("My error message");
messageBox.Show();
答案 1 :(得分:1)
是的!有一个公开的功能,可以收到这个文本:
pseudo: public void updateTextBox(string new_text)
并让函数从那里更新文本框。
不要将UI与逻辑混合。
答案 2 :(得分:1)
public partial class Form1 : Form
{
private static TextBox box;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// box
box = new TextBox();
box.Location = new System.Drawing.Point(87, 230);
box.Name = "box";
box.Size = new System.Drawing.Size(100, 20);
box.TabIndex = 1;
this.Controls.Add(box);
}
}
答案 3 :(得分:0)
您可以先将TextBox公开。 第二个创建一个静态表单(表单名称)变量,并在每次加载时将表单放在那里... 这在我眼里不是一个好的解决方案,但是如果不了解你的设计,那么我能想出的最好......
答案 4 :(得分:0)
允许公开访问类中包含的对象可能是一种设计。如何在名为ErrorText
的错误窗口类中编写属性,并在该属性中将ErrorMessage.Text
设置为传递的字符串?或者我错过了什么?
答案 5 :(得分:0)
如果您真的想直接访问控件:
public Form2(Form1 form1)
{
InitializeComponent();
Owner = form1;
_form1 = form1;
}
现在您可以通过以下方式访问form1上的控件:
_form1.theForm1ControlName
答案 6 :(得分:0)
你可以在Error窗口的类中创建一个类似于:
的方法public void SetErrorText(string errorText)
{
this.ErrorMessage.Text = errorText; // assuming the TextBox' ID is ErrorMessage.
}
答案 7 :(得分:0)
通过添加你所做的公共功能听起来你在正确的轨道上,但你不应该让它静止。作为一般规则,如果你必须进入Designer生成的代码,有些东西是不对的,那么让我们离开那里 - 这并不意味着你不能将代码添加到表单中,显然,但不要在那一节做到:)
你得到的错误我相信来自于一个非实例化的对象(即创建错误形式,如:
formErrorWindow myErrorWindow;
而不是像实例化/创建它的实例一样:
formErrorWindow myErrorWindow = new formErrorWindow();
你已经将函数设置为静态,所以你可以从任何地方看到它,但是该函数实际上无法做任何事情,因为它运行的表单在你使用“= new formErrorWindow()之前还不存在; ”
因此,如果我在一个名为“MainForm”的表单中并且我想显示错误,那么使用您提供的函数的名称就可以这样做:
formErrorWindow myErrorWindow = new formErrorWindow();
myErrorWindow.showError("Title", "User Message Here", "Error Message Here");
myErrorWindow.ShowDialog();
关键是要确保没有任何东西是静态的,然后确保你使用= new formErrorWindow()初始化formErrorWindow;
答案 8 :(得分:0)
像这样将TextBox传递给函数/方法
public static void myFunc(TextBox textBox1)
{
textBox1.Text = "do stuff";
{