Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
752 views
in Technique[技术] by (71.8m points)

c# - Pass variable between forms when clicking button

I have two Forms. One with where all the main code is being executed. And the other form is displayed when clicking a menu item by using this method:

Form2 videoSettings = new Form2();       

private void videoToolStripMenuItem_Click(object sender, EventArgs e)
{
    videoSettings.Show();
}

The form which is then opened containsfields where the user gets to set some settings for the application. enter image description here Then when clicking the "save" button I want this variable: public int deviceIndex; to be fetched from the original Form.

So I'm wondering if I can add any event or something in Form1 which detects when the save button is clicked in videoSettings (Form2)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would do it a different way. I'd separate the code between the UI handling and the business logic layers. So your scenario would run in such a way:

  1. The first form issues an event notifying that the button with certain semantics has been activated. The data needed for the processing is included into the event's data.
  2. The business logic listens to this event, and decides to issue a command on the second form. It calls an appropriate method on the form's class, passing the needed information as a parameter (and maybe preprocessing the parameter if needed).
  3. The second form receives the command from the business logic and updates the view.

This way the problem doesn't arise at all.


Example: (I'm not the winforms expert, beware it can be totally wrong from the POV of best winforms practices.)

Part 1 (first form):

class ProcessingActivatedEventArgs : EventArgs
{
    public ProcessingActivatedEventArgs(int data) { MoreData = data; }
    public int MoreData { get; protected set; }
}

class Form1 : Form
{
    private int currentData;
    public event EventHandler<ProcessingActivatedEventArgs> ProcessingActivated;
    void OnButtonClick(object sender, EventArgs args)
    {
        // ...
        if (ProcessingActivated != null)
            ProcessingActivated(new ProcessingActivatedEventArgs(currentData));
    }
}

Part 2: (business logic)

class Controller
{
    Form1 f1;
    Form2 f2;

    void StartFirstForm()
    {
        f1 = new Form1();
        f1.ProcessingActivated += OnProcessingActivated;
        f1.Show();
    }

    void OnProcessingActivated(object sender, ProcessingActivatedEventArgs args)
    {
        int data = args.MoreData;
        f1.DisableProcessingRequests();
        model.ProcessingFinished += OnProcessingFinished;
        model.StartProcessing(data);
        if (data > 0)
            f2.DisplayDataProcessing(0, data);
        else if (data < 0)
            f2.DisplayDataProcessing(data, 0);
        else
            throw new SomeCoolException("impossible data");
    }
}

Part 3: (second form)

class Form2 : Form
{
    public void DisplayDataProcessing(int lower, int upper)
    {
        // ... update the UI
    }
}

Note that this implementation ties the Controller and forms tighter than it could be done. In WPF, the decoupling is achieved by using the appropriate DataContext (but I don't know how to do it properly in WinForms).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...