C# timer - Getting the 'Method name expected' error - c#

When I try to set the tick event of my timer, and use the method, I get this error. What's going wrong here?
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;
using System.Timers;
namespace QueueSimulation
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
}
public void goButton_Click(object sender, EventArgs e)
{
ProcessCustomers CustomerQueue = new ProcessCustomers(); // create the CustomerQueue
System.Windows.Forms.Timer queueTimer = new System.Windows.Forms.Timer();
queueTimer.Interval = Convert.ToInt32(customerArriveChooser.Value*1000);
queueTimer.Tick += new ElapsedEventHandler(CustomerQueue.Arrive());
CustomerQueue.Arrive();
}
private void stopButton_Click(object sender, EventArgs e)
{
// put code here to break out of the program
}
}
public class Customer
{
int timeInQueue;
}
public class ProcessCustomers
{
public void Arrive(){}
public void Leave(){}
}
public class Server
{
bool servingStatus = false; // true for serving, false for not serving
}
public class Queue
{
Customer[] queue = new Customer[49]; // initialise a queue (array) capable of holding 50 customers
}
}

I suspect you mean to use the method name, not call it and use the return value:
queueTimer.Tick += new EventHandler(CustomerQueue.Arrive);
Since the return value of Arrive is not a delegate type, you can't use it.
Note that the event handler signature should match the delegate signature - in the case of Tick, it is EventHandler:
public delegate void EventHandler(
Object sender,
EventArgs e
)
So, your Arrive method should take these two parameters:
public void Arrive(Object sender, EventArgs e){}

ElapsedEventHandler is the handle for System.Timer not for System.Windows.Forms.Timer
the event must look like this:
queueTimer.Tick += new ElapsedEventHandler(queueTimer_Tick);
void queueTimer_Tick(object sender, EventArgs e)
{
CustomerQueue.Arrive();
}

Related

Visual Studio C# while loop freezing forms application [duplicate]

This question already has answers here:
Thread.Sleep() in C#
(4 answers)
Closed 6 years ago.
Here's my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace _8BB_2._0
{
public partial class Form1 : Form
{
public static class globalVars
{
public static bool spacerunning = false;
}
public Form1()
{
InitializeComponent();
globalVars.spacerunning = false;
}
private void button1_Click(object sender, EventArgs e)
{
if (!globalVars.spacerunning)
{
globalVars.spacerunning = true;
while (globalVars.spacerunning)
{
Thread.Sleep(1000);
SendKeys.Send(" ");
}
}
else if (globalVars.spacerunning)
{
globalVars.spacerunning = false;
}
}
}
}
When I click button1 it's starts hitting space every second like it should but when I try to click it again to shut it off the application freezes and it keeps pressing space. I've tried multiple other ways but can't seem to figure out how I can do two things at once since I get locked inside of the while loop.
Calling Thread.Sleep() will block the UI thread. Try to use async/await instead.
private async void button1_Click(object sender, EventArgs e)
{
globalVars.spacerunning = !globalVars.spacerunning;
while (globalVars.spacerunning)
{
await Task.Delay(1000);
SendKeys.Send(" ");
}
}
UPDATE:
You may use a Timer instead.
public class MainForm : Form
{
private Timer timer = new Timer() { Interval = 1000 };
public MainForm()
{
/* other initializations */
timer.Enabled = false;
timer.Tick += timer_Tick;
}
private void timer_Tick(object sender, EventArgs e)
{
SendKeys.Send(" ");
}
private void button1_Click(object sender, EventArgs e)
{
globalVars.spacerunning = !globalVars.spacerunning;
timer.Enabled = globalVars.spacerunning;
}
}

No Extension Method for Client Name in C#

Given the code below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NetworksApi.TCP.CLIENT;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Form1 client;
public Form1()
{
InitializeComponent();
}
private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox3.Text!= "" &&textBox4.Text!="")
{
client = new Form1();
client.ClientName = textBox4.Text;
client.ServerIp = textBox3.Text;
client.Connect();
}
else
{
MessageBox.Show("Fill it completely");
}
}
private void button3_Click(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
System.Environment.Exit(System.Environment.ExitCode);
}
}
}
I get the following error message whenever I try to compile:
'WindowsFormsApplication1.Form1' does not contain a definition for
ClientName and no extension method 'ClientName' accepting a first
argument of type.
Do you have any idea on how to fix this?
There is no ClientName property on a Windows Form class. However, since you are inheriting from Form, you can add one. But that doesn't make sense either. Are you sure you want a variable of type Form1 to have properties for ClientName, ServerIP, and a method for Connect()? Much more likely you want either some other pre-existing class or to make your own.
public class ClientService
{
public string ClientName {get; set;}
public string ServerIp {get; set;}
public void Connect()
{
//logic here
}
}
And change your UI logic to
if (!String.IsNullOrEmpty(textBox3.Text) && !String.IsNullOrEmpty(textBox4.Text))
{
var client = new ClientService();
client.ClientName = textBox4.Text;
client.ServerIp = textBox3.Text;
client.Connect();
}
else
{
MessageBox.Show("Fill it completely");
}
This is the documentation for the Form class in .NET: https://msdn.microsoft.com/en-us/library/system.windows.forms.form(v=vs.110).aspx
Notice there's no member for ClientName listed. You cannot reference it because it doesn't exist.

