MS Access和VB具有表单和控件的Tag属性,您可以在运行时存储并使用该应用程序保留。在.NET中有相同的东西吗?
答案 0 :(得分:3)
.NET中的大多数UI控件都有一个.Tag
属性,可以用于相同的目的。
但是,如果您需要其他功能,可以从基本控件创建一个继承的类(即您可以创建一个名为 SpecialPictureBox 的类继承自 PictureBox 并添加其他字段),并在Windows窗体中使用它,就像使用PictureBox一样。
答案 1 :(得分:1)
Windows窗体中有一个Tag属性,但它不会保留。
我使用的模式是创建一个名为Preferences的类(具有我想要保留的每条信息的属性),然后使用这样的PreferencesManager类进行管理:
public class PreferencesManager
{
static private string dataPath = null;
static public string DataPath
{
get
{
if (dataPath == null)
{
string baseFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
dataPath = Path.Combine(baseFolder, @"MY_APP_NAME");
if (!Directory.Exists(dataPath))
{
Directory.CreateDirectory(dataPath);
}
}
return dataPath;
}
}
/// <summary>
/// Saves the specified preferences.
/// </summary>
/// <param name="pref">The preferences.</param>
static public void Save(Preferences pref)
{
// Create file to save the data to
string fn = "Preferences.xml";
string path = Path.Combine(DataPath, fn);
using (FileStream fs = new FileStream(path, FileMode.Create))
{
// Create an XmlSerializer object to perform the serialization
XmlSerializer xs = new XmlSerializer(typeof(Preferences));
// Use the XmlSerializer object to serialize the data to the file
xs.Serialize(fs, pref);
}
}
static public Preferences Load()
{
Preferences ret = null;
string path = string.Empty;
try
{
// Open file to read the data from
string fn = "Preferences.xml";
path = Path.Combine(DataPath, fn);
using (FileStream fs = new FileStream(path, FileMode.Open))
{
// Create an XmlSerializer object to perform the deserialization
XmlSerializer xs = new XmlSerializer(typeof(Preferences));
// Use the XmlSerializer object to deserialize the data from the file
ret = (Preferences)xs.Deserialize(fs);
}
}
catch (System.IO.DirectoryNotFoundException)
{
throw new Exception("Could not find the data directory '" + DataPath + "'");
}
catch (InvalidOperationException)
{
return new Preferences();
}
catch (System.IO.FileNotFoundException)
{
return new Preferences();
}
return ret;
}
}
答案 2 :(得分:0)