How to play media element synchronously? - c#

I have a datatemplate in which i have a text block and speech synthesizer. Whwn i bind it with data the template spawns atleast 3 children. A speech synthesizer is activated on click of one checkbox. It works fine in normal conditions. But if i test it vigorously and try to play more than one synthesizer before initialization, it plays unexpected audio. And it continue even after exiting from that page.
I am sharing code for check box click event. Please suggest a solution.
private async void checkboxPlay_Click(object sender, RoutedEventArgs e)
{
// when _mediaCounter == 0, synthesizer is stopped or not played
// when _mediaCounter == 1, it is playing
// when _mediaVounter == 2, it is paused
Grid gd = (Grid)((sender as CheckBox).Parent as Grid).Parent;
var child = VisualTreeHelper.GetParent(gd);
try
{
if (sender is CheckBox)
{
if (_listElement.Count > 0)
{
if (sender != _listElement[_checkCounter].CheckBox && _listElement[_checkCounter].CheckBox != null)
{
_listElement[_checkCounter].MediaElement.Stop();
_mediaCounter = 0;
_timer.Stop();
_listElement[_checkCounter].Slider.Value = 0;
_description = string.Empty;
_listElement[_checkCounter].CheckBox.IsChecked = false;
}
}
CheckBox cb = sender as CheckBox;
Grid x = (Grid)VisualTreeHelper.GetParent(cb);
_mediaIndex = Convert.ToInt32(
x.DataContext.ToString().Substring(x.DataContext.ToString().Length - 1, 1)
) - 1;
_checkCounter = _mediaIndex;
if (_description != cb.DataContext.ToString())
{
cb.IsChecked = true;
_description = cb.DataContext.ToString();
_mediaCounter = 0;
_InitializeCheckbox(cb);
_InitializeMedia();
}
}
if (_mediaCounter == 0)
{
_mediaCounter = 1;
string desc = string.Empty;
SpeechSynthesizer synth = new SpeechSynthesizer();
SpeechSynthesisStream stream = await synth.SynthesizeTextToStreamAsync(_description.ToString());
_listElement[_checkCounter].MediaElement.SetSource(stream, stream.ContentType);
_listElement[_checkCounter].MediaElement.Play();
}
else if (_mediaCounter == 1)
{
_listElement[_checkCounter].MediaElement.Pause();
_timer.Stop();
_mediaCounter = 2;
}
else
{
_listElement[_checkCounter].MediaElement.Play();
_timer.Start();
_mediaCounter = 1;
}
}
catch
{
}
}

Have you tried using the MediaElement.CurrentState property?
MediaElement.CurrentState
here's a link
http://msdn.microsoft.com/En-US/Library/Windows/Apps/windows.ui.xaml.controls.mediaelement.currentstate

Related

Text in richtextbox applying text decorations outside of selection

I have a WPF desktop application that uses MVVM architecture. I started making it following Tim Buchalka's "Windows Presentation Foundation Masterclass" on Udemy (paid course, only mentioning it in case anyone already knows what it's about), specifically the part with making a simple Evernote Clone.
I have a RichTextBox and several buttons that allow the user to change the formatting of the currently selected text - bold, italics, underline and strikethrough. Bold and italics work perfectly fine, the issue is with underline and strikethrough. I can apply either of the properties fine as long as it's the same in the entire selection (all underlined, all strikethrough, both or neither). But when I try to apply the styling to a selection, where one part is udnerlined/strikethrough and the other is not, the formatting gets applied to a text range that is offset compared to the selection itself (not to mention that the styling isn't applied the way I would have like, but the offset is currently the bigger problem). I'll try adding pictures to better illustrate what I mean.
Styling working correctly when the selection is homogenous:
Selected non-homogenous part of text, about to click the underline button:
How the styling gets applied after I clicked the button:
I tried to figure out what's going on using breakpoints, but the method only seems to iterate through the selection, as intended. I'll appreciate any advice and/or constructive criticism. Anyways, here's my code:
Bold button's method:
private void btnUnderline_Click(object sender, RoutedEventArgs e)
{
bool isButtonChecked = (sender as ToggleButton).IsChecked ?? false;
TextDecorationCollection textDecorations = new TextDecorationCollection();
try
{
textDecorations.Add( contentRichTextBox.Selection.GetPropertyValue(
Inline.TextDecorationsProperty) as TextDecorationCollection);
if (isButtonChecked)
{ textDecorations.Add(TextDecorations.Underline); }
else { textDecorations.TryRemove(TextDecorations.Underline, out textDecorations);}
contentRichTextBox.Selection.ApplyPropertyValue(Inline.TextDecorationsProperty,
textDecorations);
}
catch (Exception)
{
textDecorations = new TextDecorationCollection();
if (!contentRichTextBox.Selection.IsEmpty)
{
var tpFirst = contentRichTextBox.Selection.Start;
var tpLast = contentRichTextBox.Selection.End;
var textRange = new TextRange(tpFirst, tpFirst.GetPositionAtOffset(1));
var underlined = (textRange.GetPropertyValue(Inline.TextDecorationsProperty)
as TextDecorationCollection).Contains(TextDecorations.Underline[0]);
for (TextPointer t = tpFirst;
t.CompareTo(tpLast) <= 0;
t = t.GetPositionAtOffset(1))
{
textDecorations.Clear();
textRange = new TextRange(t, t.GetPositionAtOffset(1));
textDecorations = textRange.GetPropertyValue(
Inline.TextDecorationsProperty) as TextDecorationCollection;
if (!underlined)
{ textDecorations.Add(TextDecorations.Underline[0]); }
else { textDecorations.TryRemove(TextDecorations.Underline,
out textDecorations); }
textRange.ApplyPropertyValue(Inline.TextDecorationsProperty,
textDecorations);
}
}
else
{
NewParagraphWhenSelectionIsEmpty();
}
}
}
Strikethrough button's method:
private void btnStrikethrough_Click(object sender, RoutedEventArgs e)
{
bool isButtonChecked = (sender as ToggleButton).IsChecked ?? false;
TextDecorationCollection textDecorations = new TextDecorationCollection();
try
{
textDecorations.Add(contentRichTextBox.Selection.GetPropertyValue(
Inline.TextDecorationsProperty) as TextDecorationCollection);
if (isButtonChecked)
{ textDecorations.Add(TextDecorations.Strikethrough); }
else { textDecorations.TryRemove(TextDecorations.Strikethrough,
out textDecorations); }
contentRichTextBox.Selection.ApplyPropertyValue(Inline.TextDecorationsProperty,
textDecorations);
}
catch (Exception)
{
textDecorations = new TextDecorationCollection();
if (!contentRichTextBox.Selection.IsEmpty)
{
var tpFirst = contentRichTextBox.Selection.Start;
var tpLast = contentRichTextBox.Selection.End;
var textRange = new TextRange(tpFirst, tpFirst.GetPositionAtOffset(1));
var strikedthrough = (textRange.GetPropertyValue(
Inline.TextDecorationsProperty) as
TextDecorationCollection).Contains(TextDecorations.Strikethrough[0]);
for (TextPointer t = tpFirst;
t.CompareTo(tpLast) <= 0;
t = t.GetPositionAtOffset(1))
{
textDecorations.Clear();
textRange = new TextRange(t, t.GetPositionAtOffset(1));
textDecorations = textRange.GetPropertyValue(
Inline.TextDecorationsProperty) as TextDecorationCollection;
if (!strikedthrough)
{ textDecorations.Add(TextDecorations.Strikethrough[0]); }
else { textDecorations.TryRemove(TextDecorations.Strikethrough,
out textDecorations); }
textRange.ApplyPropertyValue(Inline.TextDecorationsProperty,
textDecorations);
}
}
else
{
NewParagraphWhenSelectionIsEmpty();
}
}
}
Method used when selection in the richtextbox changes:
private void contentRichTextBox_SelectionChanged(object sender, RoutedEventArgs e)
{
var selectedWeight = contentRichTextBox.Selection.GetPropertyValue(
FontWeightProperty);
btnBold.IsChecked = (selectedWeight != DependencyProperty.UnsetValue)
&& (selectedWeight.Equals(FontWeights.Bold));
var selectedStyle = contentRichTextBox.Selection.GetPropertyValue(FontStyleProperty);
btnItalics.IsChecked = (selectedStyle != DependencyProperty.UnsetValue)
&& (selectedStyle.Equals(FontStyles.Italic));
var selectedDecoration = contentRichTextBox.Selection.GetPropertyValue(
Inline.TextDecorationsProperty) as TextDecorationCollection;
btnUnderline.IsChecked = (selectedDecoration != null)
&& (selectedDecoration != DependencyProperty.UnsetValue)
&& selectedDecoration.Contains(TextDecorations.Underline[0]);
btnStrikethrough.IsChecked = (selectedDecoration != null)
&& (selectedDecoration != DependencyProperty.UnsetValue)
&& selectedDecoration.Contains(TextDecorations.Strikethrough[0]);
cbEditorFontFamily.SelectedItem = contentRichTextBox.Selection.GetPropertyValue(
Inline.FontFamilyProperty);
if (contentRichTextBox.Selection.GetPropertyValue(Inline.FontSizeProperty)
!= DependencyProperty.UnsetValue)
{ cbEditorFontSize.Text = (contentRichTextBox.Selection.GetPropertyValue(
Inline.FontSizeProperty)).ToString(); }
}
Method used when inserting a new paragraph:
private void NewParagraphWhenSelectionIsEmpty()
{
var fontFamily = new FontFamily(cbEditorFontFamily.SelectedItem.ToString());
var fontSize = Convert.ToDouble(cbEditorFontSize.Text);
var fontStyle = (btnItalics.IsChecked ?? false) ? FontStyles.Italic :
FontStyles.Normal;
var fontWeight = (btnItalics.IsChecked ?? false) ? FontWeights.Bold :
FontWeights.Normal;
var textDecorations = new TextDecorationCollection();
if (btnUnderline.IsChecked ?? false)
{ textDecorations.Add(TextDecorations.Underline[0]); }
if (btnStrikethrough.IsChecked ?? false)
{ textDecorations.Add(TextDecorations.Strikethrough[0]); }
// Check to see if we are at the start of the textbox and nothing has been added yet
if (contentRichTextBox.Selection.Start.Paragraph == null)
{
// Add a new paragraph object to the richtextbox with the fontsize
Paragraph p = new Paragraph();
p.FontFamily = fontFamily;
p.FontSize = fontSize;
p.FontStyle = fontStyle;
p.FontWeight = fontWeight;
p.TextDecorations = textDecorations;
contentRichTextBox.Document.Blocks.Add(p);
}
else
{
// Get current position of cursor
TextPointer curCaret = contentRichTextBox.CaretPosition;
// Get the current block object that the cursor is in
Block curBlock = contentRichTextBox.Document.Blocks.Where
(x => x.ContentStart.CompareTo(curCaret) == -1
&& x.ContentEnd.CompareTo(curCaret) == 1).FirstOrDefault();
if (curBlock != null)
{
Paragraph curParagraph = curBlock as Paragraph;
// Create a new run object with the fontsize, and add it to the current block
Run newRun = new Run();
newRun.FontFamily = fontFamily;
newRun.FontSize = fontSize;
newRun.FontStyle = fontStyle;
newRun.FontWeight = fontWeight;
newRun.Foreground = new
SolidColorBrush((Color)wpfcpEditorFontColour.SelectedColor);
newRun.TextDecorations = textDecorations;
curParagraph.Inlines.Add(newRun);
// Reset the cursor into the new block.
// If we don't do this, the font size will default again when you start
// typing.
contentRichTextBox.CaretPosition = newRun.ElementStart;
}
}
}

