试图了解函数之间的C#,WPF和用户输入

时间:2019-03-05 03:02:45

标签: c# arrays wpf function

我还是C#的新手。我试图更好地学习它,这对我自己是一个基本的编程挑战,我试图理解在提交到列表中显示的表后如何从用户输入/文本框中移动或返回某些值。< / p>

这是我的“挑战”,我正在尝试创建一个简单的程序,该程序具有2个文本框,一个用于将新值的名称添加到列表中(不是一个我很难学的数组),另一个用于所述列表中搜索到的值的名称。每个文本框的“提交”按钮上都有一条消息,指出添加时为“已添加值”,或者为搜索按钮为“已找到”,“不存在”。然后,在所述框和按钮的一侧,我实际上想显示带有可滚动2列窗口/框的列表,将第一列作为表中的位置(如value所在的位置),然后添加该值的实际名称。 (哦,列表本身也是个清除按钮)

所以这是我到目前为止收集的内容。我知道我必须将所有输入转换为字符串,然后将其推入列表。我知道如何显示MessageBox.Show(""),但是我不知道如何为它编写条件代码。我将尝试一个简单的if (),但首先需要编程一个有效的搜索功能,该功能需要从列表中推入和拉出数据。我知道JavaScript具有array.pusharray.indexof,这使得查找和将事物推入数组相当简单,但是据我所知,C#没有该功能。

我对此并不陌生,因此,希望阅读任何有助于我学习C#的材料的技巧或有关如何正确完成此工作的任何技巧。我最大的努力是将所述文本框中的值返回到另一个private void中并在我的var中使用它,换句话说,将一个函数的乘积推入另一个函数中(如下面的示例将Add_Text.Text插入var names = new List<string>();上方的using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace ArrayApp { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } // ARRAY CODING / LIST CODING public class Values { public string Position { get; set; } = string.Empty; public string Name { get; set; } = string.Empty; } public void App() { var names = new List<string>(); } // BUTTON CLICKS / BUTTON ACTION CODING private void Add_Button_Click(object sender, RoutedEventArgs e) { List_Box.Content = Add_Text.Text; MessageBox.Show("Value Added"); Add_Text.Clear(); } private void Search_Button_Click(object sender, RoutedEventArgs e) { } // TEXT BOXES / WHAT BUTTON ACTUALLY INPUTS INTO OUR DISPLAY private void Add_Text_TextChanged(object sender, TextChangedEventArgs e) { } private void Search_Text_TextChanged(object sender, TextChangedEventArgs e) { } // DISPLAY - List_Box not added yet } } 中。无论如何,这是我的编码或尝试使此“工作”失败的尝试。

"\\APP401\I\Run\Folder\Client\20171031\25490175\Data\brtbvsch\" -replace "^\\",'\\10.0.0.1\share'

2 个答案:

答案 0 :(得分:2)

让我们逐步完成。如前所述,您需要一些存储数据的工具。 List是个好主意,因为您不知道大小。 当前,您正在创建类型为List的{​​{1}},它可以正常工作。
实际上,不需要string类,因为因为您可以使用名为Values的函数来获取项目的索引-以后再更多。

现在,添加项目后显示IndexOf后,还必须将其实际添加到MessageBox列表中。为此,请在类中声明names并在构造函数中对其进行初始化。这样一来,您就可以从课堂上的任何地方进行访问。

List

可以使用private List<string> names; public void MainWindow() { InitializeComponent(); names = new List<string>(); } 方法添加项目,这很简单。

.Add

