Saturday 10 August 2013

Updating a string from a text box in C#

Updating a string from a text box in C#

In the program I'm making, I made a string in Settings, called "Tickers".
The scope is Application, and the value is "AAPL,PEP,GILD" without the
quotes.
I have a RichTextBox, called InputTickers where a user should put in stock
tickers, such as AAPL, SPLS, and more. You get the point. When they click
the button below the InputTickers, I need it to get
Settings.Default["Tickers"]. Next, I need it to check is any of the
tickers they typed in, are already in the Tickers list. If not, I need
them added in.
After adding them in, I need to turn it back into the Tickers string to
store in the Settings again.
I'm still learning coding, so this is my best guess, for how far I have
gotten on this. I can't quite think of how to get this done correctly,
though.
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);
}

No comments:

Post a Comment