I'm learning from Mark Heath course of NAudio.
I'm using a slider with the event:
Thumb.DragCompleted="SilderPositionOnDragCompleted"
And in the c#:
private void SilderPositionOnDragCompleted(object sender, System.Windows.Controls.Primitives.Thumb.DragCompleted e)
{
if (reader != null)
{
reader.CurrentTime = TimeSpan.FromSeconds(slider.Value);
}
}
I'm pretty sure that I wrote wrong the:
System.Windows.Controls.Primitives.Thumb.DragCompleted
Because I have no idea what I need to write in there - I saw this here in the site.
Here are the errors.
What do I need to do?
Thanks!
Try this:
private void SilderPositionOnDragCompleted(object sender, RoutedEventArgs e)
{
if (reader != null)
{
reader.CurrentTime = TimeSpan.FromSeconds(slider.Value);
}
}
The type of the second argument should be RoutedEventArgs.
And if you are hooking up the event handler programmatically you should use the following syntax:
Thumb.DragCompleted += SilderPositionOnDragCompleted;
...where "Thumb" is the name of your Thumb:
<Thumb x:Name="Thumb" />
Or
Thumb Thumb = ...;
Related
In my current project, I am trying to make a button change its image when a particular key is pressed. So far, my code looks something like
if (e.KeyCode == Keys.H)
{
button1.Image = bitmap.FromFile(C: filename\filename\filename\filename);
}
I don't know if I'm calling the file correctly, or using the right method or class. I'm still fairly new to this, so a simple explanation is probably best, thanks.
If you include the images to your resources you can do it like this:
public Form1()
{
InitializeComponent();
button1.MouseEnter += new EventHandler(button1_MouseEnter);
button1.MouseLeave += new EventHandler(button1_MouseLeave);
}
void button1_MouseLeave(object sender, EventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img1));
}
void button1_MouseEnter(object sender, EventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img2));
}
I would not recommend hardcoding image paths.
For reference i use this link... maybe some other answer help you as well.
Reference: LINK
In my program I would like to call to a SelectedItemChanged event using c# code-behind, I am just unsure about what to pass as parameters. This is for a TreeViewItem.
//Gets selected item in TreeView
private void TreeOne_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
MainWindowViewModel.SelectedItem = e.NewValue as TreeViewItem;
}
//I'm calling the SelectedItemChanged event from a RightButtonDown event
private void TreeOne_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
TreeOne_SelectedItemChanged(/* What would go here? **/);
}
Also, when I try to build this I receive this compiler error that pretty much led to this question...
No overload for method TreeOne_SelectedItemChanged takes '0' arguments
I'm hoping that this is an easy question, but if I have not provided enough information, or haven't been clear enough please let me know.
Adding to #Bart Friederichs' answer and assuming that you have a reference to your TreeView, you could add the following method:
private void SetSelectedItem()
{
MainWindowViewModel.SelectedItem = TreeOne.SelectedItem;
}
Then you can simply call this from wherever you like:
private void TreeOne_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
SetSelectedItem();
}
private void TreeOne_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
SetSelectedItem();
}
The usual design pattern would be to call some kind of processing method, and not to "manually" fire events:
private TreeOne_SelectedItemChaned(object sender,
RoutedPropertyChangedEventArgs<object> e) {
processChange();
}
Then, from withing your code, you just call processChange(), no need to call TreeOne_SelectedItemChanged.
try to call
TreeOne_SelectedItemChanged(null, null);
Working in VS 2012, WinForms, C#...
I have a ListBox I would like to populate depending upon the value selected in a ComboBox. I've tested my SQL Query and it works, but I'm getting a weird problem where, when I run my routines, my ComboBox comes up empty, as well as my ListBox. When I comment out the code in my cb_Session_SelectedValueChanged routine, my CB and LB load just fine, but when it's not commented out is when my LB and CB end up blank.
This is what I have:
private void cb_Session_SelectedValueChanged(object sender, EventArgs e)
{
listbox_Sessions.Visible = true;
LoadSessionListbox();
}
private void LoadSessionListbox()
{
int tempID = Convert.ToInt32(cb_Session.SelectedValue);
// Code here to load listbox, which works without above routine.
}
Am I missing something? Why are my CB and LB blank with that first routine added?
[EDIT]:
I put the routines which were in SelectedValueChanged in a MouseClick event and it works, but not when I want it to... You have to click a couple times to get it to re-load with the correct ID. I feel like I'm getting closer, but still not the right event.
Try this:
private void cb_Session_SelectedValueChanged(object sender, EventArgs e)
{
if(cb_Session.SelectedValue>-1)
{
listbox_Sessions.Visible = true;
LoadSessionListbox();
}
}
Figured it out!!
I ended up adding a simple if statement to my SelectedValueChanged routine, and it fixed everything!
private void cb_Sessions_SelectedValueChanged(object sender, EventArgs e)
{
listBox_Sessions.Visible = true;
if (cb_Sessions.SelectedValue != null)
LoadSessionListbox();
}
Works perfectly now.
Try in SelectedIndexChanged Event and follow
private void cb_Session_SelectedIndexChanged(object sender, EventArgs e)
{
if (cb_Session.SelectedValue == null) return;
if (cb_Session.SelectedIndex == -1) return;
listbox_Sessions.Visible = true;
LoadSessionListbox((int)cb_Session.SelectedValue);
}
private void LoadSessionListbox(int selectedValue)
{
//TODO: Do stuff
}
Sorry i'm new in C# and i'm looking everywhere, and can't find even it looks easy to do.
I want to get the object by click on it but i don't know how to do it.
A simple button in xaml :
<TextBlock
Text="{Binding ProjectName}"
VerticalAlignment="Center" Tapped="On_Tapped_Project"/>
And i Use a simple function :
private void On_Tapped_Project(object sender, TappedRoutedEventArgs e)
{
this.Frame.Navigate(typeof(NoteFolders), MyProjects[3]);
}
But i would like to have the specific project like MyProjects[x] x=(click Project).
Any ideas ?
Try this
var element = (sender as FrameworkElement);
if (element != null)
{
var project = element.DataContext as Project;
if (project != null)
{
//Implementation
}
}
You need to cast the event OriginalSource to your type (e.g. Project):
this.Frame.Navigate(typeof(NoteFolders), (Project) e.OriginalSource);
You can store project id in a tag property of a control
<TextBlock
Text="{Binding ProjectName}" Tag="{Binding ProjectID}"
VerticalAlignment="Center" Tapped="On_Tapped_Project"/>
You can recover it by access to a tag property
private void On_Tapped_Project(object sender, TappedRoutedEventArgs e)
{
var projectID = ((TextBlock)sender).Tag as int;
Frame.Navigate(typeof(NoteFolders), MyProjects[projectID]);
}
private void On_Tapped_Project(object sender, TappedRoutedEventArgs e)
{
var projectID = ((TextBlock)sender).Tag as int;
Frame.Navigate(typeof(NoteFolders), MyProjects[projectID]);
}
is a way to do it, but it is by far more recommended to do it this way:
private void On_Tapped_Project(object sender, TappedRoutedEventArgs e)
{
var projectID = (sender as TextBlock).Tag as int;
Frame.Navigate(typeof(NoteFolders), MyProjects[projectID]);
}
"sender as ElementType" is the correct way to cast sender, other methods can cause minor issues, always do the best code and never wonder why it went wrong that one time.
am wondering why this code fails to focus the textbox...?
private void sendEmail_btn_Click(object sender, EventArgs e)
{
String sendTo = recipientEmail_tbx.Text.Trim();
if (!IsValidEmailAddress(sendTo))
{
MessageBox.Show("Please Enter valid Email address","Cognex" MessageBoxButtons.OK, MessageBoxIcon.Error);
recipientEmail_tbx.Focus();
}
}
Use Select() instead:
recipientEmail_tbx.Select();
Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx
Add Delay some miliSec. Delay then call Focus() and Not forget to put inside Dispatcher.
Task.Delay(100).ContinueWith(_ =>
{
Application.Current.Dispatcher.Invoke(new Action(() =>
{
TextBoxNAme.Focus();
}));
});
Even I tried with lots of above solutions but none of them worked for me as am trying to focus on page load. Finally I got this solution and it worked.
private void txtBox_LayoutUpdated(object sender, EventArgs e)
{
txtBox.Focus();
}
Use the Form_Activated event handler, in conjunction with a firstActivation boolean.
private bool firstActivation = true;
private Control firstWindowsControl = null;
...
private void DynamicForm_Activated(object sender, EventArgs e)
{
if (firstActivation)
{
firstActivation = false;
bool fwcPresent = (firstWindowsControl != null);
Console.WriteLine($"DynamicForm_Activated: firstWindowControl present: {fwcPresent}");
if (fwcPresent)
{
firstWindowsControl.Focus();
}
}