Using Visual Basic to Make Average Test Score Generator in C#

I'm trying to create a calculator for an assignment that takes three integer inputs and takes the average and returns that to user. I'm using Visual Basic (it is required) to make the GUI. I'm having trouble with two things, first, I cannot get the aVer to divide by 3 because it is not a integer and second, I do not know how to get an output with the average in the last textbox.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private string userInfo1;
private string userInfo2;
private string userInfo3;
private string aVer;
private string num1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int aVer = 0;
aVer = Int32.Parse(userInfo1 + userInfo2 + userInfo3);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
int userInfo1 = 0;
userInfo1 = Int32.Parse(textBox1.Text);
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
int userInfo2 = 0;
userInfo2 = Int32.Parse(textBox2.Text);
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
int userInfo3 = 0;
userInfo3 = Int32.Parse(textBox3.Text);
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
int num = Int32.Parse(aVer);
MessageBox.Show(num.ToString());
}
private void label3_Click(object sender, EventArgs e)
{
}
}
}
You are declaring your variables multiple times. The variables inside the function calls will hide the variables you have defined outside in the class declaration, and the class variables will never be set to a value.
It doesn't appear that you are ever using any of the variables in their string form, so all the private string declarations should be changed to private int declarations. This also makes the int userInfo1 = 0; declarations unnecessary. Having them actually breaks the ability to see the values in the button1_Click function.

Use object between multiple functions

I am creating a spam email checker. One method scans the email, another adds a known flag to an array of words and phrases to check against; both methods are part of Tester class. Currently I have a button per method, however each event creates its own spam object. How do I get both events to use the same object, allowing the scan to recognize the flag I just added?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HW8_DR
{
public partial class Spam_Scanner : Form
{
public Spam_Scanner()
{
InitializeComponent();
}
private void testButton_Click(object sender, EventArgs e)
{
Tester scan = new Tester();
scan.tester(Convert.ToString(emailBox.Text));
this.SpamRatingBox.Text = string.Format("{0:N1}%", Tester.countSpam / Tester.wordCount * 100);
this.WordsBox.Text = Tester.posSpam;
this.OutputPanal.Visible = true;
this.pictureBox1.Visible = false;
}
private void addButton_Click(object sender, EventArgs e)
{
Tester scan = new Tester();
scan.addSpam(Convert.ToString(addFlagBox.Text));
this.addFlagBox.Text = "";
}
}
}
Move the Tester variable to the class field, like this:
public partial class Spam_Scanner : Form
{
Tester scan;
public Spam_Scanner()
{
InitializeComponent();
scan = new Tester();
}
private void testButton_Click(object sender, EventArgs e)
{
scan.tester(Convert.ToString(emailBox.Text));
this.SpamRatingBox.Text = string.Format("{0:N1}%", Tester.countSpam / Tester.wordCount * 100);
this.WordsBox.Text = Tester.posSpam;
this.OutputPanal.Visible = true;
this.pictureBox1.Visible = false;
}
private void addButton_Click(object sender, EventArgs e)
{
scan.addSpam(Convert.ToString(addFlagBox.Text));
this.addFlagBox.Text = "";
}
}
Variables declared inside of a method (as yours are) have method scope, so they can't be seen by other methods.
Instead, declare the variable in the class scope so that both the class's methods can see it.
public partial class Spam_Scanner : Form
{
private Tester scan;
private void testButton_Click(object sender, EventArgs e)
{
scan = new Tester();
...
}
private void addButton_Click(object sender, EventArgs e)
{
scan.addSpam(Convert.ToString(addFlagBox.Text));
...
}
}
Depending on the order of button clicks, you may want to initialize the variable in the declaration rather than in the testButton_Click method, but thats up to you. The important thing to remember is that scopes can see their own members, and all scopes they are nested in. Thus, methods can see class-scope variables, but not each other's.

How can I interrupt playing of text to speech synthesis in my application?

This is the code that I used to speech a richTextBox. My problem is that I can't click on anything when the text is playing. I can't even stop playing. How can I fix the problem? Is there any way to stop playing by clicking on a button?
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;
using System.Speech.Synthesis;
namespace Merger
{
public partial class Form1 : Form
{
SpeechSynthesizer tell = new SpeechSynthesizer();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
tell.Rate = trackBar1.Value;
}
private void Form1_Resize(object sender, EventArgs e)
{
this.Refresh();
}
private void pictureBox2_Click(object sender, EventArgs e)
{
tell.Volume = 100;
tell.Speak(richTextBox1.SelectedText);
}
private void trackBar1_ValueChanged(object sender, EventArgs e)
{
tell.Rate = trackBar1.Value;
}
private void button1_Click(object sender, EventArgs e)
{
tell.SpeakAsyncCancelAll();
}
}
}
The problem is that the Speak() method is Synchronous, so it will lock the thread you're on. Assuming you're on a single thread, that will be the UI thread, thus locking anything you're doing.
You might perhaps be better using a different thread to Speak(), which won't lock your current (UI) thread.
SpeechSynthesizer.Speak Method (String) - MSDN
Or you can use the SpeechAsync method, which will do it asyncronously!
SpeechSynthesizer.SpeakAsync Method (String) - MSDN

Categories

Resources