简体   繁体   English

如何返回两个值但仅在文本框中显示一个并在 C# 中隐藏另一个

[英]How can I return two values but only show one in a textbox and hide the other in C#

I want to display only the quote in the quote textbox but I also want to display the quotee name in the quotee textbox the problem is I can only return one value if I return both values they kinda go together in the quote textbox我只想在报价文本框中显示报价,但我也想在报价文本框中显示报价人姓名问题是如果我在报价文本框中同时返回两个值,它们有点像 go

public static string DisplayQuote() 
{
    Dictionary<string, string> quotes = new Dictionary<string, string>();
    quotes.Add("Dr. Seuss",
        "\"You’re off to great places, today is your day. Your mountain is waiting, so get onyour way.\"");
    quotes.Add("A.A. Milne",
        "\"You’re braver than you believe, and stronger than you seem, and smarter than you think.\"");
    quotes.Add("Willie Nelson", "\"Once you replace negative thoughts with positive ones, you’ll start having positive results.\"");
    quotes.Add("Les Brown", 
        "\"In every day, there are 1,440 minutes. That means we have 1,440 daily opportunities to make a positive impact.\"");
    quotes.Add("Dr Seuss",
        "\"When you are enthusiastic about what you do, you feel this positive energy. It’s very simple.\"");

    int index = 0;
    for (int i = 0; i < 5; i++)
    {
        index = randomQuote.Next(quotes.Count);
    }
    _quote = quotes.Values.ElementAt(index).ToString();
    _quotee = quotes.Keys.ElementAt(index).ToString();
    return (_quote, _quotee).ToString();
}

My WPF:我的 WPF:

namespace DailyQuotes
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
    
            DailyQuote daily = new DailyQuote();
            txtQuotee.Text = daily.Quoteee;
            txtPeriod.Text = daily.GetPeriod(); 
            txtQuote.Text = daily.DisplayQuote();
            txtTime.Text = daily.Time.ToString("h:mm tt");
        }
    }
}

Why don't you just return the tuple instead of calling ToString on it?为什么不直接返回元组而不是调用ToString呢?

public static (string quote, string quotee) DisplayQuote() 
{
    ....
    return (_quote, _quotee);
}

Then you can access the two returns like this:然后你可以像这样访问两个返回:

DailyQuote daily = new DailyQuote();
txtQuotee.Text = daily.quoeee;
txtQuote.Text = daily.quote;

This requires named tuples , which are available from C# 7.0 and onwards.这需要命名元组,可从 C# 7.0 及更高版本获得。 If you don't have C# 7.0 you'll need to use unnamed Tuples like so;如果您没有 C# 7.0,则需要像这样使用未命名的元组;

public Tuple<string,string> DisplayQuote()
{
    ...
    return new Tuple<string,string>(_quote, _quotee);
}

var tuple = DisplayQuote();
txtQuotee.Text = tuple.Item1;
txtQuote.Text = tuple.Item2;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使C#按钮仅单击一次并使文本框显示两个结果? - How to make C# button click only one time and make a textbox show two result? 在C#网站中,我们如何连接两个用户,以便他们可以彼此传递文本框值? - in c# website how we can connect two users so that they can pass textbox values to each other? 从JObject仅返回一个值,并且其中包含两个值C# - Return only one value from JObject with two values in it C# 如何在C#中显示一个用户控件并隐藏其余用户控件 - How can I show one user control and hide the rest in C# 如何在特定文本框 c# 中显示 xml 节点 - How can I show xml node into specific textbox c# 如何将文本从表单返回到文本框C#? - How can I return text from a form to a textbox C#? 尝试使用C#和WPF调整文本框中的TextBox +字体大小,只能做到其中之一 - Trying to resize TextBox + font size in text box with C# and WPF, can only do one or the other 如何比较两个列表并使用C#相应地设置像素值? - How can I compare two Lists against each other and set the pixels values accordingly using C#? 如何使用C#在Oracle中基于其他两个列的值获取列的值? - How can i get value of column based on values of two other columns in Oracle Using C#? 将两个文本框值添加到第三个C#中时出错 - Error with adding two textbox values into third one C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM