首先,我对编码的大部分经验是通过python,因此我使用了global
我有一个列表,我希望能够搜索列表并查找x
长度的所有单词。我的主要问题是不知道如何从事件监听器内部调用函数。
因此myList
在我的函数functionOne
中生成,我想在functionTwo
中使用此列表,最后我想在buttonOne
时调用它点击。 E.g buttonOne_Click ....
这是我到目前为止functionOne
public List<string> noDup(List<string> myList)
{
var convert = myList.ConvertAll(i => i.ToLower());
List<string> remove = convertLower.Distinct().ToList();
return remove;
}
functionTwo
public List<string> length(List<string> myList)
{
int i = int.Parse(lengthSearch.Text);
List<string> temp = new List<string>();
foreach(string item in myList)
{
if(item.Length == i)
{
temp.Add(item);
}
}
searchResult.Text = string.Join(",", temp);
}
最后是我的事件监听器
private void button_Click(object sender, EventArgs e)
{
length(remove);
}
注意:remove
是我尝试使用的列表
但此代码没有输出,因为我收到错误消息说在此上下文中不存在删除操作,我不确定如何创建此列表global
答案 0 :(得分:0)
我们在这里是如何在win form c#应用程序中实现这一目标的总体思路。
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private List<string> _remove;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
length(_remove);
}
public List<string> noDup(List<string> myList)
{
var convert = myList.ConvertAll(i => i.ToLower());
_remove = convertLower.Distinct().ToList();
}
public List<string> length(List<string> myList)
{
int i = int.Parse(lengthSearch.Text);
List<string> temp = new List<string>();
foreach (string item in myList)
{
if (item.Length == i)
{
temp.Add(item);
}
}
searchResult.Text = string.Join(",", temp);
}
}
}
请注意,由于我刚刚使用了您的代码,因此无法构建。例如,length
方法应该返回List<string>
但不会返回_remove
。您可能希望更改传入的列表然后返回它,或者改变类级变量button1_Click
。
无论哪种方式,事件处理程序_remove
都需要使用类级别变量(例如我添加的#include <iostream>
using namespace std;
int main()
{
double type_ticket, num_tickets, price1, price2, price3, total_price, decision;
cout << "Welcome to the ticket kiosk.";
cout << "\n";
cout << "\n";
cout << "1. VVIP - RM 200";
cout << "\n";
cout << "2. VIP - RM 150";
cout << "\n";
cout << "3. Normal - RM 100" << endl;
cout << "\n";
do
{
cout << "Please select the category of ticket you would like to purchase: ";
cin >> type_ticket;
cout << "\n";
if (type_ticket == 1)
{
cout << "How many would you like: ";
cin >> num_tickets;
cout << "\n";
price1 = num_tickets * 200;
cout << "The price is: RM " << price1 << endl;
cout << "\n";
cout << "\n";
cout << "1. YES" << endl;
cout << "2. NO" << endl;
cout << "\n";
cout << "Would you like to continue purchasing more tickets: ";
cin >> decision;
cout << "\n";
}
else if (type_ticket == 2)
{
cout << "How many would you like: ";
cin >> num_tickets;
cout << "\n";
price2 = num_tickets * 150;
cout << "The price is: RM " << price2 << endl;
cout << "\n";
cout << "\n";
cout << "1. YES" << endl;
cout << "2. NO" << endl;
cout << "\n";
cout << "Would you like to continue purchasing more tickets: ";
cin >> decision;
cout << "\n";
}
else if (type_ticket == 3)
{
cout << "How many would you like: ";
cin >> num_tickets;
cout << "\n";
price3 = num_tickets * 100;
cout << "The price is: RM " << price3 << endl;
cout << "\n";
cout << "\n";
cout << "1. YES" << endl;
cout << "2. NO" << endl;
cout << "\n";
cout << "Would you like to continue purchasing more tickets: ";
cin >> decision;
cout << "\n";
}
else
{
cout << "You have entered an invalid input, please try again. " << endl;
cout << "\n";
}
}
while (decision == 1);
total_price = price1 + price2 + price3;
cout << "The grand total is: RM " << total_price << endl;
cout << "\n";
cout << "Thank you for using this service today, we hope you enjoy the show." << endl;
cout << "\n";
}
)或调用将返回列表的方法(如另一个{ {3}}由Zbigniew撰写。)
答案 1 :(得分:0)
将所有函数放在同一个类中,并在列表实例上使用noDup。
public List<string> noDup(List<string> myList)
{
var convert = myList.ConvertAll(i => i.ToLower());
List<string> remove = convertLower.Distinct().ToList();
return remove;
}
public void length(List<string> myList)
{
int i = int.Parse(lengthSearch.Text);
List<string> temp = new List<string>();
foreach(string item in myList)
{
if(item.Length == i)
{
temp.Add(item);
}
}
searchResult.Text = string.Join(",", temp);
}
private void button_Click(object sender, EventArgs e)
{
var remove = noDup(yourInputList);
length(remove);
}