C#组合框选择的项目更改(从单击的选项到底部的选项)

时间:2019-09-11 13:28:09

标签: c# winforms combobox google-sheets-api

当我选择一个选项或更改组合框中的selectedindex时,它会显示为底部项目,然后再切换到所选项目。

以下是该问题的视频:https://imgur.com/gallery/5CTuSjI

我对C#还是很陌生,所以如果我的代码没有经过优化/简洁,我深感抱歉。

代码中包含一些敏感项目,例如API密钥和安全信息,因此某些内容将丢失。

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
namespace Yugioh_Program
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // Events

        private void Label2_Click(object sender, EventArgs e)
        {

        }

        private void TextBox1_TextChanged(object sender, EventArgs e)
        {

        }

加载功能(通过电子表格设置组合框的值)

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] _scopes = { SheetsService.Scope.Spreadsheets };
            string _applicationName = "My Application Name from Google API Project ";
            string _spreadsheetId = "CONFIDENTIAL";
            string sheet = "ALL";

            SheetsService service;
            GoogleCredential credential;

            using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
            {
                credential = GoogleCredential.FromStream(stream)
                    .CreateScoped(_scopes);
            }

            service = new SheetsService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = _applicationName,
            });

            List<List<string>> searchedCards = new List<List<string>>();

            var range = $"{sheet}!B2:G";
            var request = service.Spreadsheets.Values.Get(_spreadsheetId, range);

            var response = request.Execute();
            var values = response.Values;
            if (values != null && values.Count > 0)
            {
                foreach (var row in values)
                {
                    searchedCards.Add(new List<string> { row[0].ToString(), row[1].ToString(), row[2].ToString(), row[3].ToString(), row[4].ToString(), row[5].ToString() });
                }

            }

            Cards item = new Cards();

            List<string> addedCards = new List<string>();
            List<string> addedCardsNoDupe = new List<string>();

            foreach (List<string> subList in searchedCards)
            {
                addedCards.Add(subList[0]);
                addedCardsNoDupe = addedCards.Distinct().ToList();
            }
            foreach (string addedCard in addedCardsNoDupe)
            {
                item.Text = addedCard;
                item.Value = addedCard;
                NameSearch.Items.Add(item);
            }
        }

SelectedIndexChanged(当更改上方组合框的内容时,从电子表格中提取值,使用上方组合框中选择的选项对其进行过滤,然后将其添加到列表中。


        private void NameSearch_SelectedIndexChanged(object sender, EventArgs e)
        {

            // API call
            string[] _scopes = { SheetsService.Scope.Spreadsheets };
            string _applicationName = "My Application Name from Google API Project ";
            string _spreadsheetId = "CONFIDENTIAL";
            string sheet = "ALL";

            SheetsService service;
            GoogleCredential credential;

            using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
            {
                credential = GoogleCredential.FromStream(stream)
                    .CreateScoped(_scopes);
            }

            service = new SheetsService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = _applicationName,
            });

            List<List<string>> searchedCards = new List<List<string>>();
            List<List<string>> searchedSets = new List<List<string>>();

            var range = $"{sheet}!B2:G";
            var request = service.Spreadsheets.Values.Get(_spreadsheetId, range);

            var response = request.Execute();
            var values = response.Values;
            if (values != null && values.Count > 0)
            {
                foreach (var row in values)
                {
                    searchedCards.Add(new List<string> { row[0].ToString(), row[1].ToString(), row[2].ToString(), row[3].ToString(), row[4].ToString(), row[5].ToString() });
                }
                foreach (List<string> subList in searchedCards)
                {
                    //   name / set / rarity / edition / quantity / row number
                    searchedSets.Add(new List<string> { subList[0].ToString(), subList[1].ToString(), subList[2].ToString(), subList[3].ToString(), subList[4].ToString(), subList[5].ToString() });
                }
            }

            string selectedCard = NameSearch.Text.ToString();
            label1.Text = selectedCard;
            SetSearch.Items.Clear();

            Cards item = new Cards();

            foreach (List<string> subSet in searchedSets)
            {
                if (selectedCard == subSet[0])
                {
                    string addedText = subSet[0] + " | " + subSet[1] + " | " + subSet[2] + " | " + subSet[3];

                    item.Text = addedText;
                    item.Value = 0;

                    SetSearch.Items.Add(item);

                }
            }
        }
    }
}

当我从第一个组合框中选择“黑暗魔术师”时,预期结果是selectedCard将=“黑暗魔术师”,第二个组合框将被其过滤。

相反,正在发生的事情是在第一个组合框显示所选选项之前,选择了第一个组合框中的最后一个列表项(“星尘龙”),并且selectedCard =“ stardust dragon”而不是预期的所选项。

寻求代码帮助的新手,因此,如果您需要更多信息/上下文等,请询问:)

1 个答案:

答案 0 :(得分:0)

在表单中添加一个成员级布尔标志,以指示该表单正在加载。在 NameSearch_SelectedIndexChanged 事件处理程序中,检查表单是否正在加载;如果只是退出处理程序;否则,继续。

public partial class Form1 : Form
{

    boolean _formLoading;

    private void Form1_Load(object sender, EventArgs e)
    {
        _formLoading = true;
        ...
        ...
        ...
        _formLoading = false;
    }

    private void NameSearch_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (_formLoading) return;
        ...
        ...
        ...
    }
}