让组合框文本分类并在文本框中返回结果

问题描述 投票:-1回答:1

所以我在做一个项目,但被困住了。我想将组合框的文本传递给我的班级,并将结果打印在文本框中。

这是我的课程:

class Meateater
{
    public static string Carnivor(string flesheater)
    {
        string wordsInTextbox = flesheater;
        return flesheater;
    }

这是我的表单代码:

private void button1_Click(object sender, EventArgs e)
{
    comboBox1.Text = Carnivoor.Carnivor(textBox1.Text);
}

当我按下button1时,组合框中选择的文本消失了,结果我的文本框保持为空,消息框显示为空字符串。我不知道如何解决此问题,所以有人可以帮助我吗?

c# winforms combobox textbox
1个回答
0
投票

您写道:

我想将组合框的文本传递给我的班级,并将结果打印在文本框中。

我不知道MeatEater与它有什么关系。

因此,当按下按钮时,您要在comboBox1中读取所选项目的文本,并将此文本放入textBox1。

因此,我们首先创建一个函数来读取ComboBox中的选定项目,并在TextBox中显示一些文本。这些功能是窗口的一部分(或Dialog,UserControl等)

public string ReadComboBox1()  // TODO: invent a proper name describing what comboBox1 represents
{
    return this.ComboBox1.SelectedItem.ToString();
}

public void WriteTextBox1(string text) // TODO: proper name describing textBox1
{
    this.textBox1.Text = text;
}

当按下Button1时,您要从ComboBox1中读取所选项目的文本并在TextBox1中显示它

public void OnButtonPressed(object sender, EventArgs e)
{
    if (object.ReferenceEquals(sender, this.Button1)
    {
        // do what you need to do when Button1 is pressed:
        string selectedText = ReadComboBox1();
        // TODO: do your MeatEater magic here
        string textAfterMeatEaterMagic = ...
        WriteTextBox1(textAfterMeatEaterMaghic);
    }
}

简单的漫画卓悦!

© www.soinside.com 2019 - 2024. All rights reserved.