patterncsharpMinor
Windows forms set saved groupbox location settings
Viewed 0 times
settingswindowsgroupboxsavedformssetlocation
Problem
I have a Windows form that when a user moves some groupboxes around, before the application exits, it saves those X and Y coordinates into a MySQL database (with a bit of added text to identify which groupbox those settings belong to). This all works fine. When they log back into the application, I can pull those settings too.
Is this the most efficient way to accomplish parse & apply saved settings from a string?
Here's the working function that's grabbing this text string and parses it:
```
private void apply_GUI_settings(string settings)
{
// The string below, GUI_settings, will evaluate to: "[1:{X=7,Y=29}][2:{X=52,Y=433}]"
string GUI_settings = settings;
bool startFound = false;
for (int c = 0; c < GUI_settings.Length; c++)
{
switch (GUI_settings[c])
{
case '[':
{
startFound = true;
}
break;
case '1': // groupBox_AccountInfo.Location settings
case '2': // groupBox_YourCharacters.Location settings
{
string which_groupBox = GUI_settings[c].ToString();
if (startFound)
{
c++; // Iterate over the "1"
if (GUI_settings[c] == ':')
{
int n = 0;
string X = "";
string Y = "";
c += 4; // Iterate over the ":{X="
while (int.TryParse(GUI_settings[c].ToString(), out n))
{
c++;
X += n.ToString();
}
n = 0;
c += 3; // Iterate over the ",Y="
while (int.TryParse(GUI_settings[c].ToString(), out n))
{
Is this the most efficient way to accomplish parse & apply saved settings from a string?
Here's the working function that's grabbing this text string and parses it:
```
private void apply_GUI_settings(string settings)
{
// The string below, GUI_settings, will evaluate to: "[1:{X=7,Y=29}][2:{X=52,Y=433}]"
string GUI_settings = settings;
bool startFound = false;
for (int c = 0; c < GUI_settings.Length; c++)
{
switch (GUI_settings[c])
{
case '[':
{
startFound = true;
}
break;
case '1': // groupBox_AccountInfo.Location settings
case '2': // groupBox_YourCharacters.Location settings
{
string which_groupBox = GUI_settings[c].ToString();
if (startFound)
{
c++; // Iterate over the "1"
if (GUI_settings[c] == ':')
{
int n = 0;
string X = "";
string Y = "";
c += 4; // Iterate over the ":{X="
while (int.TryParse(GUI_settings[c].ToString(), out n))
{
c++;
X += n.ToString();
}
n = 0;
c += 3; // Iterate over the ",Y="
while (int.TryParse(GUI_settings[c].ToString(), out n))
{
Solution
Parsing the settings strings character by character is troublesome and can be hard to get it right.
It would be better to use a regex to match the string and extract the relevant parts using capture groups, for example:
Debuggex Demo
I know too little of C# to give a sample implementation,
in case it helps, here it is in Java:
It would be better to use a regex to match the string and extract the relevant parts using capture groups, for example:
\[(\d+):\{X=(\d+),Y=(\d+)\}\]Debuggex Demo
I know too little of C# to give a sample implementation,
in case it helps, here it is in Java:
Pattern pattern = Pattern.compile("\\[(\\d+):\\{X=(\\d+),Y=(\\d+)\\}\\]");
String sample = "[1:{X=7,Y=29}][2:{X=52,Y=433}]";
Matcher matcher = pattern.matcher(sample);
while (matcher.find()) {
int id = Integer.parseInt(matcher.group(1)),
int x = Integer.parseInt(matcher.group(2)),
int y = Integer.parseInt(matcher.group(3))
// do what you need with id, x, y
}Code Snippets
\[(\d+):\{X=(\d+),Y=(\d+)\}\]Pattern pattern = Pattern.compile("\\[(\\d+):\\{X=(\\d+),Y=(\\d+)\\}\\]");
String sample = "[1:{X=7,Y=29}][2:{X=52,Y=433}]";
Matcher matcher = pattern.matcher(sample);
while (matcher.find()) {
int id = Integer.parseInt(matcher.group(1)),
int x = Integer.parseInt(matcher.group(2)),
int y = Integer.parseInt(matcher.group(3))
// do what you need with id, x, y
}Context
StackExchange Code Review Q#65330, answer score: 5
Revisions (0)
No revisions yet.