First of all, I'd like to point out that the desired result is a char, not its position index (int).
I'm trying to give my users an option of choosing their desired date format and as such, I've created 2 comboBoxes: comboBox_DateFormatDivider where user chooses between dot, dash and a slash; and comboBox_DateFormat where user chooses date format. comboBox_DateFormat contains a List<string> as follows:
_dateFormatsList = new List<string>()
{
"d-M-yy (" + DateTime.Now.ToString("d-M-yy") + ")",
"dd-M-yy (" + DateTime.Now.ToString("dd-M-yy") + ")",
"d-MM-yy (" + DateTime.Now.ToString("d-MM-yy") + ")",
"dd-MM-yy (" + DateTime.Now.ToString("dd-MM-yy") + ")",
"d-M-yyyy (" + DateTime.Now.ToString("d-M-yyyy") + ")",
"dd-M-yyyy (" + DateTime.Now.ToString("dd-M-yyyy") + ")",
"d-MM-yyyy (" + DateTime.Now.ToString("d-MM-yyyy") + ")",
"dd-MM-yyyy (" + DateTime.Now.ToString("dd-MM-yyyy") + ")",
"yy-M-d (" + DateTime.Now.ToString("yy-M-d") + ")",
"yy-M-dd (" + DateTime.Now.ToString("yy-M-dd") + ")",
"yy-MM-d (" + DateTime.Now.ToString("yy-MM-d") + ")",
"yy-MM-dd (" + DateTime.Now.ToString("yy-MM-dd") + ")",
"yyyy-M-d (" + DateTime.Now.ToString("yyyy-M-d") + ")",
"yyyy-M-dd (" + DateTime.Now.ToString("yyyy-M-dd") + ")",
"yyyy-MM-d (" + DateTime.Now.ToString("yyyy-MM-d") + ")",
"yyyy-MM-dd (" + DateTime.Now.ToString("yyyy-MM-dd") + ")"
};
comboBox_DateFormat.DataSource = _dateFormatsList;
When user chooses different divider, it has to be reflected in the other comboBox, as such DateFormat is dependent on DateFormatDivider, so its contents have to be changed at runtime. And here's the code (and the question) for that:
private void comboBox_DateFormatDivider_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < _dateFormatsList.Count; i++)
{
_dateFormatsList[i] = _dateFormatsList[i].Replace(_dateFormatsList[i].???.ToString(), comboBox_DateFormatDivider.SelectedText);
}
}
Chosen date format is later saved to database, so I guess I could also add another field where divider would be stored, but I'd prefer not to. As you can see, the code above contains ??? in it. So, the question is: how do I replace these question marks with code that will find me the divider it contains?
Also, if I save it with `@`, then if someone else wants to use it anywhere else in their part of code, they'd have to do the exact same operation of getting the divider and replacing the `@`, which makes it more complicated than it should be. – user6807975 Jan 13 '17 at 11:28