搜索项目也很容易。如果您想知道项目是否存在,只需使用private void Add_Button_Click(object sender, RoutedEventArgs e) { List_Box.Content = Add_Text.Text; MessageBox.Show("Value Added"); names.Add(Add_Text.Text); // Adding the content of Add_text.Text Add_Text.Clear(); } ,或者如果您也想拥有索引,请使用Contains。注意:如果找不到任何内容,IndexOf将返回IndexOf

-1

private void Search_Button_Click(object sender, RoutedEventArgs e) { if(names.Contains( SEARCH_TEXT.TEXT /* or wherever you get your pattern from */ )){ // found, display this in some way } else { // not found, display this is some way } } 包含您要寻找的模式。我不知道您如何命名控件,只需替换它即可。

就是这样。

答案 1 :(得分:0)

因此,在阅读了一些内容之后,您的评论也大有帮助,我认为我很了解它,并且至少对C#的逻辑运作方式有一些基本的了解。这是我尝试创建的AMAZING程序的“最终”版本。谢谢大家的帮助!

P.S。这些注释供我将来在学习C#或忘记事情时学习和参考:)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


/* QUICK TERMINOLOGY

    List = An Array that constantly adjusts its maximum size

    names = Our actual "database" or table with our values that we've inputted

    List_Box = The display table that is visible on the program itself

    var = Variable... You know...

    "public" or "private" = Those define whether the function is visible to the rest of the program or other "sheets" in the program

    void = Defines whether the return value or the output of a function should be something, void means not specified, 
            if you want string you put string, if you want an integer you put int... etc etc.

*/



namespace ArrayApp
{
    public partial class MainWindow : Window
    {

        /* Private Function for Creating the List which will be a String
        We are using a List instead of an Array as an Array needs 
        a specific amount of indexes so if we have a specific number of data or 
        a maximum amount of data that a user can input then array would be used
        but since we don't know how many indexes we need a list will automatically
        adjust the maximum size of our table to suit our needs. I.e. limitless  */

        private List<string> names;
        public MainWindow()
        {
            InitializeComponent();
            names = new List<string>();

        }

        /* Class for our Items in our list this is not referring the list above but...
        the list that it displays as we have a search on demand
        but also allows us to search for specific people in the List (array/table) rather than
        display over 100+ people, if our database was to get to that size.

        Our class function defines what data can be put into our Display List ( List_Box ) 
        Therefore the index can only be an integer and name can only be a string
        more on this later.  */

        class Items
        {
            public int Index { get; set; }
            public string Name { get; set; }
        }

        /* The Add Button Function
        This button takes the content of the TextBox that is right next to it and 
        adds it to our list called "names" but does not update our display, instead
        it shows us a message stating that the value was added to the list.
        If we were using an Array with a limited size, we could use an IF to check
        if there is a space available and output a message saying "Full" or "Failed"  */

        private void Add_Button_Click(object sender, RoutedEventArgs e)
        {
            names.Add(Add_Text.Text); // Adds the value

            Add_Text.Clear(); // Clears the text box

            MessageBox.Show("Value Added"); // Displays a message
        }


        /* Firstly...

         * We have an IF function that checks whether "names" contains the content
                of the search box, so if its a letter "a", it checks if its in our list.
         * It then creates a variable "SearchText" that we can later use that simply
                means that instead of writing the whole code we can just refer to it by our new name
         * Another variable! This one defines our Index in our list, it takes
                our previous variable and looks for it in our list and finds what
                the index number of that value is.
         * Now, since its Search on demand we essentially have two Lists (arrays) now
                that we have the name of the value we looking for in string format,
                we also have our index as integer (defined earlier in class). We need to take that data
                and add it to our display List and for that we have our function.
                Adds new Items to our list using the Items Class and also defines
                what data should be put into each column.
         * It then clears the search text box
         * and shows us that the value has been found.

         We then move to ELSE which is simple really...

            * Didn't find data
            * Clears search text box
            * Displays message that its not been found...  */


        private void Search_Button_Click(object sender, RoutedEventArgs e)
        {
            if (names.Contains(Search_Text.Text))  // Our If statement
            {
                var SearchText = Search_Text.Text;  // First Variable

                var FindIndex = names.IndexOf(SearchText);  // Second Variable

                List_Box.Items.Add(new Items() { Index = FindIndex, Name = SearchText});  // Adding items to display list

                Search_Text.Clear();  // Clear search text box

                MessageBox.Show("Data Found"); // Display message
            }
            else
            {
                Search_Text.Clear();

                MessageBox.Show("Not Found");
            };
        }

        /* Lastly a simple clear button for our display list.
         * Once a user searches for many values and wants to clear the display list
         * he can do it by hitting a single button.
         * 
         * This button DOES NOT delete anything from our "names" list it only
         * clears the display data meaning that the user can search for more data
         * that has been added already.  */


        private void Clear_Button_Click(object sender, RoutedEventArgs e)
        {
            List_Box.Items.Clear();
        }

        private void Add_Text_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void Search_Text_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

    }
 }

Here is how it looks, simple but you get the idea, I'm learning...