繁体   English   中英

WPF 中的 IComparable

[英]IComparable in WPF

我需要实现一个搜索按钮,它将我的TextBox中的值与数组中的值进行比较,如果它们匹配,则返回索引,否则将返回-1 我已经实现了doubleint的随机数组以及所有的界面和按钮。 必须使用IComparableCompareTo()但我不知道如何实现它。 我尝试实现Search方法,但它不起作用,我不知道如何在SearchButton_Click事件处理程序中调用它。

这就是我到目前为止所拥有的:

WPF应用程序

public partial class MainWindow : Window 
{
    int[] numb = new int[6];
    double[] numb2 = new double[6];

    public MainWindow () 
    {
        InitializeComponent ();
    }

    //Create Int
    private void Button_Click (object sender, RoutedEventArgs e) 
    {
        resultsBox.Items.Clear ();
        resultsBox.Items.Add ("Index Value\n");
        Random rnd = new Random ();
        for (int i = 0; i < 6; i++) {
            numb[i] = rnd.Next (0, 999);
            string m = i.ToString () + "\t" + numb[i].ToString ();
            resultsBox.Items.Add (m);
        }
    }

    //Create Double
    private void CreateDouble_Click (object sender, RoutedEventArgs e) 
    {
        resultsBox.Items.Clear ();
        resultsBox.Items.Add ("Index Value\n");
        Random rnd = new Random ();
        for (int i = 0; i < 6; i++) {
            numb2[i] = Math.Round (rnd.NextDouble () * (999), 2);
            string m = i.ToString () + "\t" + numb2[i].ToString ();
            resultsBox.Items.Add (m);
        }
    }

    //Search
    private void SearchButton_Click (object sender, RoutedEventArgs e) { }

    static int Search<T> (T[] dataArray, T searchKey) where T : IComparable<T> 
    {
        //Iterate through the array.
        for (int iter = 0; iter < dataArray.Length; iter++) {
            //Check if the element is present in the array.
            if (dataArray[iter].CompareTo (searchKey) == 0) {
                //Return the index if the element is present in the array.
                return iter;
            }
        }
        //Otherwise return the index -1.
        return -1;
    }
}

谢谢!

首先,您必须为TextBox命名,以便您可以在代码隐藏中引用它,以便检索搜索键。 然后在单击按钮时,您必须检查搜索键的数字类型,以确定搜索目标数组。 然后将两个参数传递给Search()方法:

主窗口.xaml

<Window>
  ...

  <TextBox x:Name="SearchInputBox` />
  ...
</Window>

主窗口.xaml.cs

public partial class MainWindow : Window 
{
  private readonly int arraySize = 6;
  private int[] IntegerValues { get; }
  private double[] DoubleValues { get; }

  public MainWindow() 
  {
    InitializeComponent();
    this.IntegerValues = new int[arraySize];
    this.DoubleValues = new double[arraySize];
  }

  // Create Int
  private void Button_Click(object sender, RoutedEventArgs e) 
  {
    resultsBox.Items.Clear();
    resultsBox.Items.Add("Index Value\n");

    Random rnd = new Random();
    for (int index = 0; index < arraySize; index++) 
    {
      this.IntegerValues[index] = rnd.Next(0, 999);
      string item = index.ToString() + "\t" + this.IntegerValues[index].ToString();
      resultsBox.Items.Add(item);
    }
  }

  // Create Double
  private void CreateDouble_Click(object sender, RoutedEventArgs e) 
  {
    resultsBox.Items.Clear();
    resultsBox.Items.Add("Index Value\n");

    Random rnd = new Random();
    for (int index = 0; index < arraySize; index++) 
    {
      this.DoubleValues[index] = Math.Round(rnd.NextDouble() * 999, 2);
      string item = index.ToString() + "\t" + this.DoubleValues[index].ToString();
      resultsBox.Items.Add(item);
    }
  }

  // Handle search button clicked
  private void SearchButton_Click(object sender, RoutedEventArgs e) 
  {
    string numericString = this.SearchInputBox.Text;

    int resultIndex = -1;
    if (int.TryParse(numericString, out int integerSearchPredicate)
    {
      resultIndex = MainWindow.Search(this.IntegerValues, integerSearchPredicate);
    }
    else if (double.TryParse(numericString, out int doubleSearchPredicate)
    {
      resultIndex = MainWindow.Search(this.DoubleValues, doubleSearchPredicate);
    }
  }

  static int Search<T>(T[] dataArray, T searchKey) where T : IComparable<T> 
  {
    // Iterate over the array.
    for (int index = 0; index < dataArray.Length; index++) 
    {
      // Check if the element is present in the array.
      if (dataArray[index].CompareTo(searchKey) == 0) 
      {
        //Return the index if the element is present in the array.
        return index;
      }
    }

    // Otherwise return the index -1.
    return -1;
  }
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM