Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I have a problem with the code in rec.Speechreconized += rec_Speachrecognized.
I have been looking for answers in the internet but it just won't work. I hope someone can help me.
namespace ai
{
public partial class Form1 : Form
{
SpeechSynthesizer s = new SpeechSynthesizer();
Choices list = new Choice {};
public Form1()
{
SpeechRecognitionEngine rec = new SpeechRecognitionEngine();
list.Add(new String[] {"Hello", "how are you"});
Grammar gr = new Grammar(new GrammarBuilder(list));
try
{
rec.RequestRecognizerUpdate();
rec.LoadGrammar(gr);
rec.SpeechRecognized += rec_Speachrecognized();
rec.SetInputToDefaultAudioDevice();
rec.RecognizeAsync(RecognizeMode.Multiple);
}
catch{return;}
s.Speak("Hi, I am Ms M, what can i help you?");
InitializeComponent();
}
public void Say(String h)
{
s.Speak(h);
}
private EventHandler<SpeechRecognizedEventArgs> rec_Speachrecognized(object sender, SpeechRecognizedEventArgs e)
{
string r = e.Result.Text;
if(r == "hello")
{
Say("hi");
}
throw new NotImplementedException();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
you need to change this line
rec.SpeechRecognized += rec_Speachrecognized();
to
rec.SpeechRecognized += rec_Speachrecognized;
basically remove the () at the end since the event will pass the params but this way you are calling the method without params
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
So far this is my code. The problem I am encountering is that the file is not being found.
namespace Assignment_Forms_Aplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Define Variable
string[] words = new string[10];
// Read the text from the text file, and insert it into the array
StreamReader SR = new StreamReader(#"Library.txt");
//
for (int i = 0; i < 10; i++)
{
words[i] = SR.ReadLine();
}
// Close the text file, so other applications/processes can use it
SR.Close();
}
}
}
Hi Gailen use the following method:
File.ReadAllLines(#"location");
If location is correct then this will work
Assign to variable, for example
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Very new to C# and programming in general. I've run into this problem and I don't really know how to solve it. First of all, here's the code :
It says in the "if" parts of the code that random1 does not exist in the current context. Yes, I am aware that random only exists within the Button_click part because it is between brackets. The code is supposed to pick a random number between 0 and 20 without displaying it so that the user has to guess it. If the user is wrong, it shows a hint saying if the number is too high or too low. How can I fix this problem? Thanks
EDIT : It seems that I was too vague, your answers were good though. This is the full code :
public void Button_Click(object sender, RoutedEventArgs e) //random
{
Random chiffrealeatoire = new Random();
int random1 = (chiffrealeatoire.Next(0, 20));
}
private void Button_Click_1(object sender, RoutedEventArgs e) //quit
{
Application.Current.Shutdown();
}
private void Button_Click_2(object sender, RoutedEventArgs e) //veri
{
}
public void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (BoiteChiffre.Text < random1)
{
MessageBox.Show("Too low");
}
if (BoiteChiffre.Text > random1)
{
MessageBox.Show("Too high");
}
else
{
MessageBox.Show("Congratulations");
}
}
The user is supposed to write in the textbox
You've closed off your method and left out the if statement! The random1 variable is defined and declared within your method so it doesn't exist outside of it. Please move the method's closing bracket to include the if statement as well.
Also, your two if statements should really be linked together with an else if. You've declared two separate if statements so only one of them will have the else. Not wrong, just better practice to the following.
Basic structure:
public void Button_Click(object sender, RoutedEventArgs e) {
...
int random1
if(<random1) {
random1
} else if(>random1) {
...
} else {
...
}
} // <- method closing bracket
Edit: Since you've heavily modified the code provided I'll have to update my explanation.
Your issue has to do with variable scope. A variable defined within a method has local scope to that method. It's not accessible and doesn't even exist outside of it. You should be declaring your method OUTSIDE all the methods so that you can have multiple methods using it.
Basic structure:
int random1
public void methodA() {
random1 = whatever
}
public void methodB() {
if(random1) {
...
}
}
Please try the below code snippet. You need to declare the variable inside of the same method.
public void Button_Click(object sender, RoutedEventArgs e) //random
{
Random chiffrealeatoire = new Random();
int random1 = (chiffrealeatoire.Next(0, 20));
if (BoiteChiffre.Text < random1)
{
MessageBox.Show("Too low");
}
if (BoiteChiffre.Text > random1)
{
MessageBox.Show("Too high");
}
else
{
MessageBox.Show("Congratulations");
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am trying to learn programming and I am starting with a book called Software Development Fundamentals. However I am having loads of difficulty understanding certain subjects. Especially because my native language is not English. I am stuck at the subject (events) and (delegates). I feel like this is to difficult for me, I can not even get this code to work!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lesson02
{
class Program
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.Changed += new EventHandler(r_Changed);
r.Length = 10;
}
static void r_changed(object sender, EventArgs e)
{
Rectangle r = (Rectangle)sender;
Console.WriteLine(
"Value Changed: Length = {0}",
r.Length);
}
}
class Rectangle
{
public EventHandler Changed;
private double length;
public double Length
{
get
{
return length;
}
set
{
length = value;
Changed(this, EventArgs.Empty);
}
}
}
}
I get this error:
Error 1 The name 'r_Changed' does not exist in the current context 14 59 Lesson02
C# is case-sensitive language. You have defined function as r_changed and using it as r_Changed
Use
r.Changed += new EventHandler(r_changed);
instead of
r.Changed += new EventHandler(r_Changed);
I'm pretty sure you'd know by now that C# is a case sensitive programming language.
This should work
static void r_Changed(object sender, EventArgs e)
{
Rectangle r = (Rectangle)sender;
Console.WriteLine("Value Changed: Length = {0}", r.Length);
}
Notice how r_Changed is capitals (r_changed is what you originally defined)
I would suggest using this because it is easier to read.
There is a little typo mistake in your code. It should be r_Changed instead of r_changed in the your Event Handler.
i.e write
static void r_Changed(object sender, EventArgs e)
in place of
static void r_changed(object sender, EventArgs e)
(Remember C# is Case-sensitive)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a question about creating calculator in C# Windows Form Application.
I want it to be possible write with form buttons an expression in textbox so for example 2+3+7= and after pressing "=" button program will read all digits and signs and perform calculation... I don't know from where to start and how could do it in such a way. Any help to any reference or smth to look at how to start doing such a expressions?
Main thing is how to read, seperate and after calculate values from textbox.
Thanks.
With the Split method you could solve this rather easy.
Try this:
private void button1_Click(object sender, EventArgs e)
{
string[] parts = textBox1.Text.Split('+');
int intSum = 0;
foreach (string item in parts)
{
intSum = intSum + Convert.ToInt32(item);
}
textBox2.Text = intSum.ToString();
}
If you would like to have a more generic calculation, you should look at this post:
In C# is there an eval function?
Where this code snippet would do the thing:
public static double Evaluate(string expression)
{
System.Data.DataTable table = new System.Data.DataTable();
table.Columns.Add("expression", string.Empty.GetType(), expression);
System.Data.DataRow row = table.NewRow();
table.Rows.Add(row);
return double.Parse((string)row["expression"]);
}
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = Evaluate(textBox1.Text).ToString();
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
To put it simply,
I start running my C# program in the morning, and the program should show the user a message at 5:45 PM. How can I do this in C#?
Edit: I asked this question because I thought using a timer is not the best solution (comparing the current time periodically to the time I need to run the task):
private void timerDoWork_Tick(object sender, EventArgs e)
{
if (DateTime.Now >= _timeToDoWork)
{
MessageBox.Show("Time to go home!");
timerDoWork.Enabled = false;
}
}
I asked this question because I thought using a timer is not the best solution (comparing the current time periodically to the time I need to run the task)
Why? why not timer a best solution? IMO timer is the best solution. but not the way you have implemented. Try the following.
private System.Threading.Timer timer;
private void SetUpTimer(TimeSpan alertTime)
{
DateTime current = DateTime.Now;
TimeSpan timeToGo = alertTime - current.TimeOfDay;
if (timeToGo < TimeSpan.Zero)
{
return;//time already passed
}
this.timer = new System.Threading.Timer(x =>
{
this.ShowMessageToUser();
}, null, timeToGo, Timeout.InfiniteTimeSpan);
}
private void ShowMessageToUser()
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(this.ShowMessageToUser));
}
else
{
MessageBox.Show("Your message");
}
}
Use it like this
SetUpTimer(new TimeSpan(17, 45, 00));
You can use Task Scheduler too.
Also there is a Timer class which can help you
You may easily implement your own alarm class. To start with, you may want to check the Alarm class at the end of the MS article.
You can use a Timer to check each minute if DateTime.Now == (the specific time you want)
This is an example of a code with windows forms
public MainWindow()
{
InitializeComponent();
System.Windows.Threading.DispatcherTimer timer_1 = new System.Windows.Threading.DispatcherTimer();
timer_1.Interval = new TimeSpan(0, 1, 0);
timer_1.Tick += new EventHandler(timer_1_Tick);
Form1 alert = new Form1();
}
List<Alarm> alarms = new List<Alarm>();
public struct Alarm
{
public DateTime alarm_time;
public string message;
}
public void timer_1_Tick(object sender, EventArgs e)
{
foreach (Alarm i in alarms) if (DateTime.Now > i.alarm_time) { Form1.Show(); Form1.label1.Text = i.message; }
}