Click here to Skip to main content
15,923,273 members
Home / Discussions / C#
   

C#

 
Questionhow to enable the USB COM Port..? Pin
Member 133808373-Sep-17 21:35
Member 133808373-Sep-17 21:35 
QuestionRe: how to enable the USB COM Port..? Pin
Jochen Arndt3-Sep-17 21:44
professionalJochen Arndt3-Sep-17 21:44 
AnswerRe: how to enable the USB COM Port..? Pin
OriginalGriff3-Sep-17 23:18
mveOriginalGriff3-Sep-17 23:18 
GeneralRe: how to enable the USB COM Port..? Pin
Jochen Arndt3-Sep-17 23:39
professionalJochen Arndt3-Sep-17 23:39 
AnswerRe: how to enable the USB COM Port..? Pin
Jochen Arndt3-Sep-17 23:49
professionalJochen Arndt3-Sep-17 23:49 
Questionbinding datagridview with entity framework Pin
Member 132649363-Sep-17 7:17
Member 132649363-Sep-17 7:17 
AnswerRe: binding datagridview with entity framework Pin
Mycroft Holmes3-Sep-17 22:49
professionalMycroft Holmes3-Sep-17 22:49 
QuestionUpdate listbox items when button is clicked Pin
Mario Lukačić3-Sep-17 2:19
Mario Lukačić3-Sep-17 2:19 
Hi,

I have a program that gives me all result i search and put them in listbox.

My first problem is that it need 1-2 second when i start to search to find me a items.

Second problem is when i update items, it writes in ini file but gives me error when i try to update a listbox with edited item.

C#
<pre>using System;
using System.Windows.Forms;
using IniParser;
using IniParser.Model;
using System.Collections.Generic;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public string myFilePath;
        public string searchItem;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            searchBtn.Enabled = false;
            writeIniBtn.Enabled = false;
            saveBtn.Enabled = false;
            AcceptButton = this.searchBtn;
        }

        private void openIniBtn_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();

            openFile.InitialDirectory = "c:\\";
            openFile.Filter = "INI files (*.ini)|*.ini";
            openFile.Title = "Select a INI file for edit";

            if (openFile.ShowDialog() == DialogResult.OK)
            {
                myFilePath = openFile.FileName; // full path to a file
                
                // enable buttons
                searchBtn.Enabled = true;
                writeIniBtn.Enabled = true;
                saveBtn.Enabled = true;
                openIniBtn.Enabled = false; // disable open ini button
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // populate listbox with founded items
            List<string> selectedItems = new List<string>();

            string item = listBox1.SelectedItem.ToString();

            if (!selectedItems.Contains(item))
            {
                selectedItems.Add(item);
            }

            // get clicked item value, section / key
            foreach (string i in listBox1.SelectedItems)
            {
                itemIdTxt.Text = i.Split('[', ']')[1];
                nameInput.Text = i.Split('[', ']')[2];
            }

            
        }

        private void searchBtn_Click(object sender, EventArgs e)
        {
            var searchItem = searchInput.Text; // read value from search input
            
            // start ini
            var parser = new FileIniDataParser();
            parser.Parser.Configuration.AllowDuplicateKeys = true;  // allow duplicate keys
            IniData data = parser.ReadFile(myFilePath);             // opens ini and search in it , myFilePath = path to ini file

            listBox1.Items.Clear(); // clear list

            searchItem = searchInput.Text;  // getting data from input to search for
            
            // loop through sections
            foreach (var item in data.Sections)
            {
                // search for string under key 1
                if (item.Keys["1"].IndexOf(searchItem, 0, StringComparison.CurrentCultureIgnoreCase) != -1)
                {
                    // founded items
                    var foundedItems = ($"[{item.SectionName}] {item.Keys["1"]}");
                    
                    // add founded items to list
                    ListBox list = new ListBox();
                    listBox1.Items.Add(foundedItems);
                }
            }
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            var id = itemIdTxt.Text;    // read id
            var name = nameInput.Text;  // read name

            // write to ini file
            var parser = new FileIniDataParser();
            parser.Parser.Configuration.AllowDuplicateKeys = true;
            IniData data = parser.ReadFile(myFilePath);
            data[id]["1"] = name;
            parser.WriteFile(myFilePath, data);

            // update changed item in listbox
            string index = listBox1.SelectedItem.ToString();
            listBox1.Items.Remove(index);
            listBox1.Items.Add(name);
        }

        
    }
}


AnswerRe: Update listbox items when button is clicked Pin
Richard MacCutchan3-Sep-17 21:14
mveRichard MacCutchan3-Sep-17 21:14 
GeneralRe: Update listbox items when button is clicked Pin
Mario Lukačić4-Sep-17 2:31
Mario Lukačić4-Sep-17 2:31 
GeneralRe: Update listbox items when button is clicked Pin
Jochen Arndt4-Sep-17 3:49
professionalJochen Arndt4-Sep-17 3:49 
GeneralRe: Update listbox items when button is clicked Pin
Mario Lukačić4-Sep-17 4:01
Mario Lukačić4-Sep-17 4:01 
GeneralRe: Update listbox items when button is clicked Pin
Jochen Arndt4-Sep-17 4:39
professionalJochen Arndt4-Sep-17 4:39 
GeneralRe: Update listbox items when button is clicked Pin
Mario Lukačić4-Sep-17 5:50
Mario Lukačić4-Sep-17 5:50 
GeneralGetting file path and setting to a public variable ?!?! Pin
Mario Lukačić1-Sep-17 8:42
Mario Lukačić1-Sep-17 8:42 
GeneralRe: Getting file path and setting to a public variable ?!?! Pin
Richard Deeming1-Sep-17 9:30
mveRichard Deeming1-Sep-17 9:30 
GeneralRe: Getting file path and setting to a public variable ?!?! Pin
Mario Lukačić1-Sep-17 9:33
Mario Lukačić1-Sep-17 9:33 
QuestionAPI to https://images.google.com Pin
JanningJuul31-Aug-17 21:34
JanningJuul31-Aug-17 21:34 
AnswerRe: API to https://images.google.com Pin
Richard MacCutchan31-Aug-17 22:06
mveRichard MacCutchan31-Aug-17 22:06 
QuestionIP Address Control in for WPF? Pin
Member 1319657431-Aug-17 20:58
Member 1319657431-Aug-17 20:58 
AnswerRe: IP Address Control in for WPF? Pin
Richard MacCutchan31-Aug-17 21:10
mveRichard MacCutchan31-Aug-17 21:10 
GeneralRe: IP Address Control in for WPF? Pin
Member 1319657431-Aug-17 21:17
Member 1319657431-Aug-17 21:17 
GeneralRe: IP Address Control in for WPF? Pin
Richard MacCutchan31-Aug-17 21:35
mveRichard MacCutchan31-Aug-17 21:35 
AnswerRe: IP Address Control in for WPF? Pin
Bernhard Hiller31-Aug-17 22:24
Bernhard Hiller31-Aug-17 22:24 
SuggestionRe: IP Address Control in for WPF? Pin
Richard Deeming1-Sep-17 2:43
mveRichard Deeming1-Sep-17 2:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.