我有一个显示狗的应用程序。
“ HelloDogs”,一个“ dog.cs”类无法看到“ form1.cs”中的文本框
有人可以阐明这一点吗?
我正在尝试使用输入到文本框中的用户定义变量来创建一条新狗
就像我试图在DogChorus中使用Dog()构造函数一样,但它不会接受textbox.Dog(int,string,string)的参数的文本内容,并且因为在form1.cs中而无法看到它们
在form1.designer.cs里面,我什至将textBox更改为public,但这也不起作用...
显示应用程序的屏幕截图
显示我单击“获取狗”按钮时会发生什么的屏幕截图
显示我单击“创建狗”按钮时会发生什么的屏幕截图
namespace HelloDogs
我必须向您展示4个课程; Form1.cs
,Form1.Designer.cs
Dog.cs
和DogChorus.cs
Form1.cs:
public partial class Form1 : Form
{
DogChorus dogs;
DogChorus dog;
public Form1()
{
InitializeComponent();
dogs = new DogChorus();
dog = new DogChorus();
}
private void button1_Click(object sender, EventArgs e)
{
dogDetailsRichTextBox.AppendText(dogs.GetOutput());
}
private void createDogsButton_Click(object sender, EventArgs e)
{
Dog newDog = new Dog(Convert.ToInt32(dogHeightTextBox.Text), dogColourTextBox.Text, dogBreedTextBox.Text);
dogDetailsRichTextBox.AppendText(dog.GetMoreOutput());
}
}
Form1.Designer.cs:
partial class Form1
{
/// <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.button1 = new System.Windows.Forms.Button();
this.dogDetailsRichTextBox = new System.Windows.Forms.RichTextBox();
this.dogBreedTextBox = new System.Windows.Forms.TextBox();
this.dogHeightTextBox = new System.Windows.Forms.TextBox();
this.dogColourTextBox = new System.Windows.Forms.TextBox();
this.dogBarkSoundTextBox = new System.Windows.Forms.TextBox();
this.createDogsButton = new System.Windows.Forms.Button();
this.enterBreedLabel = new System.Windows.Forms.Label();
this.enterDogHeightLabel = new System.Windows.Forms.Label();
this.enterDogColourLabel = new System.Windows.Forms.Label();
this.enterBarkSoundLabel = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(292, 119);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(122, 23);
this.button1.TabIndex = 0;
this.button1.Text = "get dogs";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// dogDetailsRichTextBox
//
this.dogDetailsRichTextBox.Location = new System.Drawing.Point(15, 148);
this.dogDetailsRichTextBox.Name = "dogDetailsRichTextBox";
this.dogDetailsRichTextBox.Size = new System.Drawing.Size(399, 328);
this.dogDetailsRichTextBox.TabIndex = 1;
this.dogDetailsRichTextBox.Text = "";
//
// dogBreedTextBox
//
this.dogBreedTextBox.Location = new System.Drawing.Point(156, 12);
this.dogBreedTextBox.Name = "dogBreedTextBox";
this.dogBreedTextBox.Size = new System.Drawing.Size(258, 20);
this.dogBreedTextBox.TabIndex = 2;
//
// dogHeightTextBox
//
this.dogHeightTextBox.Location = new System.Drawing.Point(156, 39);
this.dogHeightTextBox.Name = "dogHeightTextBox";
this.dogHeightTextBox.Size = new System.Drawing.Size(258, 20);
this.dogHeightTextBox.TabIndex = 3;
//
// dogColourTextBox
//
this.dogColourTextBox.Location = new System.Drawing.Point(156, 66);
this.dogColourTextBox.Name = "dogColourTextBox";
this.dogColourTextBox.Size = new System.Drawing.Size(258, 20);
this.dogColourTextBox.TabIndex = 4;
//
// dogBarkSoundTextBox
//
this.dogBarkSoundTextBox.Location = new System.Drawing.Point(156, 93);
this.dogBarkSoundTextBox.Name = "dogBarkSoundTextBox";
this.dogBarkSoundTextBox.Size = new System.Drawing.Size(258, 20);
this.dogBarkSoundTextBox.TabIndex = 5;
//
// createDogsButton
//
this.createDogsButton.Location = new System.Drawing.Point(156, 119);
this.createDogsButton.Name = "createDogsButton";
this.createDogsButton.Size = new System.Drawing.Size(130, 23);
this.createDogsButton.TabIndex = 6;
this.createDogsButton.Text = "Create Dogs";
this.createDogsButton.UseVisualStyleBackColor = true;
this.createDogsButton.Click += new System.EventHandler(this.createDogsButton_Click);
//
// enterBreedLabel
//
this.enterBreedLabel.AutoSize = true;
this.enterBreedLabel.Location = new System.Drawing.Point(12, 19);
this.enterBreedLabel.Name = "enterBreedLabel";
this.enterBreedLabel.Size = new System.Drawing.Size(116, 13);
this.enterBreedLabel.TabIndex = 7;
this.enterBreedLabel.Text = "Enter the breed of dog:";
//
// enterDogHeightLabel
//
this.enterDogHeightLabel.AutoSize = true;
this.enterDogHeightLabel.Location = new System.Drawing.Point(12, 46);
this.enterDogHeightLabel.Name = "enterDogHeightLabel";
this.enterDogHeightLabel.Size = new System.Drawing.Size(118, 13);
this.enterDogHeightLabel.TabIndex = 8;
this.enterDogHeightLabel.Text = "Enter the height of dog:";
//
// enterDogColourLabel
//
this.enterDogColourLabel.AutoSize = true;
this.enterDogColourLabel.Location = new System.Drawing.Point(12, 73);
this.enterDogColourLabel.Name = "enterDogColourLabel";
this.enterDogColourLabel.Size = new System.Drawing.Size(118, 13);
this.enterDogColourLabel.TabIndex = 9;
this.enterDogColourLabel.Text = "Enter the colour of dog:";
//
// enterBarkSoundLabel
//
this.enterBarkSoundLabel.AutoSize = true;
this.enterBarkSoundLabel.Location = new System.Drawing.Point(12, 96);
this.enterBarkSoundLabel.Name = "enterBarkSoundLabel";
this.enterBarkSoundLabel.Size = new System.Drawing.Size(142, 13);
this.enterBarkSoundLabel.TabIndex = 10;
this.enterBarkSoundLabel.Text = "Enter the bark sound of dog:";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(431, 488);
this.Controls.Add(this.enterBarkSoundLabel);
this.Controls.Add(this.enterDogColourLabel);
this.Controls.Add(this.enterDogHeightLabel);
this.Controls.Add(this.enterBreedLabel);
this.Controls.Add(this.createDogsButton);
this.Controls.Add(this.dogBarkSoundTextBox);
this.Controls.Add(this.dogColourTextBox);
this.Controls.Add(this.dogHeightTextBox);
this.Controls.Add(this.dogBreedTextBox);
this.Controls.Add(this.dogDetailsRichTextBox);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.RichTextBox dogDetailsRichTextBox;
public System.Windows.Forms.TextBox dogBreedTextBox;
public System.Windows.Forms.TextBox dogHeightTextBox;
public System.Windows.Forms.TextBox dogColourTextBox;
public System.Windows.Forms.TextBox dogBarkSoundTextBox;
private System.Windows.Forms.Button createDogsButton;
private System.Windows.Forms.Label enterBreedLabel;
private System.Windows.Forms.Label enterDogHeightLabel;
private System.Windows.Forms.Label enterDogColourLabel;
private System.Windows.Forms.Label enterBarkSoundLabel;
}
Dog.cs:
class Dog
{
private string barkSound;
private string breed;
private int dogHeight;
private string dogColour;
private static int noOfLegs;
public string Breed
{
get { return breed; }
set { breed = value; }
}
public int DogHeight { get => dogHeight; set => dogHeight = value; }
public string DogColour { get => dogColour; set => dogColour = value; }
public static int NoOfLegs { get => noOfLegs; set => noOfLegs = value; }
private string dogSpeech;
public Dog()
{
barkSound = "Woof!";
breed = "cocker spaniel";
}
public Dog(int dogHeight, string dogColour, string breed)
{
this.dogHeight = dogHeight;
this.dogColour = dogColour;
this.breed = breed;
}
private bool IsBig(int number)
{
if (number < 50)
{
return false;
}
else
{
return true;
}
}
public string GetSpeech()
{
if (IsBig(dogHeight))
{
dogSpeech = "Hello. I am a " + dogColour + " " + breed + ". I am a big dog, my height is " + dogHeight + " cm. " + barkSound;
}
else
{
dogSpeech = "Hello. I am a " + dogColour + " " + breed + ". I am not a big dog, my height is " + dogHeight + " cm. " + barkSound;
}
return dogSpeech;
}
public void SetSound(String barkSound)
{
this.barkSound = barkSound;
}
}
DogChorus.cs:
class DogChorus
{
Dog lady;
Dog tramp;
Dog chief;
Dog bingo;
Dog newDog;
public DogChorus() {
lady = new Dog();
tramp = new Dog();
tramp.SetSound("Ruff!");
chief = new Dog(55, "blue", "bulldog");
bingo = new Dog(45, "red", "boxer");
newDog = new Dog(0,"","");
}
public string GetOutput(){
return lady.GetSpeech() + " I have " + Dog.NoOfLegs + " legs \n " + tramp.GetSpeech() + " I have " + Dog.NoOfLegs + " legs \n " + chief.GetSpeech() + " I have " + Dog.NoOfLegs + " legs \n " + bingo.GetSpeech() + " I have " + Dog.NoOfLegs + " legs \n";
}
public string GetMoreOutput()
{
return newDog.GetSpeech() + " I have " + Dog.NoOfLegs + "legs \n ";
}
}
答案 0 :(得分:1)
根据您提出的问题,我没有阅读您所有的代码,我认为您需要将Form1
的实例(通过构造函数)传递给您的{{3} }类。
请在下面的示例中的代码内查看我的评论:
Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// global variable of Dogs Class instance - pass it a Form1 instance
public Dogs _Dogs;
private void Form1_Load(object sender, EventArgs e)
{
// initilize Dogs class and pass this Form1 instance
_Dogs = new Dogs(this);
}
private void button1_Click(object sender, EventArgs e)
{
// show textbox1 text from Dogs class
_Dogs.showTextBox1Text();
// you can also just pass the value of textbox1 to Dogs instance
_Dogs.passedTextFromForm1(textBox1.Text);
}
}
狗类:
public class Dogs
{
// global variable to hold form1
public Form1 MyForm1;
public Dogs(Form1 form1Istance)
{
this.MyForm1 = form1Istance;
}
public void showTextBox1Text()
{
// do not forget to set the access modifier of TextBox1 to public (you can do it in the Designer, the propery is "Modifier")
System.Windows.Forms.MessageBox.Show("showTextBox1Text(): " + this.MyForm1.textBox1.Text);
}
public void passedTextFromForm1(string txtfromtextbox1)
{
// another way to share data between instances
System.Windows.Forms.MessageBox.Show("passedTextFromForm1(): " + txtfromtextbox1);
}
}
TextBox
控件的访问修饰符可以在此处更改(公开以与其他类共享):