Updating a row from ΤextΒox

In the program that I create, I created a line in the settings called "Tickers". The scope is the application, and the value is "AAPL, PEP, GILD" without quotes.

I have a RichTextBox called InputTickers where the user must enter stock tickers such as AAPL, SPLS, etc. You understand. When they click the button below InputTickers, I need this to get Settings.Default ["Tickers"]. Then I need to check if any of the tickers that they entered are already in the Tickers list. If not, I need them to add.

After adding them, I need to return it back to the Tickers line in order to save it again in the settings.

I'm still learning coding, so this is my best guess as far as I got this. I can’t figure out how to do it right.

private void ScanSubmit_Click(object sender, EventArgs e) { // Declare and initialize variables List<string> tickerList = new List<string>(); try { // Get the string from the Settings string tickersProperty = Settings.Default["Tickers"].ToString(); // Split the string and load it into a list of strings tickerList.AddRange(tickersProperty.Split(',')); // Loop through the list and do something to each ticker foreach (string ticker in tickerList) { if (ticker !== InputTickers.Text) { tickerList.Add(InputTickers.Text); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } 
+4
source share
2 answers

Try to like it,

  foreach (string ticker in tickerList) { if (InputTickers.Text.Split(',').Contains(ticker)) { tickerList.Add(InputTickers.Text); } } 

If your input line has a space,

  if (InputTickers.Text.Replace(" ","").Split(',').Contains(ticker)) { } 
0
source

You can use LINQ extension methods for collections. Results are much simpler than code. First, split the line from the settings and add items to the collection. Secondly, split (you forgot this) a line from a text box and add these elements. Third, use the extension method to get a separate list.

 // Declare and initialize variables List<string> tickerList = new List<string>(); // Get the string from the Settings string tickersProperty = Settings.Default["Tickers"].ToString(); // Split the string and load it into a list of strings tickerList.AddRange(tickersProperty.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)); tickerList.AddRange(InputTickers.Text.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)); Settings.Default["Tickers"] = String.Join(',', tickerList.Distinct().ToArray()); Settings.Default["Tickers"].Save(); 
0
source

Source: https://habr.com/ru/post/1496292/


All Articles