C#报错:Non-Invocable member 'ButtonBase.Text' cannot be used like a method 求助
Hey there! Let's break down what's causing this error and get your random champion selector up and running smoothly.
What's Going Wrong?
The error is pointing directly to this line in your RandomChampionSelector_Click method:
RandomChampionSelector.Text("Here is ", champions[cIndex]);
The core issue is simple: Text is a property of your button, not a method. Properties are used to get or set values (like the text displayed on a control), so you can't call them with parentheses like you would a function. Instead, you need to use the assignment operator (=) to set its value.
How to Fix It
You want to combine the static string "Here is " with your randomly selected champion name. There are two clean, idiomatic ways to do this in C#:
String concatenation with
+:
Replace the problematic line with this straightforward assignment:RandomChampionSelector.Text = "Here is " + champions[cIndex];String interpolation (recommended for readability):
C# has a cleaner syntax for building dynamic strings using$and curly braces to inject variables directly:RandomChampionSelector.Text = $"Here is {champions[cIndex]}";
Quick Reality Check
You actually already used the correct property assignment syntax earlier in your code! Look at the Form1_Load method:
RandomChampionSelector.Text = "Random champ";
That's exactly how you should set the Text property everywhere else—you just needed to extend that pattern to include your dynamic champion name.
Full Corrected Click Method
Here's what your fixed RandomChampionSelector_Click method will look like:
private void RandomChampionSelector_Click(object sender, EventArgs e) { string[] champions = new string[] { "Ahri", "Akali", "Amumu", "Ashe", "Azir", "Brand", "Draven", "Elise", "Evelynn", "Fiddlesticks", "Fiora", "Fizz", "Gangplank", "Garen", "Heimerdinger", "Jax", "Jhin", "Kalista", "Katarina", "Kayle", "Kayn", "LeBlanc", "Lee Sin", "Lucian", "Lux", "Malphite", "Maokai", "Master Yi", "Morgana", "Poppy", "Quinn", "Rengar", "Ryze", "Shaco", "Singed", "Sion", "Sivir", "Sona", "Soraka", "Taric", "Teemo", "Tristana", "Twisted Fate", "Udyr", "Vayne", "Veigar", "Warwick", "Xayah", "Xin Zhao", "Yasuo", "Zed", "Zilean", "Zoe" }; Random rnd = new Random(); int cIndex = rnd.Next(champions.Length); RandomChampionSelector.Text = $"Here is {champions[cIndex]}"; // Fixed line }
That should eliminate the error and make your button display the random champion correctly when clicked!
内容的提问来源于stack exchange,提问作者RCO Gamer