C# Form/Control Width Not Updating All the Time

I've made a small tool bar that sits in a transparent form, it loads a variable sized menu from a text file and can be changed on the fly. Each button is a type of Label, the bar is just a list of buttons and adds/removes them in the correct spots. Width of the form is only a little bigger than the menu bar so that sub menu isn't cut off
Everything is working sweet except, when I reload everything part of the toolbar is lost. I've attempted to change the width so many ways, I've cleared and removed the controls from the form, refreshing the form/menu, updating it etc however nothing seems to make it work as intended EXCEPT if I call the reload function twice in a row, it works. I can't see why calling it once doesn't work but calling it twice works.
I'm fine with calling reload twice in a row as it would only be called a couple times a week.
Question: what on earth is causing this?
photo of issues first photo shows what it should look like, second is after removing a menu button and reloading, third is after adding a button and reloading
//calling this.reload() doesn't work
//calling this.reload();this.reload() works
void reload(Object o = null, EventArgs e = null)
{
this._menuBar.clear();
this.loadFromFile();
}
void loadFromFile(Object o = null, EventArgs e = null)
{
try
{
if (File.Exists("kpi.txt"))
{
string cline = "", cmenu = "", lhs = "";
menuList mb = null;
StreamReader sr = new StreamReader("kpi.txt");
while (!sr.EndOfStream)
{
cline = sr.ReadLine(); //get current line
if (cline.Length > 0 && cline[0] != ';')
{
//check if main menu/command
if (cline[0] == '[')
{
cmenu = Regex.Match(cline, #"(?<=^\[)[a-zA-Z -\#_{-~\^\r\n]+(?=\])").Value;
if (cmenu != "")
{
mb = this._menuBar.addMenuButton(cmenu);
mb.data["options"] = Regex.Match(cline, #"\/\w+$").Value;
var match = Regex.Match(cline, #"(?<=<)([^>\[\]\r\n]+)(?=>)");
mb.data["count"] = (match.Success ? match.Value : "0");
mb.data["copy"] = "";
applyMenuOptions(mb, false);
}
}
//just a standard line
else
{
cline = cline.Trim();
lhs = Regex.Match(cline, #"^[^\;\<\[\]\r\n]+(?=$|\<|\;)").Value;
if (mb.getSubMenuItem(lhs) == null)
{
var newButton = mb.addSubMenu(lhs);
if (newButton != null)
{
newButton.parent = mb;
newButton.data["options"] = mb.data["options"];
newButton.data["copy"] = Regex.Match(cline, #"((?<=\;)[^\[\]\<\r\n]+(?=<|$))").Value;
var matches = Regex.Match(cline, #"(?<=<)([^>\[\]\r\n]+)(?=>)");
int intout = 0;
if (int.TryParse(matches.Value, out intout))
{//no description
newButton.data["description"] = "";
newButton.data["count"] = intout.ToString();
}
else
{
newButton.data["description"] = matches.Value;
newButton.data["count"] = (matches.NextMatch().Success ? matches.NextMatch().Value : "0");
}
applyMenuOptions(newButton);
newButton.addMiddleClick(this.addcopy);
if (newButton.data["options"].Contains("i"))
{
newButton.addRightClick(this.appendInfo);
newButton.addRightClick(this.increment);
}
}
}
}
}
}
sr.Close();
this._menuBar.squish();
this.Width = this._menuBar.Width+50;
}
else
{
menuList mb = this._menuBar.addMenuButton("menu");
mb.data["options"] = "\\m";
mb.data["count"] = "0";
mb.data["copy"] = "";
mb.data["description"] = "";
applyMenuOptions(mb, false);
saveDictonary();
}
}
catch (Exception ex)
{
MessageBox.Show("Failed to load data " + ex);
//ILog log = LogManager.GetLogger(typeof(Program));
//log.Info(ex);
}
}
public menuList addMenuButton(string s, int w = 0, int h = 0, int x = -1, int y = -1)
{
menuList mb = new menuList(this._form, s);
if (this.menuItems.Exists(z => z.Text == s)) return null;
mb.Width = (w==0?settings.intOf("ButtonWidth"):w);
mb.Height = (h==0?settings.IntOf("ButtonHeight"):h);
if (x == -1 || y == -1)
mb.Location = new Point(this.menuItems.Count > 0 ? this.menuItems.Last().Location.X + this.menuItems.Last().Width : padding);
else mb.Location = new Point(x, y);
mb.BringToFront();
mb.Show();
this.menuItems.Add(mb);
// this.Refresh();
return mb;
}
internal void clear()
{
foreach(var i in this.menuItems)
{
this._form.Controls.Remove(i);
i.clear();
i.Dispose();
}
this.menuItems.Clear();
this._form.Controls.Remove(this);
this.menuItems = new List<menuList>();
this._form.Controls.Add(this);
}
internal void squish()
{
try
{
this.Width = (this.menuItems.Count * this.menuItems.First().Width) + (2 * padding);
}
catch(Exception ex) { MessageBox.Show(""+ex); }
}
Found the culprit, bother the button class and the tool bar class were both adding themselves to the form control instead of button class adding to the tool bar (picture box) controls!
Removing transparency showed the buttons not moving when the tool bar was moved!

How to disable more than one NumericUpDown controls using one method?

i have a form with more than one NumericUpDown as controls to input answer. i want every input is true for an operation (multiplication, sum etc), NumericUpDown for that operation will be disable. i have used the code below (just for sum operation), but i think its not efficient because i have to make a method to check every operation.
private void IsSumTrue() {
if (add1 + add2 == sum.Value)
{
sum.Enabled = false;
}
}
private void IsDifferenceTrue()
{
if (add1 - add2 == difference.Value)
{
difference.Enabled = false;
}
}
private void IsProductTrue()
{
if (add1 * add2 == product.Value)
{
product.Enabled = false;
}
}
private void IsQuotientTrue()
{
if (add1 / add2 == quotient.Value)
{
quotient.Enabled = false;
}
}
anyone have idea how to make it more efficient with just a method for all operation?
below is my idea, but to check the value is true for every NumericUpDown i don't know how.
private void DisableIfValueIsTrue()
{
foreach(Control control in this.Controls)
{
NumericUpDown value = control as NumericUpDown;
// if(value [NEED HELP]
}
}
Considering your situtaion, you can set a tag for each NumericUpDown in design mode like this:
sum.Tag=1;
square.Tag=2;
etc
Then define some int variables:
int iSum=add1+add2;
int iSquare= //Whatever you want
etc
And finally loop through your controls this way:
foreach (NumericUpDown control in this.Controls.OfType<NumericUpDown>())
{
int intCondition = Convert.ToInt32(control.Tag) == 1
? iSum
: Convert.ToInt32(control.Tag) == 2
? iSquare
: Convert.ToInt32(control.Tag) == 3
? i3
: i4; //You should extend this for your 8 controls
control.Enabled = intCondition == control.Value;
}
OK! Second way I offer
Since you will have to always check 8 different conditions, you could simply forget about looping through the controls and just change your method like this:
private void DisableIfValueIsTrue()
{
sum.Enabled = add1 + add2 != sum.Value;
difference.Enabled= add1 - add2 != difference.Value;
product.Enabled= add1 * add2 != product.Value;
quotient.Enabled= (add2 !=0) && (add1 / add2 != quotient.Value);
//etc
}
I came across this while doing some research and would like to give my solution I used for my situation and hope it helps people. I needed minimum and maximum numbers for a calculation, so mine are named appropriately and I correlated these with some CheckBoxes. I used null in beginning of minimum and end of maximum to account for empty. I also had to create an event handler SubscribeToEvents() shown below.
In my load event for my form:
SubscribeToEvents();
_checkBoxs = new[] { cbXLight, cbLight, cbMedium, cbHeavy, cbXHeavy, cbXXHeavy, cbXXXHeavy };
_minimumsNumericUpDowns = new[] { null, nLightMin, nMediumMin, nHeavyMin, nXHeavyMin, nXXHeavyMin, nXXXHeavyMin };
_maximumsNumericUpDowns = new[] { nXLightMax, nLightMax, nMediumMax, nHeavyMax, nXHeavyMax, nXXHeavyMax, null };
then I created a method:
private void DisableNumericUpDowns()
{
// disable everything:
foreach (var n in _minimumsNumericUpDowns)
{
if (n != null)
n.Enabled = false;
}
foreach (var n in _maximumsNumericUpDowns)
{
if (n != null)
n.Enabled = false;
}
}
The event handler:
private bool _eventsSubscribed;
private void SubscribeToEvents()
{
if (_eventsSubscribed)
return;
_eventsSubscribed = true;
cbXXHeavy.CheckedChanged += CheckBox_NumericState;
cbXHeavy.CheckedChanged += CheckBox_NumericState;
cbXLight.CheckedChanged += CheckBox_NumericState;
cbHeavy.CheckedChanged += CheckBox_NumericState;
cbLight.CheckedChanged += CheckBox_NumericState;
cbMedium.CheckedChanged += CheckBox_NumericState;
cbXXXHeavy.CheckedChanged += CheckBox_NumericState;
}
Now I can used this to check when they are enabled and if they are greater than or less than 0 if needed in the method CheckBox:
private void CheckBox_NumericState(object sender, EventArgs e)
{
// disable everything
DisableNumericUpDowns();
// see if more than one checkbox is checked:
var numChecked = _checkBoxs.Count((cb) => cb.Checked);
// enable things if more than one item is checked:
if (numChecked <= 1) return;
// find the smallest and enable its max:
var smallest = -1;
for (var i = 0; i < _checkBoxs.Length; i++)
{
if (!_checkBoxs[i].Checked) continue;
if (_maximumsNumericUpDowns[i] != null)
{
_maximumsNumericUpDowns[i].Enabled = true;
}
smallest = i;
break;
}
// find the largest and enable its min:
var largest = -1;
for (var i = _checkBoxs.Length - 1; i >= 0; i--)
{
if (!_checkBoxs[i].Checked) continue;
if (_minimumsNumericUpDowns[i] != null)
{
_minimumsNumericUpDowns[i].Enabled = true;
}
largest = i;
break;
}
// enable both for everything between smallest and largest:
var tempVar = largest - 1;
for (var i = (smallest + 1); i <= tempVar; i++)
{
if (!_checkBoxs[i].Checked) continue;
if (_minimumsNumericUpDowns[i] != null)
{
_minimumsNumericUpDowns[i].Enabled = true;
}
if (_maximumsNumericUpDowns[i] != null)
{
_maximumsNumericUpDowns[i].Enabled = true;
}
}
}
So I can check each state as required:
I want to check if Extra Light is check:
// Extra Light
if (!cbXLight.Checked) return;
if (nXLightMax.Enabled == false)
{
_structCategoryType = XLight;
CheckStructureSheets();
}
else
{
if (nXLightMax.Value > 0)
{
_dMax = nXLightMax.Value;
_structCategoryType = XLight;
CheckStructureSheets();
}
else
{
MessageBox.Show(#"Extra Light Max cannot be zero (0)");
}
}
and next light checks both:
// Light
if (cbLight.Checked)
{
if (nLightMin.Enabled == false && nLightMax.Enabled == false)
{
_structCategoryType = Light;
CheckStructureSheets();
}
else
{
if (nLightMin.Enabled && nLightMin.Value > 0)
{
if (nXLightMax.Enabled && nLightMin.Enabled && nLightMax.Enabled == false)
{
_dMin = nLightMin.Value;
_structCategoryType = Light;
CheckStructureSheets();
}
else
{
if (nLightMax.Value > 0)
{
_dMin = nLightMin.Value;
_dMax = nLightMax.Value;
_structCategoryType = Light;
CheckStructureSheets();
}
else
{
MessageBox.Show(#"Light Max cannot be zero (0)");
return;
}
}
}
else if (nLightMin.Enabled == false && nLightMax.Enabled)
{
if (nLightMax.Value > 0)
{
_dMax = nLightMax.Value;
_structCategoryType = Light;
CheckStructureSheets();
}
else
{
MessageBox.Show(#"Light Max cannot be zero (0)");
}
}
else
{
MessageBox.Show(#"Light Min cannot be zero (0)");
return;
}
}
}
Hope this helps someone.
Tim
thanks for #AlexJoliq and #BrettCaswell. just want to inform that before Alex edited his answer from using "==" to "!=", i (thought) already solved the problem. but i don't know where is the more effective and efficient way, alex's or mine.
below is my code for DisableIfValueIsTrue():
if (add1 + add2 == sum.Value) sum.Enabled = false;
if (add1 - add2 == difference.Value) difference.Enabled = false;
if (add1 * add2 == product.Value) product.Enabled = false;
if (add1 / add2 == quotient.Value) quotient.Enabled = false;

background worker RunWorker Completed event is executed even before do work event

I have WPF application where in the Ui I have a header check box and when it is checked all the body checkboxes are checked. When any checkbox is checked, Im doing some work that I need by calling the Background worker which works perfect. If I select any checkboxes individually without checking header then Background worker works fine.
Problem is when I check the header all the body checkboxes are checked and in this case the Background worker 'Run Worker completed' event is being executed even before 'do work' is executed which is breaking my code. Please help. Here is my code. RunJob is from command for button in front end.
public void RunJob(object obj)
{
obforSave = obj;
workerforRun = new BackgroundWorker();
workerforRun.WorkerReportsProgress = true;
workerforRun.DoWork += workerforRun_DoWork;
workerforRun.ProgressChanged += workerforRun_ProgressChanged;
workerforRun.RunWorkerCompleted += workerforRun_RunWorkerCompleted;
if (!workerforRun.IsBusy)
{
workerforRun.RunWorkerAsync();
}
}
//Special case header is checked Runwroker completed is executing before do work.
void workerforRun_DoWork(object sender, DoWorkEventArgs e)
{
IsValidationsComplete = false;
int count = 0;
//var obj = e.Argument;
if (IsRunJobAlreadyExecuted == false)
{
ResultExtract = JobEntities.Any(s => s.ExtractIsSelected != null && (bool)s.ExtractIsSelected);
var resultTransform = JobEntities.Any(x => x.TransformIsSelected != null && (bool)x.TransformIsSelected);
var resultLoad = JobEntities.Any(b => b.LoadIsSelected != null && (bool)b.LoadIsSelected);
//Check if any of the entities are either Extracted, Transformed or Loaded.
if (ResultExtract || resultTransform || resultLoad)
{
SaveJobConfigurationChanges(obforSave);
var jobEngine = new JobEngine();
var jobId = JobEntities[0].JobId;
jobEngine.ProcessJob(jobId);
MessageBox.Show("Job Execution Complete", "Run Job");
AllResults = GetJobConfigurationResults();
foreach (var item in AllResults)
{
if (item.ExtractIsSelected == true && item.ExtractStatus == "Completed Successfully")
{
count += Convert.ToInt32(item.ExtractRowsSelected);
}
if (item.TransformIsSelected == true && item.TransformStatus == "Completed Successfully")
{
count += Convert.ToInt32(item.TransformRowsSelected);
}
if (item.LoadIsSelected == true && item.LoadStatus == "Completed Successfully")
{
count += Convert.ToInt32(item.LoadRowsSelected);
}
}
workerforRun.ReportProgress(count);
//MessageBox.Show(count.ToString());
}
else
{
//When No Entity is Selected and the Run Button is pressed.
MessageBox.Show("Select an Entity First");
}
}
IsRunJobAlreadyExecuted = false;
}
void workerforRun_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progress = e.ProgressPercentage;
}
void workerforRun_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
{
/*When Header is checked and when the Run is clicked, you want to Uncheck the header back after completion.
This property is set in the CS file.*/
AllResults = GetJobConfigurationResults();
foreach (var item in AllResults)
{
item.ExtractIsSelected = false;
item.TransformIsSelected = false;
item.LoadIsSelected = false;
}
SaveTaskSelection();
JobEntitiesCollectionViewSource.Source = AllResults;
JobEntitiesCollectionViewSource.View.Refresh();
ExecuteFilteredEntitiesStoredProcedure();
IsValidateEnabled = AllResults.Any(p => p.ExtractStatus == "Completed Successfully");
}), DispatcherPriority.Background, null);
MessageBox.Show(progress.ToString());
IsValidationsComplete = true;
}
CS file:
public IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
var child = VisualTreeHelper.GetChild(depObj, i);
// If there is a child found and if the child is of the T type.
//Dont remove null check . If no check i
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
private IEnumerable<CheckBox> GetAllCheckBoxs()
{
var allCheckBoxes = FindVisualChildren<CheckBox>(UserControlJob);
return allCheckBoxes;
}
private void ChkHeaderExtract_OnChecked(object sender, RoutedEventArgs e)
{
var extractCheckBoxes = GetAllCheckBoxs();
var i = 0;
foreach (var chk in extractCheckBoxes)
{
if (i%3 == 0) // Since I have many sets of checkboxes and I want only the first set of checkboxes checked when header is clicked Im doing this.
{
chk.IsChecked = true;
}
i++;
}
}

How to Show AutoComplete Programmatically without entering a text

C# TextBox
AutoCompleteCustomSource has a List<string>,
AutoCompleteMode = Suggest.
I can see the List when I type a Letter.
How to show entire list without Typing a Letter Programmatically? This must be done while the User presses the Down Arrow Key in the TextBox.
Is there any Win32 API Available?
My Solution
I refined a Better Solution.
Add a ListBox Control to the form and make it as Visible = false
int curSelIndex = -1;
The below given Code will be executed Form_Load Event.
txtEmpId.AutoCompleteCustomSource.AddRange(EmpIds.ToArray());
lstAutoComplete.Items.Clear();
lstAutoComplete.Items.AddRange(EmpIds.ToArray());
txtEmpId.KeyDown += (ks, ke) =>
{
if (!(ke.KeyCode == Keys.Down ||
ke.KeyCode == Keys.Up ||
ke.KeyCode == Keys.Enter))
{
lstAutoComplete.Visible = false;
return;
}
ke.Handled = true;
if (ke.KeyCode == Keys.Enter)
{
if (lstAutoComplete.Visible)
{
var str = lstAutoComplete.SelectedItem + "";
// Process the Selected Item and set to TextBox.
}
}
if (!lstAutoComplete.Visible && txtEmpId.Focused)
{
var loc = txtEmpId.Location;
loc.Y += txtEmpId.Height;
lstAutoComplete.Location = loc;
lstAutoComplete.Size = txtEmpId.Size;
lstAutoComplete.Height = 100;
lstAutoComplete.SelectedIndex = 0;
curSelIndex = 0;
lstAutoComplete.Visible = true;
}
else if(lstAutoComplete.Visible && txtEmpId.Focused)
{
if (ke.KeyCode == Keys.Down)
{
curSelIndex++;
if (curSelIndex >= lstAutoComplete.Items.Count)
curSelIndex = lstAutoComplete.Items.Count - 1;
if (lstAutoComplete.Items.Count > 0)
lstAutoComplete.SelectedIndex = curSelIndex;
}
else if (ke.KeyCode == Keys.Up)
{
curSelIndex--;
if (curSelIndex < 0)
curSelIndex = 0;
if (lstAutoComplete.Items.Count > 0)
lstAutoComplete.SelectedIndex = curSelIndex;
}
}
};
txtEmpId.Leave += (ls, le) => lstAutoComplete.Visible = false;
I didn't find any API for your problem, so I just make a my own suggestion box by using ListBox to show when the Down Arrow Key is pressed, when you do other operation, it disappeares. I hope it is useful to you. code sample is bellow:
//string datasource
List<string> strList = null;
//suggestion listbox
ListBox sugBox = null;
public FrmTextSuggest()
{
InitializeComponent();
//setting the textbox control
strList = new List<string>()
{
"USA",
"England",
"China",
"Japan",
"Korea",
"India",
"France",
"Canada"
};
var autoCollection = new AutoCompleteStringCollection();
autoCollection.AddRange(strList.ToArray());
this.txtCountry.AutoCompleteCustomSource = autoCollection;
this.txtCountry.AutoCompleteMode = AutoCompleteMode.Suggest;
this.txtCountry.AutoCompleteSource = AutoCompleteSource.CustomSource;
//register the Down Arrow Key event
this.txtCountry.KeyDown += new KeyEventHandler(txtCountry_KeyDown);
}
void txtCountry_KeyDown(object sender, KeyEventArgs e)
{
//show the your own suggestion box when pressing down arrow and the text box is empty
if (e.KeyCode == Keys.Down && txtCountry.Text.Trim().Equals(""))
{
sugBox = new ListBox();
//define the box
sugBox.Width = txtCountry.Width;
Point p = txtCountry.Location;
p.Y += txtCountry.Height;
sugBox.Location = p;
sugBox.Items.AddRange(strList.ToArray());
//copy the value to the textbox when selected index changed.
sugBox.SelectedIndexChanged += new EventHandler(sugBox_SelectedIndexChanged);
//show box
if (sugBox.Items.Count > 0)
{
sugBox.SelectedIndex = 0;
this.Controls.Add(sugBox);
sugBox.Focus();
}
}
//remove and hide your own suggestion box when other operation
else
{
if (sugBox != null && this.Controls.Contains(sugBox))
{
this.Controls.Remove(sugBox);
sugBox.Dispose();
sugBox = null;
}
}
}
void sugBox_SelectedIndexChanged(object sender, EventArgs e)
{
string selText = this.sugBox.SelectedItem.ToString();
if (!string.IsNullOrEmpty(selText))
{
this.txtCountry.Text = selText;
}
}
here is my result of test:

Categories

Resources