|
Aww it didnt work.
It paused but the same thing happned
Steve
|
|
|
|
|
Can you post some code?
That would really be helpful in helping you !
(Remember to use "code block" when posting the code bits.)
|
|
|
|
|
OK, here you go, but please bare in mind I am a learner.
Also, It sort of works now, but the text that is output last is added onto the new text, so I am going wrong somewhere.
This part is elisa (name of the AI reponder)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace AI
{
class elisa
{
private string input;
private string output;
private int NumberOfReplies = 0;
public string respond(string input)
{
output = ("");
this.input = input;
if (NumberOfReplies >= 1)
{
output = ("Please wait!");
}
else
output = ("Sorry am a bit buzy at the min. ");
NumberOfReplies += 1;
Thread.Sleep(4000);
return output;
}
}
}
And this is from my form, it has a text box to enter what you want to say, and an edit box that then shows that text, and shows the responce from elisa...
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.Threading;
namespace AI
{
public partial class Form1 : Form
{
elisa _elisa = new elisa();
string userInput;
string elisaResponce;
string you = ("You: ");
string elisa = ("Elisa: ");
public Form1()
{
InitializeComponent();
status.Text = ("");
}
private void btnSend_Click(object sender, EventArgs e)
{
updateInput(textBoxInput.Text);
textBoxInput.Clear();
status.Text = ("Elisa is typing a message");
getResponceFromElisa();
}
private void updateInput(string input)
{
richTextBoxOutput.Text += you += input + ("\n");
}
private void getResponceFromElisa()
{
timer1.Start();
}
private void textBoxInput_TextChanged(object sender, EventArgs e)
{
if (textBoxInput.Text == (""))
{
status.Text = ("");
}
else
status.Text = ("You are typing a message...");
}
private void timer1_Tick(object sender, EventArgs e)
{
elisaResponce = _elisa.respond(userInput);
richTextBoxOutput.Text += elisa += elisaResponce;
timer1.Stop();
status.Text = ("");
}
}
}
Thank you for asking to have a look, I appreciate it
Steve
|
|
|
|
|
I'll have a look at your post when I'm home from work.
(And I'm getting ready to go just about now)
Bye for now
|
|
|
|
|
ManfredRBihy wrote: I'll have a look at your post when I'm home from work.
(And I'm getting ready to go just about now)
Thank you
Steve
|
|
|
|
|
your problem lies in elisa.respond() . Inside it lies the folowing code: Thread.Sleep(4000); . You use that method in a Timer.Tick event. So even if timer ticks, the event doesn't finish for next event tick
|
|
|
|
|
this is a quick hack:
try a whateverControl.Refresh() before you put your main thread to sleep.
A thread or timer approach is better though as it would keep your app responsive.
|
|
|
|
|
OK, thank you.
Can you see anywhere in my code why it is that the strings are being added together?
For example, if I type:
Hello.
Then I type hello again, it would output as
hellohello.
I have tried to clear the strings atraight after use but no luck.
Thank you
Steve
|
|
|
|
|
stephen.darling wrote: I have tried to clear the strings atraight after use but no luck
stephen.darling wrote: richTextBoxOutput.Text += you += input + ("\n")
two very strange statements.
|
|
|
|
|
Luc Pattyn wrote: two very strange statements.
Maybe so, but this is me we are talking about, you proberbly know by bow how difficult it is to explain things to you.
You would think, as a Biomedical Scientist that I would not have a issue, but when it comes to a new area, for me, it is
I am awaiting delivery of my new book, head first c#, so hopefully I can start learning properly.
Regards,
Steve
|
|
|
|
|
Here's an extremely cheesy hack. It's only merit (if you can call it that) is that it's quick to write. For grins, you can show/hide a marquee style progress bar before/after calling GenerateResponse() to simulate processing.
string GenerateResponse
(string input)
{
DateTime start = DateTime.Now;
int pause = 3000;
while ((DateTime.Now - start).TotalMilliseconds < pause) {
Application.DoEvents();
}
return "Some response based on " + input;
}
/ravi
|
|
|
|
|
You're a dirty, dirty bird!
|
|
|
|
|
Hence the disclaimers.
If all he wants to do is write a fun little app, it'll do. But I would never put anything like this in my freeware or commercial apps.
/ravi
|
|
|
|
|
How about this?
string GenerateResponse
(string input)
{
DateTime start = DateTime.Now;
int pause = 3000;
loop:
if ((DateTime.Now - start).TotalMilliseconds >= pause) {
return "Some response based on " + input;
}
Application.DoEvents();
goto loop;
}
/ravi
|
|
|
|
|
Now that's just sad, so, so sad
|
|
|
|
|
Go on - 5 it. You know you wanna.
/ravi
|
|
|
|
|
If by "5 it", you mean "punch it in the face with my fist", then I'm sure he does want to.
|
|
|
|
|
Having trouble seeing the smilies?
/ravi
|
|
|
|
|
I too was joking (making reference to hands having 5 fingers). Didn't feel the need to taint the joke with an emotion though.
|
|
|
|
|
I aasigned Text to the Textbox from code behind like "Showing <b>"+Test+" </b> message ...";
Its not displaying Test in bold.How can I do this
|
|
|
|
|
Since you refer to 'code behind' your question is probably in the wrong forum. It should more properly be in (at a guess) the ASP or WPF forum.
In Windows Forms programming the TextBox cannot display text styles from tags, only from being assigned a boldened font.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
If you use classic Windows Form App then you need to use TextBox.Font[^]
In Wpf, it is FontWeight
|
|
|
|
|
I aasigned Text to the Textbox from code behind like "Showing "+Test+" message ...";
Its not displaying Test in bold.How can I do this
|
|
|
|
|
Please try to avoid posting the same question more than once!
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
I'm taking this exam in a little more then a week from now and am searching for some practice questions. I found some and didn't always understand the question so I tried it out. "It didn't work". Really, the code they gave as the answer did not what the question requested.
I'm curious if the source is trustworthy or if I'm going bananas here (or both ) ??
thanks.
below an example of such a question and expected answer:
(apologies for the formatting)
You use Microsoft .NET Framework 4 to create a Windows Presentation Foundation (WPF) application.
You write the following code fragment.
<StackPanel TextBox.PreviewTextInput="StackPanel_PreviewTextInput">
<TextBox Name="TxtBoxA"/>
<TextBox Name="TxtBoxB"/>
<TextBox Name="TxtBoxC"/>
</StackPanel>
You create an event handler named StackPanel_PreviewTextInput. You also have a collection of strings named Keywords.
You need to ensure that TxtBoxA and TxtBoxB do not contain any of the strings in the Keywords collections.
Which code segment should you use?
A. private void StackPanel_PreviewTextInput(
object sender, TextCompositionEventArgs e)
{ FrameworkElement feSource = sender as FrameworkElement;
if (feSource.Name == "TxtBoxA" || feSource.Name == "TxtBoxB")
{ foreach(string keyword in Keywords)
{
if(e.Text.Contains(keyword)) { e.Handled = false;
return;
}
}} e.Handled = true;
} }
B. private void StackPanel_PreviewTextInput(
object sender, TextCompositionEventArgs e) {
FrameworkElement feSource = e.Source as FrameworkElement;
f (feSource.Name == "TxtBoxA" || feSource.Name == "TxtBoxB")
f (feSource.Name == "TxtBoxA" || feSource.Name == "TxtBoxB") {
foreach(string keyword in Keywords)
{
if(e.Text.Contains(keyword)) { e.Handled = false;
return;
}
} e.Handled = true;
C. private void StackPanel_PreviewTextInput(
object sender, TextCompositionEventArgs e)
{
FrameworkElement feSource = sender as FrameworkElement;
if (feSource.Name == "TxtBoxA" || feSource.Name == "TxtBoxB")
{ foreach(string keyword in Keywords)
{ if(e.Text.Contains(keyword)) {
e.Handled = true;
return; }
} e.Handled = false;
} }
D. private void StackPanel_PreviewTextInput(
object sender, TextCompositionEventArgs e)
{ FrameworkElement feSource = e.Source as FrameworkElement;
if (feSource.Name == "TxtBoxA" || feSource.Name == "TxtBoxB")
{
foreach(string keyword in Keywords)
{ if(e.Text.Contains(keyword)) {
e.Handled = true;
return;
} } e.Handled = false;
}
}
Answer D is the right answer...
However, following the question I found following to actually do what was requested:
(except when you paste the word in the textbox so even the idea of using this code for the requested functionality is not really ideal)
the problem was that e.Text only gave the typed character and not the entire contents.
private void StackPanel_PreviewTextInput(object sender, TextCompositionEventArgs e){
FrameworkElement feSource = e.Source as FrameworkElement;
if (feSource.Name == "TxtBoxA" || feSource.Name == "TxtBoxB") {
foreach (string keyword in Keywords) {
lbl_info1.Content = e.Text;
if ( ((((TextBox)feSource).Text)+e.Text).Contains(keyword)) {
e.Handled = true;
return;
}
}
e.Handled = false;
}
}
V.
|
|
|
|