i am trying to use a WP speech library i got from codeplex, now the code is fine with no errors, but there are no voice output!!
using wpSpeech;
namespace TalkForMe
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
SpeechTTS spk;
spk = new SpeechTTS ( textBox1.Text);
spk.SpeakLanguage = "en";
spk.SpeakText(textBox1.Text);
}
}
}
Your code has a very small mistake. You created a reference for SpeakTTS but didn't allocate any memory for it. try like this and try.
SpeechTTS spk=new SpeakTTS();
Related
Is there another way to access MainWindow's public variables than :
MainWindow mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
mainWindow.variable....
this work fine, but I'm creating a WPF application and integrating a USB Webcam to my project and using this code above to access MainWindow's variables. This causes some problems like program is still running when I close MainWindow and camera won't stop.
Any suggestions?
This is an over simplified example for what i wrote in comments(you really should look at mvvm the example below is not mvvm).
public class SelectedIndexData
{
public int SelectedIndex { get; set; }
}
public partial class MainWindow : Window
{
private readonly SelectedIndexData _selectedIndexData = new SelectedIndexData();
public MainWindow()
{
InitializeComponent();
}
private void ComboBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
_selectedIndexData.SelectedIndex = ComboBox.SelectedIndex;
}
private void ShowChildWindow(object sender, RoutedEventArgs e)
{
new ChildWindow(_selectedIndexData).Show();
}
}
public partial class ChildWindow : Window
{
private SelectedIndexData _selectedIndexData;
public ChildWindow(SelectedIndexData selectedIndexData)
{
InitializeComponent();
_selectedIndexData = selectedIndexData; // do whatever you want with your data here
}
}
This approach work also if you are developing a dll (you can't tell the same about Application.Current.MainWindow, which is clearer then your attempt anyways)
DependencyObject ucParent = this.Parent;
while (!(ucParent is MainWindow))
{
ucParent = LogicalTreeHelper.GetParent(ucParent);
}
mainview = ucParent as MainWindow;
You can pack this inside a method, save the variables that you need and you shouldn't have problems
To make sure you application shuts down when MainWindow is closed:
public MainWindow()
{
InitializeComponent();
this.Closing += (sender, args) => Application.Current.Shutdown();
}
I am trying to write a small application to read BarCode using Motorola MC5040 Symbol device. Clicking on a button on form should read BarCode.
I am having hard time finding any sample projects. I reference Symbol and Symbol.Barcode DLLs
Here is the code that is not working. Not sure how to control the side buttons on device either.
public partial class Form1 : Form
{
public static Symbol.Barcode.Reader SymbolReader = new Reader();
public static Symbol.Barcode.ReaderData SymbolReaderData = new ReaderData(ReaderDataTypes.Text, 100);
public static System.EventHandler SymbolEventHandler = null;
public Form1()
{
InitializeComponent();
InitScanner();
}
public void InitScanner()
{
SymbolEventHandler = new EventHandler(this.SymbolReader_ReadNotify);
SymbolReader.Actions.Enable();
}
public void SymbolReader_ReadNotify(object sender, EventArgs e)
{
SymbolReader.Actions.Enable();
Symbol.Barcode.ReaderData TheReaderData = SymbolReader.GetNextReaderData();
if (TheReaderData.Result == Symbol.Results.SUCCESS )
{
txtBarcode.Text = TheReaderData.Text.ToString();
SymbolReader_CycleScannerReader();
return;
}
SymbolReader_CycleScannerReader();
}
public void SymbolReader_CycleScannerReader()
{
SymbolReader.Actions.Read(SymbolReaderData);
}
private void button1_Click(object sender, EventArgs e)
{
SymbolReader_ReadNotify(sender, e);
}
}
}
Any pointers or correction will be great.
Here is a sample application using the Symbol.Barcode2 library
https://github.com/bigfont/2013-128CG-Vendord/blob/master/HelpfulStuff/CS_Barcode2Sample1/API.cs
if you initialize a Barcode2 object you can then use that object to capture scan data
var myBarcode2Obj = new Barcode2();
myBarcode2Obj.OnScan += //Your scan even here;
There are only (3) things I need to know in here.
Firstly, there is only (1) button (close_button) in this form. Here's my frm_main.cs code:
public partial class frm_main : Form
{
public_class pc = new public_class();
public frm_main()
{
InitializeComponent();
this.Load += new System.EventHandler(frm_main_Load);
}
private void close_button_Click(object sender, EventArgs e)
{
this.Close();
}
private void frm_main_Load(object sender, EventArgs e)
{
pc.screen_adjust(close_button);
}
}
And here's my public_class.cs code:
public partial class public_class
{
public int cl_b;
public void screen_adjust(Button b)
{
cl_b = frm_main.ActiveForm.Width;
frm_main.ActiveForm.Width = Screen.PrimaryScreen.Bounds.Width;
frm_main.ActiveForm.Height = Screen.PrimaryScreen.Bounds.Height;
b.Left += frm_main.ActiveForm.Width - cl_b;
}
}
The aim of this borderless program is to auto-stretch the form to the whole screen. Now, what I'd like to learn is:
Did I do a correct "VB.net module" in C# properly?
How do I call the methods in public_class.cs without using the 'public_class pc = new public_class();' and 'pc.screen_adjust(close_button);'?
In the public_class.cs, for example, if I want to change the close_button's text, how should I do it? I can't do frm_main.close_button afterall...
Thanks!
I am creating a winforms application which uses Gmaps.net. I am unable to alter the order in which on Load methods are being called. For some reason the map_load is being called before the man_Load. Is there any way to change the order of this ?
If I can provide any more information to help just ask.
Thanks!
Dan.
public partial class main : Form
{
public main()
{
InitializeComponent();
}
private void main_Load(object sender, EventArgs e)
{
MessageBox.Show("main_load");
}
private void map_Load(object sender, EventArgs e)
{
MessageBox.Show("map_load");
}
}
It seems that you used the WinForms designer to create the map. The code behind is in the InitializeComponent() method and seems that the map is being loaded before the MainForm is loaded.
My recommendation is to create the map, once the MainForm has been loaded:
public partial class main : Form
{
public main()
{
InitializeComponent();
}
private void main_Load(object sender, EventArgs e)
{
Control map = CreateMap();
map.Docking = DockStyle.Fill;
this.Controls.Add(map);
}
private Control CreateMap()
{
// Create a new GMaps.NET object, intialize it and return
}
}
Hope it helps.
Total n00b to C# and events although I have been programming for a while.
I have a class containing a text box. This class creates an instance of a communication manager class that is receiving frames from the Serial Port. I have this all working fine.
Every time a frame is received and its data extracted, I want a method to run in my class with the text box in order to append this frame data to the text box.
So, without posting all of my code I have my form class...
public partial class Form1 : Form
{
CommManager comm;
public Form1()
{
InitializeComponent();
comm = new CommManager();
}
private void updateTextBox()
{
//get new values and update textbox
}
.
.
.
and I have my CommManager class
class CommManager
{
//here we manage the comms, recieve the data and parse the frame
}
SO... essentially, when I parse that frame, I need the updateTextBox method from the form class to run. I'm guessing this is possible with events but I can't seem to get it to work.
I tried adding an event handler in the form class after creating the instance of CommManager as below...
comm = new CommManager();
comm.framePopulated += new EventHandler(updateTextBox);
...but I must be doing this wrong as the compiler doesn't like it...
Any ideas?!
Your code should look something like:
public class CommManager()
{
delegate void FramePopulatedHandler(object sender, EventArgs e);
public event FramePopulatedHandler FramePopulated;
public void MethodThatPopulatesTheFrame()
{
FramePopulated();
}
// The rest of your code here.
}
public partial class Form1 : Form
{
CommManager comm;
public Form1()
{
InitializeComponent();
comm = new CommManager();
comm.FramePopulated += comm_FramePopulatedHander;
}
private void updateTextBox()
{
//get new values and update textbox
}
private void comm_FramePopulatedHandler(object sender, EventArgs e)
{
updateTextBox();
}
}
And here's a link to the .NET Event Naming Guidelines mentioned in the comments:
MSDN - Event Naming Guidelines
Here you have "The Simplest C# Events Example Imaginable".
public partial class Form1: Form
{
CommManager comm;
public Form1()
{
InitializeComponent();
comm = new CommManager();
comm.OnFramePopulated += new EventHandler(updateTextBox);
}
private void updateTextBox(object sender, EventArgs ea)
{
//update Textbox
}
}
class CommManager
{
public EventHandler OnFramePopulated;
public void PopulateFrame()
{
OnFramePopulated(this, null);
}
}
Yes - change the signature of updateTextBox to :
private void updateTextBox(object sender, Eventargs ea)
Although that might not be the best design. Things would look a lot neater if you wrote a proper event handler, and then called updateTextBox from there...