将Windows窗体变量解析为主要使用方法

时间:2014-01-21 17:37:08

标签: c# winforms

我正在使用我的可视化工具的设置面板,它可以对图表上显示的内容进行精细控制,例如选择文本颜色,标题文本输入等。

enter image description here

以下是代码片段:

    public HM_SettingsForm()
    {
        InitializeComponent();
    }

    private void label2_Click(object sender, EventArgs e)
    {

    }

    private void HM_SettingsForm_Load(object sender, EventArgs e)
    {
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {

    }

我的问题是,在main函数中,如何使用从标题文本框等表单输出的字符串和数字的输出?

比如说,我在标题框中输入“这是一个图形”,我可以在我的主方法中使用该字符串,无论我需要做什么。例如,将其存储到String列表中。

我将以下内容声明为实例变量。

HM_SettingsForm settings = new HM_SettingsForm();

我希望我可以通过使用这样的命令来解析变量,所以我可以在我的应用程序中使用它。

settings.checkBox1.Enabled = ...

我是否理解正确?我为新手问题道歉。

6 个答案:

答案 0 :(得分:1)

您可以创建一个Settings类,并使用Settings实例以设置形式发送数据,然后将其取回:

设置类

public class Settings
{
    public string Title {get;set;}
    // ..another props
}

设置表单

private Settings _settings;

public HM_SettingsForm(Settings settings)
{
    InitializeComponent();
    _settings = settings;
    titleTextBox.Text = settings.Title;
    // ..another props inialization
}

public void FormClosing(object Sender, FormClosingEventArgs args)
{
    if (!_valid()) {
        args.Cancel = true;
    }
}

public void FormClose(object Sender, EventArgs args)
{
    _settings.Title = titleTextBox.Text;
    // ..another props updating
}

答案 1 :(得分:0)

在您的设置类中根据需要创建公共属性

public class HM_SettingsForm
    { 


        //just do something like this for all your properties   
       public bool CheckBox1State
        { 
          get{return settings.checkBox1.Checked;}

       }

    }

检索结果

HM_SettingsForm settings = new HM_SettingsForm(); 
bool state= settings.CheckBox1State; 

答案 2 :(得分:0)

查看内置的Application Settings。它允许您将设置存储到文件中,并在应用重新加载时重新加载它们。加载应用程序时,请检查设置并根据设置设置字体颜色,标题等。

Settings.Default["FontColor"] = "#000FFF"; //Some color
Settings.Default["BackgroundColor"] = "#FFF000"; //Some color

答案 3 :(得分:0)

有两种方法:

1 - 您可以创建一个对象,该对象将代表弹出窗体中的数据。

(伪代码)

class MySettings
{
    bgColor
    textColor
    title
    xAxis
    yAxis
    showTitle
    showText
}

现在,添加Ok和Cancel按钮。单击按钮确定后,您将传递MySettings对象并读取它以设置您尝试修改的控件的属性。

2 - 在这里,您将创建弹出窗体的实例,并在弹出窗口中将控件的事件连接到您尝试修改的控件。

例如,您可以创建一个回调函数并将其传递给弹出窗口

public delegate void ComboItemCallBackDelegate(string text);

private ComboItemCallBackDelegate _comboCallBack;

public void SetComboCallback(ComboItemCallBackDelegate cb)
{
    _comboCallBack = cb;
}

private void Combo1_SelectedIndexChanged(sender, e)
{
    _comboCallBack(Combo1.Text);
} 

这样,您将在弹出窗口中收到对主控件的更改。

希望这有帮助

答案 4 :(得分:0)

您可以按照以下方式执行此操作:

在您的设置表单中

声明一个有争议的构造函数:

  public HM_SettingsForm(String titleText)
    {
        InitializeComponent();
string titleText=this.titleField.text;
test t=new test();
t.main(titleText) // passing titleText to main function of another class or in another form.
}

在main函数中调用下面的构造函数:

public class test{ // if main function is in another class or form
Public void main(string titleText)// say main function
{
// do something with the titleText
} 

}

希望这会有所帮助。感谢

答案 5 :(得分:0)

我可能会因为这样说而被投票,但我个人会远离Application Settings。我发现它们比使用XmlSerializer的简单类更烦人。也许他们早在.NET的早期就很好,但我觉得它们现在比现在更有用了。

也就是说,你应该创建一个表示Settings窗口将包含的所有可能参数的类,即使它们是只读的。所以,让我们看看你到目前为止所做的事情:

sealed class GraphSettings
{
  public Color BackgroundColor { get; set; }
  public Color ForegroundColor { get; set; }

  public string Title { get; set; }

  public double OffsetX { get; set; }
  public double OffsetY { get; set; }

  public bool ShowTitle { get; set; }
  public bool ShowText { get; set; }

  public GraphSettings()
  {
    // Set your default values here.
    this.ShowTitle = true;
  }
}

您的Settings窗口应包含控件操作的GraphSettings对象。操纵控件应反映GraphSettings属性值的变化。

然后,您可以使用XmlSerializer将该对象序列化/反序列化(保存/加载)为XML:

static void SaveSettings(GraphSettings settings, string file)
{
  var serializer = new XmlSerializer(typeof(GraphSettings));

  using (var stream = File.Create(file))
  {
    serializer.Serialize(stream, settings);

    stream.Flush();
    stream.Close();
  }
}

static GraphSettings LoadSettings(string file)
{
  var serializer = new XmlSerializer(typeof(GraphSettings));

  using (var stream = File.Open(file, FileMode.Open, FileAccess.Write, FileShare.Read))
  {
    var settings = serializer.Deserialize(stream) as GraphSettings;
    stream.Close();

    return settings;
  }
}

一般流程如下:

  1. 加载设置文件,如果未找到/已损坏,则使用默认值。
  2. 创建设置窗口并设置GraphSettings
  3. 使用GraphSettings
  4. 中的值填充控件
  5. 使用用户界面中所做的任何更改来更新GraphSettings
  6. 关闭设置窗口。
  7. 从设置窗口保存更新的GraphSettings
  8. 将新的GraphSettings传递给您的main功能/表单。