简体   繁体   中英

Accessors (Properties) without Form.ShowDialog()

I am trying to set a ConstantLine for a DevExpress SplineChart that is created in the Form1 from Form2 and also set a numericalupdown.value placed in the Form2 for a textBox.text that is placed in the Form1 , whilst both Form1 and Form2 are Open and running.

I am using from accessors {get;set;} to get and set values of DevExpressChart as i have written down in my codes.

I can get the values, but i can't set any value without using Form1.ShowDialog() .

I have also used Form1.Update(); and Form1.Refresh(); but the mentioned code only run successfully with the use of Form1.Show(); or Form1.ShowDialog();

However, i want them to execute while both forms are running Form2 as a child of Form1 an seeing the changes in the Form1

Code

//Code Snippet in the Form2:
//NumericalUpDown-ValueChanged Event: In Form2

private void numUpDnShkgTimeRstcConfig_ValueChanged(object sender, EventArgs e)
{
    Form1 frm1 = new Form1();

    if (chkBxShakingTimeRestCteLineConfig.Checked == true)
    {
        XYDiagram diagram = (XYDiagram)Frm1.SplineChart.Diagram;
        diagram.AxisX.ConstantLines.Add(new ConstantLine("Shaking Time", Convert.ToString(numUpDnShkgTimeRstcConfig.Value)));
        Frm1.TxtBx = Convert.ToString(numUpDnShkgTimeRstcConfig.Value);
    } 
}

//Code Snippet in the  Form1
//Pass Objects And Parameter.

public DevExpress.XtraCharts.ChartControl SplineChart
{
    get {return SplineChrt1Form1; }
    set { SplineChrt1MainFrm = value; }
}

public string TxtBx
{
    get { return txtBxSmplWt1Form1.Text; }
    set { txtBxSmplWt1Form1.Text = value; }
}

...

The way I understood your problem was that:

  1. You are displaying a Chart in Form1.
  2. From Form1, you want to show a second form (Form2) that allows you to change or specify values for the Chart in Form1.
  3. You want to get the updated values from Form2 in to Form1.

The best way to setup this sort of notification and updating of a parent form from a child form is to use Events . Events enable a child form to notify its parent without actually knowing anything about the parent.

Step 1 - Create an EventArgs class. This class will be used to hold the information you want to pass from Form2 to Form1.

Generally speaking, I find it is better to have the properties as Read Only and only set them in the constructor for this sort of event.

// I wasn't sure what the parameters were called or their type,
// so I just used an int and string to demonstrate the functionality
public class ChartValuesChangedEventArgs : EventArgs
{
    public ChartValuesChangedEventArgs (int value1, string value2)
    {
        Value1 = value1;
        Value2 = value2;
    }

    public int Value1 { get; private set; }
    public string Value2 { get; private set; }
}

Step 2 - Declare the event that will be raised from Form2. This is what will notify the Parent (Form1) that the values have changed and what the values are.

public event EventHandler<ChartValuesChangedEventArgs> ValuesChanged;

Step 3 - Raise the Event. This is where you notify the Parent that the values have changed. For this example, I am raising the event on a Button Click. You can just as easily put the content of this function in your own numUpDnShkgTimeRstcConfig_ValueChanged function.

private void button1_Click(object sender, EventArgs e)
{
    ChartValuesChangedEventArgs chartValuesChangedEventArgs = new
        ChartValuesChangedEventArgs(numUpDnShkgTimeRstcConfig.Value,
                                    txtBxSmplWt1Form1.Text);
    OnValuesChanged(chartValuesChangedEventArgs);
}

protected virtual void OnValuesChanged(ChartValuesChangedEventArgs e)
{
    EventHandler<ChartValuesChangedEventArgs> handler = ValuesChanged;
    if (handler != null)
    {
        handler(this, e);
    }
}

Step 4 - Handle the event. This is where you update your chart with the new/updated values from Form2

private void ShowForm2Button_Click(object sender, EventArgs e)
{
    Form2 form2 = new Form2();
    form2.ValuesChanged += form2_ValuesChanged;
    form2.Show();
}

void form2_ValuesChanged(object sender, ChartValuesChangedEventArgs e)
{
    // Update the chart values here
    Debug.Print(e.Value1.ToString());
    Debug.Print(e.Value2);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM