Visual Studio Question: if i have a form and i click a button that runs an algor
ID: 3906594 • Letter: V
Question
Visual Studio Question: if i have a form and i click a button that runs an algorithm in a new window. (see italized for 3 part question)
How can i run an algorithm when the button is clicked?
how can i save the results back to the main form where the algorithm was run?
How would i be able to call the ProgramTest.cs & Program.cs with the click function while reading in the text from the main form?
With visual studio i am unable to locate the feature to read in text unless im supposed to read into a txt.file.
This is the function that is called when the button is clicked:
private void button6_Click(object sender, EventArgs e)
{
}
Here is the algorithm code that needs to be run ( 2 parts below).
ProgramTest.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bruteforce
{
public class ProgramTest
{
public static void Main( string[] args)
{
Program myProgram = new Program("Thank you for running the " +
"Brute Force Algorithm");
myProgram.InputPattern(); //read in the inputted pattern from user
myProgram.RandomGenerator(); //run the random generator program
//i am trying to run this function so that it can run the string and pattern
myProgram.Search();
myProgram.DisplayTime();
Console.Read();
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bruteforce
{
public class Program
{
string s;
string pattern;
public Program(string name)
{
}
public void InputPattern()
{
Console.WriteLine("Enter the pattern you want to search: ");
pattern = (Console.ReadLine()).ToUpper(); //read user input
}
public void RandomGenerator()
{
Random geneSequence = new Random(); // The random number sequence
for (int i = 0; i < 100; i++)
{
int x = geneSequence.Next(1,4);
switch (x)
{
case 1:
s = s + 'C';
break;
case 2:
s = s + 'T';
break;
case 3:
s = s + 'A';
break;
case 4:
s = s + 'G';
break;
}
}
Console.WriteLine(s); //display results of string
}
public void Search() // s is string sequence, pattern is what is inputted from user
{
//brute force algorithm is executed here
int n;
n = s.Length;
int m;
m = pattern.Length;
int i;
int j;
for (i = 0; i <= n - m; i++)
{
for (j = 0; j < m; j++)
{
if (pattern[j] != s[i + j])
break;
}
if (j == m)
{
Console.WriteLine("Pattern found at:{0}", i);
Console.WriteLine(pattern);
}
}
}
public void DisplayTime()
{
var watch = System.Diagnostics.Stopwatch.StartNew();
// the code that you want to measure comes here
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine("Time processed: {0}ms", watch.ElapsedMilliseconds);
}
}
}
Explanation / Answer
As a c# developer I think I can answer your query regarding the windows forms
You can run the algoriyhm by:
namespace ConsoleApplication17
{
public class Program
{
string s;
string pattern;
public Program(string name)
{
}
public void InputPattern()
{
Console.WriteLine("Enter the pattern you want to search: ");
pattern = (Console.ReadLine()).ToUpper(); //read user input
}
public void RandomGenerator()
{
Random geneSequence = new Random(); // The random number sequence
for (int i = 0; i < 100; i++)
{
int x = geneSequence.Next(1,4);
switch (x)
{
case 1:
s = s + 'C';
break;
case 2:
s = s + 'T';
break;
case 3:
s = s + 'A';
break;
case 4:
s = s + 'G';
break;
}
}
Console.WriteLine(s); //display results of string
}
public void Search() // s is string sequence, pattern is what is inputted from user
{
//brute force algorithm is executed here
int n;
n = s.Length;
int m;
m = pattern.Length;
int i;
int j;
for (i = 0; i <= n - m; i++)
{
for (j = 0; j < m; j++)
{
if (pattern[j] != s[i + j])
break;
}
if (j == m)
{
Console.WriteLine("Pattern found at:{0}", i);
Console.WriteLine(pattern);
}
}
}
public void DisplayTime()
{
var watch = System.Diagnostics.Stopwatch.StartNew();
// the code that you want to measure comes here
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine("Time processed: {0}ms", watch.ElapsedMilliseconds);
}
private void button6_Click(object sender, EventArgs e)
{
Program myProgram = new Program("Thank you for running the " +
"Brute Force Algorithm");
myProgram.InputPattern(); //read in the inputted pattern from user
myProgram.RandomGenerator(); //run the random generator program
//i am trying to run this function so that it can run the string and pattern
myProgram.Search();
myProgram.DisplayTime();
Console.Read();
}
1) Here in the above case you have to include the programtest class inside the button6 click method then it will call the program class respectively.
2) You can call the main form using its constructor name with results in the paarmeter.
3) Both classes can be called by using its concern constructor name with parameters overloading with the text in the parameter of constructors.
4) Read in text is possible by creating a text box in the form and get its value by using textboxname.Text property in the form of string.
If the above solution is helpful to you in any way please rate it Or if you have any concerns please comment it, I will help you through
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.