BabaG

i have an app created and just about doing all the things i need it to do
thanks to all the folks here; i'm anything but a programmer. there's one
thing i haven't seen anywhere, though.

my app is a simple set of checkboxes and radio buttons (for the most part,
though there are other things). they set variables, mostly true/false which
are saved to a text file for use in running a powershell script.

my app now saves properly but i'd like to be able to open the text file and
have my various buttons and boxes reflect the state defined in the text file.

how do i do that i assume i'd use streamreader.readline to get the lines
from the text file (there is one variable per line), but how do i make the
buttons highlight according to the variable i've read

thanks,
BabaG


Re: Visual C# Express Edition pass text file variable to set radio button state?

Brendan Grant

Could you post a portion of the data file Parsing files tends to be pretty easy... only it requires knowledge of the layout of the file.




Re: Visual C# Express Edition pass text file variable to set radio button state?

Livery

set the

Control.Checked = true;

to have it "highlighted".





Re: Visual C# Express Edition pass text file variable to set radio button state?

Brendan Grant

Ahh... try the following code that demonstrates the basis of doing something similar:

Code Snippet

string file = "checkBox1.Checked = true;\r\ncheckBox2.Text = \"New Text\";\r\ncheckBox3.Checked = true;";

//Split into an array of manageable lines

string[] lines = file.Split('\n');

Type parentType = this.GetType();

foreach(string l in lines)

{

//Remove any excess whitespace (extra \r's to be specific)

string line = l.Trim();

//Find position of dot so as to learn control name

int x = line.IndexOf(".");

int y = line.IndexOf("=");

string controlName = line.Substring(0, x);

string propertyName = line.Substring(x+1, (y-x)-1).Trim();

string valueString = line.Substring(y+1).Trim();

//Trim off trailing ; if present

if (valueString.EndsWith(";"))

{

valueString = valueString.Substring(0, valueString.Length - 1);

}

//Reference type of control within parent object

FieldInfo fi = parentType.GetField(controlName, BindingFlags.NonPublic | BindingFlags.Instance);

//Get reference to actual control

object theControl = fi.GetValue(this);

//Now begin work with the actual control whose property we will set

Type controlType = theControl.GetType();

//learn about specific property

PropertyInfo pi = controlType.GetProperty(propertyName);

object value = null;

//Determine what type this property uses and do any cleaning/preping of the value before setting it

if( pi.PropertyType == typeof(bool))

{

value = bool.Parse(valueString);

}

else if( pi.PropertyType == typeof(int))

{

value = int.Parse(valueString);

}

else if( pi.PropertyType == typeof(short))

{

value = short.Parse(valueString);

}

else if( pi.PropertyType == typeof(long))

{

value = long.Parse(valueString);

}

else if( pi.PropertyType == typeof(string))

{

//Trim off any extra leading or trailing double quote marks

valueString = valueString.TrimStart('\"');

valueString = valueString.TrimEnd('\"');

value = valueString;

}

//Set the actual value on the control in question

pi.SetValue(theControl, value, null);

}

Note thought that this likely doesn't do everything you want... but will get you started.

Rather than doing it in such a manual manor... you might want to consider using .NET's Application Settings... it'd require you storing your files in a different format, but would make saving and loading far easier.






Re: Visual C# Express Edition pass text file variable to set radio button state?

BabaG

thanks brendan. i'll go over what you've posted. here's an example of some of the
generated text file (called ScriptVariables.txt):

------------ Film Settings --------------------
35mm
ONeg

------------ Camera Settings ------------------
Optical
Canon_5D
FullFrame
cr2
FullFrame

------------ Capture Frame Size Settings --------------
False
False
False
False
False
False
False
False
True
False
False