用于更改实例值的文本框 [英] Textbox to change instance value

查看:45
本文介绍了用于更改实例值的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过文本框将新变量设置为我的 Filed(实例)值到新值中...但我不知道如何制作...这是我的代码,希望我能从中获得一些见解别人.

I'm trying to set new variable into my Filed (instance) value into new value through text box... but I've no idea how to make it.. here is my code wish I can get some insight from someone else.

private string barkSound;
private string breed;
private int dogHeight;
private string dogColour;
private static int noOfLegs;

所有的 get 和 set 都已设置.

All the get and set has been set.

public Dog()   
{
    barkSound = "Woof!";
    breed = "cocker spaniel";
    dogHeight = 10;
    dogColour = "white";   
}

public string GetSpeech()
{
    dogSpeech = "Hello. I am a " + breed + ". " + barkSound + "\n I'm " 
                    + (IsBig()? "Big" : "Small") +", i got " + DogHeight +" CM, Colour is " + dogColour + ". I have " + noOfLegs + " Legs" ;
    return dogSpeech;
}

public Dog()
{
    barkSound = "Woof!";
    breed = "cocker spaniel";
    // Question(C) add Constructor for dogHeight. dogColour and breed.
    dogHeight = 10;
    dogColour = "white";
}

推荐答案

好的,我已经创建了一个像你一样的表单,在创建"按钮上我创建了新的 Dog.

Okay, I have created an form like you, At the Create button I created new Dog.

这是我的Dog类,

public class Dog
        {
            private string barkSound;
            private string breed;
            private int dogHeight;
            private string dogColour;
            public Dog(string bark, string breed, int height, string color)
            {
                this.barkSound = bark;
                this.breed = breed;
                this.dogHeight = height;
                this.dogColour = color;

            }
            public string BarkingSound { get { return this.barkSound; } }
            public string BreedName { get { return this.breed; } }
            public string Height { get { return this.dogHeight.ToString(); } }
        }

这是创建按钮的点击(我有 Dog 列表来添加狗);

Here is the Create button's click (And I have Dog list to add Dogs to it);

List<Dog> dogs = new List<Dog>();
private void createBtn_Click(object sender, EventArgs e)
        {
            Dog myDog = new Dog(textBox1.Text, textBox2.Text, int.Parse(textBox3.Text), textBox4.Text);
            dogs.Add(myDog);
        }

在 GetDogs Button 的 Click 处,我遍历所有狗并将它们添加到 listbox.

At GetDogs Button's Click I loop over all dogs and add them into listbox.

 private void button1_Click(object sender, EventArgs e)
        {   listBox1.Items.Clear();
            foreach (var item in dogs)
            {
                string text = "Hello. I am a " + item.BreedName + ". " + item.BarkingSound + "etc";
                listBox1.Items.Add(text);
            }
        }

问题不清楚,也许你想改变狗的现有价值.

The question is not clear, maybe you want to change the existing value of the dog.

所以首先我们需要在 Dog 类中进行更改.

So first we need to make change at Dog class.

public class Dog{
        private string barkSound;
        private string breed;
        private int dogHeight;
        private string dogColour;
        public Dog(string bark, string breed, int height, string color)
        {
            this.barkSound = bark;
            this.breed = breed;
            this.dogHeight = height;
            this.dogColour = color;

        }
        public string BarkingSound { get { return this.barkSound; }  set { this.barkSound = value; }  } // so we can set variables like this dog.BarkingSound = "bla"
        public string BreedName { get { return this.breed; } }
        public string Height { get { return this.dogHeight.ToString(); } }
    }

我们为什么要这样做?

listbox 的选定索引更改事件,首先我们需要使用ElementAt() 在我们的列表中找到Dog.然后检查 BarkSound 的文本框,如果它不是空的,则将其设置为现有 Dog 的 BarkingSound,

At listbox's selected index change event, first we need to find the Dog inside our list by using ElementAt(). Then Check textbox of BarkSound, if it is not empty set it to existing Dog's BarkingSound,

 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                Dog TempDog = dogs.ElementAt(listBox1.SelectedIndex);
                TempDog.BarkingSound = textBox1.Text;
            }

        }

尝试用两种方式进行解释,希望其中一种对您有用.

Tried to explain with both way, Hope that one of them will be useful for you.

这篇关于用于更改实例值的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