首页 > 解决方案 > == 不适用于字符串和字符串数组值

问题描述

基本代码是这样的

public string[,] values = new string[2, 100];
public string category_read;
 
if(values[1, 0] == category_read)
{
output_textbox.Text = "test-check";
}

两个变量都填充了一个字符串(我检查过),并且看起来完全一样。

我想做什么的基本描述:我想做一个可以保存文本的应用程序,给它们一个类别和一个名称。然后将文本内容保存在其自己的 .txt 文件中。其余的保存在两个已经存在的文件中。当我切换到读取数组值时,数组值被填满,应该使用所选类别进行检查,然后相应的名称应该显示在清单中。然后我可以在那里选择我想读哪一个,但我还没有写。

如果有更好的方法来存储数据的更好的方法来做任何事情,请告诉我。

完整的代码在这里以防万一。代码还没有清理,我测试了很多,所以我需要稍后删除一些不必要的东西。请不要恨我,我很新。

        
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace C_Sharp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            checkedListBox1.CheckOnClick = true;
            
        }
        public string[,] values = new string[2, 100];
        public string[] shown = new string[100];
        public string device_name;
        public string category_read;
        public int count = 0;
        private void read_button_Click(object sender, EventArgs e)
        {
            write_panel.Hide();
            read_panel.Show();

            string path_category = device_name + @"\c-sharp\Files\all_category.txt";
                string path_name = device_name + @"\c-sharp\Files\all_names.txt";

                StreamReader cat = new StreamReader(path_category);
                StreamReader nam = new StreamReader(path_name);
                

                string data_cat = cat.ReadToEnd();
                string data_nam = nam.ReadToEnd();
                cat.Close();
                nam.Close();
                string[] current_cat = data_cat.Split('\n');
                string[] current_nam = data_nam.Split('\n');
                for (int i = 0; i < current_cat.Length; i++)
                {
                    values[0, i] = Convert.ToString(current_nam[i]);
                    values[1, i] = Convert.ToString(current_cat[i]);
                }
            
        }

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string path  = device_name + @"\c-sharp\Files\" + category_read + @"\" + checkedListBox1.Text + ".txt";
        }

        private void write_button_Click(object sender, EventArgs e)
        {
            write_panel.Show();
            read_panel.Hide();
        }

        private void search_textbox_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void search_textbox_Click(object sender, EventArgs e)
        {

        }

        private void category_comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            checkedListBox1.Items.Clear();
            object category_obj = category_comboBox.SelectedItem;
            category_read = Convert.ToString(category_obj);

            for (int i = 0; i < 100; i++)
            {
                if(values[1, i] == category_read)
                {
                    output_textbox.Text = "cool story bro";
                    checkedListBox1.Items.Add("cool");
                }
            }
            
        }


        private void search()
        {
            
            try
            {   
                //PC Version
                //string userPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
                //string path = @"" + @userPath + @"\Desktop\test_for_c-sharp\text1.txt";
                
                //USB-Stick Version
                string path = device_name + @":\test_for_c-sharp\text1.txt";
                StreamReader stream = new StreamReader(path);
                string filedata = stream.ReadToEnd();
                output_textbox.Text = filedata;
                
            }
            catch { }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            read_panel.Hide();
            write_panel.Hide();
            this.Height = 490;
            //"D:\c-sharp\Files\all.txt"

        }
        private void save_button_Click(object sender, EventArgs e)
        {
            try
            {
                TextWriter txt = new StreamWriter(device_name + @"\c-sharp\Files\" + category_write_combobox.SelectedItem + @"\" + name_write_textbox.Text + ".txt");
                txt.Write(input_textbox.Text);
                txt.Close();
                //input_textbox.Text = "success";
            }
            catch { }

            string path_category = device_name + @"\c-sharp\Files\all_category.txt";
            string path_name = device_name + @"\c-sharp\Files\all_names.txt";

                using (StreamWriter cat = File.AppendText(path_category))
                {
                    cat.WriteLine(category_write_combobox.SelectedItem);
                    input_textbox.Text = "success";
                }
                using (StreamWriter nam = File.AppendText(path_name))
                {
                    nam.WriteLine(name_write_textbox.Text);
                }
            
        }
        public bool a = false;
        private void setting_Click(object sender, EventArgs e)
        {
            if(a == false)
            {
                this.Height = 600;
                a = true;
            }
            else if(a == true)
            {
                this.Height = 490;
                a = false;
            }
        }

        private void checkedListBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach(string s in checkedListBox2.CheckedItems)
            device_name = s;
            try
            {
                string path_category = device_name + @"\c-sharp\Files\all_category.txt";
                string path_name = device_name + @"\c-sharp\Files\all_names.txt";
                
                StreamReader cat = new StreamReader(path_category);
                StreamReader nam = new StreamReader(path_name);
                

                string data_cat = cat.ReadToEnd();
                string data_nam = nam.ReadToEnd();
                cat.Close();
                nam.Close();

                string[] current_cat = data_cat.Split('\n');
                string[] current_nam = data_nam.Split('\n');
                for (int i = 0; i < current_cat.Length; i++)
                {
                    values[0, i] = current_nam[i];
                    values[1, i] = current_cat[i];
                }
            }
            catch { }
        }
    }
}






标签: c#

解决方案


推荐阅读