[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

Opening a windows form from a class library

Hello,

I want to do something very simple here.

I have a class library and in one of the methods I will need to open a new form and wait for user input which basically has just two buttons ( For example YES and NO )

Depending on what the user choses the execution of the program will proceed.

I am doing this right now ..


public void ValidateResultsManually()
{
    Form1 form = new Form1();
    form.Show();
}

But depending on the user selection my program execution will vary.

So how will I make the execution of the program stop until the user inputs anything... and how would I acess what the user has selected in the form ?

I very new to windows forms and any help would  be greatly appreciated.

Thanks,
Ashwin
0
Ashwin_shastry
Asked:
Ashwin_shastry
4 Solutions
 
andrewjbCommented:
.ShowDialog() rather than .Show()
0
 
sachinpatil10dCommented:
Set Form1 buttons  Ok Button DialogResult to Yes and Cancel  Button DialogResult to No


Form1 form = new Form1();
if (form.ShowDialog() ==  System.Windows.Forms.DialogResult.Yes )
{

}
0
 
Ashwin_shastryAuthor Commented:
Thanks for the quick response guys , here is the thing , I will also need to set the text on the form which opens up Like this

Please  validate the results for <Test Scenario Name>  and mark them as passed or failed.

<Test Scenario Name > will keep changing. How do i set this at run time ?

Thanks,
Ashwin
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
Naman GoelCommented:
create a class library, add System.Windows.Forms assembly to it, add a form to it, form code should be like this:

 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ConsoleApplication2
{
    public partial class Form1 : Form
    {
        private Button btnNo;
        private Button btnYes;
        private Label labelMessage;
    
        public Form1()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.btnNo = new System.Windows.Forms.Button();
            this.btnYes = new System.Windows.Forms.Button();
            this.labelMessage = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // btnNo
            // 
            this.btnNo.DialogResult = System.Windows.Forms.DialogResult.No;
            this.btnNo.Location = new System.Drawing.Point(205, 92);
            this.btnNo.Name = "btnNo";
            this.btnNo.Size = new System.Drawing.Size(75, 23);
            this.btnNo.TabIndex = 5;
            this.btnNo.Text = "No";
            this.btnNo.UseVisualStyleBackColor = true;
            // 
            // btnYes
            // 
            this.btnYes.DialogResult = System.Windows.Forms.DialogResult.Yes;
            this.btnYes.Location = new System.Drawing.Point(114, 92);
            this.btnYes.Name = "btnYes";
            this.btnYes.Size = new System.Drawing.Size(75, 23);
            this.btnYes.TabIndex = 4;
            this.btnYes.Text = "Yes";
            this.btnYes.UseVisualStyleBackColor = true;
            // 
            // labelMessage
            // 
            this.labelMessage.AutoSize = true;
            this.labelMessage.Location = new System.Drawing.Point(101, 35);
            this.labelMessage.Name = "labelMessage";
            this.labelMessage.Size = new System.Drawing.Size(132, 13);
            this.labelMessage.TabIndex = 3;
            this.labelMessage.Text = "Do You want to Continue?";
            // 
            // Form1
            // 
            this.ClientSize = new System.Drawing.Size(392, 142);
            this.Controls.Add(this.btnNo);
            this.Controls.Add(this.btnYes);
            this.Controls.Add(this.labelMessage);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "Form1";
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.ResumeLayout(false);
            this.PerformLayout();

        }
    }
}

Open in new window


add that ClassLibrary to main project and add code something like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Form1 form1 = new Form1();
            if (form1.ShowDialog() == System.Windows.Forms.DialogResult.Yes)
            {
                // your code
            }
           
        }
    }
}
0
 
Naman GoelCommented:
so here is the code for you:

 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ConsoleApplication2
{
    public partial class Form1 : Form
    {
        private Button btnNo;
        private Button btnYes;
        private Label labelMessage;

        public string TestScenarioName
        {
            set
            {
                labelMessage.Text = String.Format("Please  validate the results for {0}  and mark them as passed or failed", value);
            }
        }
    
        public Form1()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.btnNo = new System.Windows.Forms.Button();
            this.btnYes = new System.Windows.Forms.Button();
            this.labelMessage = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // btnNo
            // 
            this.btnNo.DialogResult = System.Windows.Forms.DialogResult.No;
            this.btnNo.Location = new System.Drawing.Point(205, 92);
            this.btnNo.Name = "btnNo";
            this.btnNo.Size = new System.Drawing.Size(75, 23);
            this.btnNo.TabIndex = 5;
            this.btnNo.Text = "No";
            this.btnNo.UseVisualStyleBackColor = true;
            // 
            // btnYes
            // 
            this.btnYes.DialogResult = System.Windows.Forms.DialogResult.Yes;
            this.btnYes.Location = new System.Drawing.Point(114, 92);
            this.btnYes.Name = "btnYes";
            this.btnYes.Size = new System.Drawing.Size(75, 23);
            this.btnYes.TabIndex = 4;
            this.btnYes.Text = "Yes";
            this.btnYes.UseVisualStyleBackColor = true;
            // 
            // labelMessage
            // 
            this.labelMessage.AutoSize = true;
            this.labelMessage.Location = new System.Drawing.Point(101, 35);
            this.labelMessage.Name = "labelMessage";
            this.labelMessage.Size = new System.Drawing.Size(132, 13);
            this.labelMessage.TabIndex = 3;
            this.labelMessage.Text = "Do You want to Continue?";
            // 
            // Form1
            // 
            this.AcceptButton = this.btnYes;
            this.CancelButton = this.btnNo;
            this.ClientSize = new System.Drawing.Size(392, 142);
            this.Controls.Add(this.btnNo);
            this.Controls.Add(this.btnYes);
            this.Controls.Add(this.labelMessage);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "Form1";
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.ResumeLayout(false);
            this.PerformLayout();

        }
    }
}

Open in new window


and call it something like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Form1 form1 = new Form1();
            form1.TestScenarioName = "First Scenario";
            if (form1.ShowDialog() == System.Windows.Forms.DialogResult.Yes)
            {
                // your code
            }
           
        }
    }
}
0
 
Ashwin_shastryAuthor Commented:
Thanks Naman,

Now one last thing .. want to repeat what i asked earlier

I will also need to set the text on the form which opens up Like this

Please  validate the results for <Test Scenario Name>  and mark them as passed or failed.

<Test Scenario Name > will keep changing. How do i set make this happen ?

<Test Scenario Name >  will need to be set from the class library ..




Thanks,
Ashwin
0
 
Naman GoelCommented:
set value like this


form1.TestScenarioName = "First Scenario";
0
 
Ashwin_shastryAuthor Commented:
Thanks for quick help guys ...
0
 
Ashwin_shastryAuthor Commented:
Thanks !
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now