How can i stop AxWindowsMediaPlayer from accepting any user commands in C# - c#

In my democode an embeded windows media player starts to load and play a video. The player shows no controls, all other options are the default options. So far this works.
What does not work is that not all userinteraction is stopped. For instance it is possible to change the mode to fullscreen with a doubleclick and it also is possible to get a full context with a right click.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
axWindowsMediaPlayer1.uiMode = "none";
}
private void button1_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = #"C:\stackoverflow.mp4";
}
}
How can i isolate the player from the user and only control the player via code?

A friend just helped me to solve this.
Disabling the context menĂ¼ was rather easy
axWindowsMediaPlayer1.enableContextMenu = false;
Disabling the doubleclick requires a message filter - there is already a solution on the web.
Application.AddMessageFilter((IMessageFilter)CustomFilter(this/*Form*/, axWMP));
I have rewritten my example and i am now using this code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
axWindowsMediaPlayer1.uiMode = "none";
axWindowsMediaPlayer1.enableContextMenu = false;
Application.AddMessageFilter(new IgnoreMouseClickMessageFilter(this, axWindowsMediaPlayer1));
}
private void button1_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = #"C:\stackoverflow.mp4";
}
}
class IgnoreMouseClickMessageFilter : IMessageFilter
{
private Control parent { get; set; }
private Control target { get; set; }
public IgnoreMouseClickMessageFilter(Control parent, Control target)
{
this.parent = parent;
this.target = target;
}
public bool PreFilterMessage(ref Message messageBeforeFiltering)
{
const Boolean FilterTheMessageOut = true;
const Boolean LetTheMessageThrough = false;
if (IsNull(parent)) return LetTheMessageThrough;
if (IsNull(target)) return LetTheMessageThrough;
if (WasNotClickedOnTarget(parent, target)) return LetTheMessageThrough;
if (MessageContainsAnyMousebutton(messageBeforeFiltering)) return FilterTheMessageOut;
return LetTheMessageThrough;
}
private bool MessageContainsAnyMousebutton(Message message)
{
if (message.Msg == 0x202) return true; /* WM_LBUTTONUP*/
if (message.Msg == 0x203) return true; /* WM_LBUTTONDBLCLK*/
if (message.Msg == 0x204) return true; /* WM_RBUTTONDOWN */
if (message.Msg == 0x205) return true; /* WM_RBUTTONUP */
return false;
}
private bool WasNotClickedOnTarget(Control parent, Control target)
{
Control clickedOn = parent.GetChildAtPoint(Cursor.Position);
if (IsNull(clickedOn)) return true;
if (AreEqual(clickedOn, target)) return false;
return true;
}
private bool AreEqual(Control controlA, Control controlB)
{
if (controlA == controlB) return true;
return false;
}
private bool IsNull(Control control)
{
if (control == null) return true;
return false;
}
}
Special thanks to my unnamed friend and to "remarkpk11" from the Microsoft Developer Network Frorums.
There are some smaller issues with the code - i dont like that the Messages are hidden from me in the first place and i also would love to get rid of the two global dependencies Cursor and Application. But as far as this question goes i consider it answered.

try axWindowsMediaPlayer1.Ctlenabled = False
EDIT: sorry, this is for vb..

axWindowsMediaPlayer1.Ctlcontrols.stop();

Related

Unity: Quiz game multiple choice button colors

I'm doing a multiple choice quiz game, and im tryinng to highlight the correct answer in green if the wrong answer is clicked. for example if i have A , B , C and D and "A" is the correct answer. i want if i choose "B" to turn B to red color and display the correct answer to green which is in this case "A". Right now i have if i click the right answer it displays green, as i want it. and if i press wrong answer it displays red. what im missing is when i click wrong answer i want to also highlight the correct answer.
Here's my functions:
In the Game controller:
public bool theAnswerIsCorrect;
public bool IsCorrected()
{
return theAnswerIsCorrect;
}
public void AnswerButtonClick(bool isCorrect)
{
if (isCorrect)
{
Debug.Log("I'm Correct");
theAnswerIsCorrect = true;
playerScore += currentRoundData.pointAddedForCorrectAnswer;
scoreDisplayText.text = "Score: " + playerScore.ToString();
}
else
theAnswerIsCorrect = false;
// Do we still have questions?
if (questionPool.Length > questionIndex + 1)
{
//questionIndex++;
UpdateQuestionIndex();
StartCoroutine(DelayTime(3));
// ShowQuestion();
}
else
{
EndRound();
}
}
And this is an a class that just holdes my data
public class answerData {
public string answerTxt;
public bool isCorrect;
}
This is being used in the ButtonClick function (the code in the begging of the question) -> this line
gameController.AnswerButtonClick (AnswerData.isCorrect);
And in the inspector i specify by the bool what is the correct answer
*** NEW SCRIPT HERE "Picking up the Correct button"
// store reference to btn text
public Text answerText;
private answerData AnswerData;
private GameController gameController;
public static AnswerButton _instace;
private void Awake()
{
_instace = this;
}
// Use this for initialization
void Start ()
{
gameController = FindObjectOfType<GameController> ();
}
public void Setup(answerData data)
{
AnswerData = data;
answerText.text = AnswerData.answerTxt;
}
IEnumerator ReturnButtonColor()
{
yield return new WaitForSeconds(2.9f);
GetComponent<Button>().image.color = Color.white;
Debug.Log("HiB");
}
public void HandleClick()
{
gameController.AnswerButtonClick (AnswerData.isCorrect);
if (gameController.IsCorrected())
{
GetComponent<Button>().image.color = Color.green;
Debug.Log("im true");
StartCoroutine(ReturnButtonColor());
}
else
{
GetComponent<Button>().image.color = Color.red;
// gameController.IsCorrected().image.color = Color.green;
StartCoroutine(ReturnButtonColor());
}
}
Thank you
I am assuming that all option button have same script attached.
Create one delegate and register this in your script which is attached to your option button.
Like this
Create delegate and event
#region DELEGATES
public delegate void OnQuestionOptionClicked ();
public static event OnQuestionOptionClicked onQuestionOptionClicked;
#endregion
#region DELEGATE_CALLS
private void RaiseOnQuestionOptionClicked ()
{
if (onQuestionOptionClicked != null)
onQuestionOptionClicked ();
}
#endregion
Register it
void OnEnable ()
{
onQuestionOptionClicked += OnQuestionOptionClicked;
}
void OnDisable ()
{
onQuestionOptionClicked -= OnQuestionOptionClicked;
}
#region DELEGATE_EVENT_LISTENER
void OnQuestionOptionClicked ()
{
GetComponent<Button>().interactable = false;
if (AnswerData.isCorrect){
GetComponent<Button>().image.color = Color.green;
Debug.Log("im true");
}
}
#endregion
Your setup method
public void Setup(answerData data)
{
AnswerData = data;
answerText.text = AnswerData.answerTxt;
GetComponent<Button>().interactable = true;
GetComponent<Button>().image.color = Color.white;
}
And on button click event
#region BUTTON_CLICK_LISTENER
public void ButtonClick()
{
gameController.AnswerButtonClick (AnswerData.isCorrect);
if (!gameController.IsCorrected())
{
GetComponent<Button>().image.color = Color.red;
Debug.Log("im true");
// StartCoroutine(ReturnButtonColor());
}
RaiseOnQuestionOptionClicked ();
}
#endregion
Hope this solution helps you.;)
Like using GetComponent<Button>().image.color = Color.green; in the if statement , you could simply add in /*Button A*/.image.color = Color.green; in the else statement.
What #BadWolf in the comments is saying is the correct answer. If you need more coding help you have to include more information. How do you determine the Correct Button? Does it have a special name in the Scene? Does the gameController know which button is the correct? It seems gameController knows which Button is the correct one so you should create a method which will look something like:
public Button GetCorrectButton(){
return theCorrectButton;
}
I don't know the code for how you see if it is the correct button but I'm guessing you have some way where you use the Correct Button. Find the Correct Button and return it in the method GetCorrectButton.
which you will then use in your code like:
public void ButtonClick()
{
gameController.AnswerButtonClick (AnswerData.isCorrect);
if (gameController.IsCorrected())
{
GetComponent<Button>().image.color = Color.green;
Debug.Log("im true");
// StartCoroutine(ReturnButtonColor());
}
else
{
GetComponent<Button>().image.color = Color.red;
// Like this:
gameController.GetCorrectButton().image.color = Color.green;
}
}
I can be more specific if I get some more information/code to see!

How to cancel tab change in WPF TabControl

I have found multiple questions about this problem on SO, however I still can't quite get a realiable solution. Here is what I came up with after reading the answers.
Xaml:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="300" Width="300" x:Name="this">
<TabControl IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Tabs, ElementName=this}" x:Name="TabControl"/>
</Window>
Code behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var tabs = new ObservableCollection<string> {"Tab1", "Tab2", "Tab3"};
Tabs = CollectionViewSource.GetDefaultView(tabs);
Tabs.CurrentChanging += OnCurrentChanging;
Tabs.CurrentChanged += OnCurrentChanged;
Tabs.MoveCurrentToFirst();
CurrentTab = tabs.First();
}
private void OnCurrentChanging(object sender, CurrentChangingEventArgs e)
{
//only show message box when tab is changed by user input
if (!_cancelTabChange)
{
if (MessageBox.Show("Change tab?", "Message", MessageBoxButton.YesNo) == MessageBoxResult.No)
{
_cancelTabChange = true;
return;
}
}
_cancelTabChange = false;
}
private void OnCurrentChanged(object sender, EventArgs e)
{
if (!_cancelTabChange)
{
//Update current tab property, if user did not cancel transition
CurrentTab = (string)Tabs.CurrentItem;
}
else
{
//navigate back to current tab otherwise
Dispatcher.BeginInvoke(new Action(() => Tabs.MoveCurrentTo(CurrentTab)));
}
}
public string CurrentTab { get; set; }
public static readonly DependencyProperty TabsProperty = DependencyProperty.Register("Tabs", typeof(ICollectionView), typeof(MainWindow), new FrameworkPropertyMetadata(default(ICollectionView)));
public ICollectionView Tabs
{
get { return (ICollectionView)GetValue(TabsProperty); }
set { SetValue(TabsProperty, value); }
}
private bool _cancelTabChange;
}
Basically I want to display a confirmation message, when user navigates to different tab, and if he clicks "no" - abort the transition. This code does not work though. If you click multiple times on "Tab2", each time choosing "no" in message box, at some point it stops working: events stop triggering. Event will trigger again if you click on "Tab3", but if you choose "yes" it opens second tab and not third. I am having trouble figuring out wtf is going on. :)
Does anyone see a bug in my solution? Or is there an easier way to display a confirmation message, when user switches tabs? I am also willing to use any opensource tab control, which does have a proper SelectionChanging event. I could not find any though.
I am using .Net 4.0.
Edit:
If I comment the message box out:
private void OnCurrentChanging(object sender, CurrentChangingEventArgs e)
{
//only show message box when tab is changed by user input
if (!_cancelTabChange)
{
//if (MessageBox.Show("Change tab?", "Message", MessageBoxButton.YesNo) == MessageBoxResult.No)
//{
Debug.WriteLine("Canceled");
_cancelTabChange = true;
return;
//}
}
_cancelTabChange = false;
}
Everything works fine. Weird.
This solution http://coderelief.net/2011/11/07/fixing-issynchronizedwithcurrentitem-and-icollectionview-cancel-bug-with-an-attached-property/
seems to work quite well with
<TabControl ... yournamespace:SelectorAttachedProperties.IsSynchronizedWithCurrentItemFixEnabled="True" .../>
private void OnCurrentChanging(object sender, CurrentChangingEventArgs e)
{
if (MessageBox.Show("Change tab?", "Message", MessageBoxButton.YesNo) == MessageBoxResult.No)
{
e.Cancel = true;
}
}
public static class SelectorAttachedProperties
{
private static Type _ownerType = typeof(SelectorAttachedProperties);
#region IsSynchronizedWithCurrentItemFixEnabled
public static readonly DependencyProperty IsSynchronizedWithCurrentItemFixEnabledProperty =
DependencyProperty.RegisterAttached("IsSynchronizedWithCurrentItemFixEnabled", typeof(bool), _ownerType,
new PropertyMetadata(false, OnIsSynchronizedWithCurrentItemFixEnabledChanged));
public static bool GetIsSynchronizedWithCurrentItemFixEnabled(DependencyObject obj)
{
return (bool)obj.GetValue(IsSynchronizedWithCurrentItemFixEnabledProperty);
}
public static void SetIsSynchronizedWithCurrentItemFixEnabled(DependencyObject obj, bool value)
{
obj.SetValue(IsSynchronizedWithCurrentItemFixEnabledProperty, value);
}
private static void OnIsSynchronizedWithCurrentItemFixEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Selector selector = d as Selector;
if (selector == null || !(e.OldValue is bool && e.NewValue is bool) || e.OldValue == e.NewValue)
return;
bool enforceCurrentItemSync = (bool)e.NewValue;
ICollectionView collectionView = null;
EventHandler itemsSourceChangedHandler = null;
itemsSourceChangedHandler = delegate
{
collectionView = selector.ItemsSource as ICollectionView;
if (collectionView == null)
collectionView = CollectionViewSource.GetDefaultView(selector);
};
SelectionChangedEventHandler selectionChangedHanlder = null;
selectionChangedHanlder = delegate
{
if (collectionView == null)
return;
if (selector.IsSynchronizedWithCurrentItem == true && selector.SelectedItem != collectionView.CurrentItem)
{
selector.IsSynchronizedWithCurrentItem = false;
selector.SelectedItem = collectionView.CurrentItem;
selector.IsSynchronizedWithCurrentItem = true;
}
};
if (enforceCurrentItemSync)
{
TypeDescriptor.GetProperties(selector)["ItemsSource"].AddValueChanged(selector, itemsSourceChangedHandler);
selector.SelectionChanged += selectionChangedHanlder;
}
else
{
TypeDescriptor.GetProperties(selector)["ItemsSource"].RemoveValueChanged(selector, itemsSourceChangedHandler);
selector.SelectionChanged -= selectionChangedHanlder;
}
}
#endregion IsSynchronizedWithCurrentItemFixEnabled
}
For some reason adding TabControl.Focus() fixes things:
private void OnCurrentChanged(object sender, EventArgs e)
{
if (!_cancelTabChange)
{
//Update current tab property, if user did not cancel transition
CurrentTab = (string)Tabs.CurrentItem;
}
else
{
//navigate back to current tab otherwise
Dispatcher.BeginInvoke(new Action(() =>
{
Tabs.MoveCurrentTo(CurrentTab);
TabControl.Focus();
}));
}
}
I still have no clue what on Earth is going on here. So I will gladly accept the answer, which sheds some light on this issue.
inside the tabControl_SelectionChanged event handler:
if (e.OriginalSource == tabControl) //if this event fired from your tabControl
{
e.Handled = true;
if (!forbiddenPage.IsSelected) //User leaving the tab
{
if (forbiddenTest())
{
forbiddenPage.IsSelected = true;
MessageBox.Show("you must not leave this page");
}
}
Note that setting forbiddenPage.IsSelected = true causes a loop and you reenter
this event handler. This time, however, we exit because the page selected IS the forbidden page.
private void MainTabControl_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ReasonBecauseLeaveTabItemIsForbidden)
{
if (MainTabControl.SelectedIndex == IndexOfTabItem)
{
MessageBox.Show(SomeMessageWhyLeaveTabItemIsForbidden);
}
MainTabControl.SelectedIndex = IndexOfTabItem;
}
}
IndexOfTabItem - index of TabItem that disabled for leaving.
He who must be obeyed requested that the application ask the user if they wish to leave the page so here is the slightly changed code:
private Object _selectedTab;
public Object SelectedTab
{
get
{
return _selectedTab;
}
set
{
if (
!(_selectedTab is ADR_Scanner.ViewModel.ConfigurationViewModel) ||
!_configurationViewModel.HasChanged ||
(System.Windows.Forms.MessageBox.Show("Are you sure you want to leave this page without saving the configuration changes", ADR_Scanner.App.Current.MainWindow.Title, System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Error) == System.Windows.Forms.DialogResult.Yes)
)
{
_selectedTab = value;
}
OnPropertyChanged("SelectedTab");
}
}
I think this small change does pretty much what you wanted.
There is a much easier solution. Add a binding to the selected item in the XAML:
<TabControl SelectedItem="{Binding SelectedTab}" ...
Then in the view model:
private Object _selectedTab;
public Object SelectedTab
{
get
{
return _selectedTab;
}
set
{
if (_selectedTab is ADR_Scanner.ViewModel.ConfigurationViewModel && _configurationViewModel.HasChanged)
{
System.Windows.Forms.MessageBox.Show("Please save the configuration changes", ADR_Scanner.App.ResourceAssembly.GetName().Name, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
}
else
{
_selectedTab = value;
}
OnPropertyChanged("SelectedTab");
}
}
Obviously you replace ADR_Scanner.ViewModel.ConfigurationViewModel with your own view model class. Lastly make sure you initialise _selectedTab in your constructor otherwise the TabControl will have no initial selection.

need a button to change its text when clicked

I am trying to do a simple toggle silent mode app. I got the for dummies book and no luck so far.
What I want to do is when the audio is on I want to change my text on my button to Turn off (using a string). and when pressed I want it to change to another string. But I can't get this thing down right. I have tried several ways so far. Any suggestions ?
package com.example.silentmodetoggle;
import android.os.Bundle;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class TSMMainActivity extends Activity {
//Instantiate an AudioManager to manage the ringer state
private AudioManager mAudioManager;
private boolean mPhoneIsSilent;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tsmmain);
mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
checkIfPhoneIsSilent();
setButtonClickListener();
//does this even work?
TextView textView=(TextView)findViewById(R.string.silence_phone);
}//onCreate
private void setButtonClickListener()
{
Button toggleButton = (Button) findViewById(R.id.toggleButton);
toggleButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if (mPhoneIsSilent)
{ //set to normal mode
mAudioManager
.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
mPhoneIsSilent = false;
}
else
{
mAudioManager
.setRingerMode(AudioManager.RINGER_MODE_SILENT);
mPhoneIsSilent = true;
}
//now toggle the UI again
toggleUi();
}
});
}
private void checkIfPhoneIsSilent()
{
int ringerMode = mAudioManager.getRingerMode();
if (ringerMode == AudioManager.RINGER_MODE_SILENT)
{
mPhoneIsSilent = true;
}
else
{
mPhoneIsSilent = false;
}
}
private void toggleUi()
{
ImageView imageView = (ImageView) findViewById(R.id.phone_icon);
Drawable newPhoneImage;
if (mPhoneIsSilent)
{
//i think i should put it here.
newPhoneImage = getResources().getDrawable(R.drawable.phone_silent);
}
else
{
newPhoneImage = getResources().getDrawable(R.drawable.phone_on);
}
imageView.setImageDrawable(newPhoneImage);
}
private void textView(int silencePhone) {
// TODO Auto-generated method stub
}
#Override
protected void onResume()
{
super.onResume();
checkIfPhoneIsSilent();
toggleUi();
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tsmmain, menu);
return true;
}
}
Try this:
#Override
public void onClick(View v)
{
if (mPhoneIsSilent)
{ //set to normal mode
mAudioManager
.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
mPhoneIsSilent = false;
((Button)v).setText("Turn On");
}
else
{
mAudioManager
.setRingerMode(AudioManager.RINGER_MODE_SILENT);
mPhoneIsSilent = true;
((Button)v).setText("Turn Off");
}
Hope this helps :)
In the OnClickListener(View v) the argument v is the button you clicked on. Therefore you can call the method setText(String s) and change the text.
It should be explicitly cast to its type as v is a generic type of View.

Toggle state in c# - wpf

I want to make a function to work in toggle(switch) mode when i press a key and i really can't figure how to do it. I tried lots of ways and only the "RegisterHotKey" method is working fine. But "RegisterHotKey" is overwriting the mapped key from the game and this is not what i need. So i'm trying to use "GetKeyState". The code below it's only working for one position no matter what i change...:
private void mw_KeyDown(object sender, KeyEventArgs e){
bool sw = (toggle = !toggle);
int tog = (GetKeyState(Key.Tab));
if ((tog & 1) == 1)
{
if (sw)
{
System.Windows.MessageBox.Show("go to second position...!");
}
}
else
{
System.Windows.MessageBox.Show("go to first position...!");
}
}
Any idea or suggestion how can i do this ?
Thank you,
Solution provided by Sergey Alexandrovich Kryukov from CodeProject.
Link: solution
public partial class MainWindow : Window
{
bool toggle;
public MainWindow()
{
InitializeComponent();
MainWindow.KeyDown += (sender, eventArgs) => { if (eventArgs.Key == Key.F7) toggle = !toggle; };
}
private void MainWindow_KeyDown(object sender, EventArgs e)
{
if (toggle)
{
//System.Windows.MessageBox.Show("go to second position...!");
}
else
{
//System.Windows.MessageBox.Show("go to first position...!");
}
}
}

Whilst using drag and drop, can I cause a Treeview to Expand the node over which the user hovers?

In brief:
Is there any built-in function in .Net 2.0 to Expand TreeNodes when hovered over whilst a drag and drop operation is in progress?
I'm using C# in Visual Studio 2005.
In more detail:
I've populated a Treeview control with a multi levelled, multinoded tree (think organisational chart or file/folder dialog) and I want to use drag and drop to move nodes within the tree.
The drag drop code works well, and I can drop onto any visible node, however I would like my control to behave like Windows Explorer does when dragging files over the folder pane. Specifically, I'd like each folder to open if hovered over for 1/2 second or so.
I've begun developing a solution using Threading and a Sleep method but I'm running into problems and wondered if there was something in place already, if not I will knuckle down and learn how to use threading (it's about time, but I was hoping to get this app out quickly)
Do I need to write my own code to handle Expanding a TreeNode when hovered over in drag-drop mode?
You can use the DragOver event; it fires repeatedly while you are dragging an object
Opening after a delay can be done very easily with two extra variables that note the last object under the mouse and the time. No threading or other tricks required (lastDragDestination and lastDragDestinationTime in my example)
From my own code:
TreeNode lastDragDestination = null;
DateTime lastDragDestinationTime;
private void tvManager_DragOver(object sender, DragEventArgs e)
{
IconObject dragDropObject = null;
TreeNode dragDropNode = null;
//always disallow by default
e.Effect = DragDropEffects.None;
//make sure we have data to transfer
if (e.Data.GetDataPresent(typeof(TreeNode)))
{
dragDropNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
dragDropObject = (IconObject)dragDropNode.Tag;
}
else if (e.Data.GetDataPresent(typeof(ListViewItem)))
{
ListViewItem temp (ListViewItem)e.Data.GetData(typeof(ListViewItem));
dragDropObject = (IconObject)temp.Tag;
}
if (dragDropObject != null)
{
TreeNode destinationNode = null;
//get current location
Point pt = new Point(e.X, e.Y);
pt = tvManager.PointToClient(pt);
destinationNode = tvManager.GetNodeAt(pt);
if (destinationNode == null)
{
return;
}
//if we are on a new object, reset our timer
//otherwise check to see if enough time has passed and expand the destination node
if (destinationNode != lastDragDestination)
{
lastDragDestination = destinationNode;
lastDragDestinationTime = DateTime.Now;
}
else
{
TimeSpan hoverTime = DateTime.Now.Subtract(lastDragDestinationTime);
if (hoverTime.TotalSeconds > 2)
{
destinationNode.Expand();
}
}
}
}
EDIT
I have a new solution, a bit far-fetched, but it works... It uses a DelayedAction class to handle delayed execution of an action on the main thread :
DelayedAction<T>
public class DelayedAction<T>
{
private SynchronizationContext _syncContext;
private Action<T> _action;
private int _delay;
private Thread _thread;
public DelayedAction(Action<T> action)
: this(action, 0)
{
}
public DelayedAction(Action<T> action, int delay)
{
_action = action;
_delay = delay;
_syncContext = SynchronizationContext.Current;
}
public void RunAfterDelay()
{
RunAfterDelay(_delay, default(T));
}
public void RunAfterDelay(T param)
{
RunAfterDelay(_delay, param);
}
public void RunAfterDelay(int delay)
{
RunAfterDelay(delay, default(T));
}
public void RunAfterDelay(int delay, T param)
{
Cancel();
InitThread(delay, param);
_thread.Start();
}
public void Cancel()
{
if (_thread != null && _thread.IsAlive)
{
_thread.Abort();
}
_thread = null;
}
private void InitThread(int delay, T param)
{
ThreadStart ts =
() =>
{
Thread.Sleep(delay);
_syncContext.Send(
(state) =>
{
_action((T)state);
},
param);
};
_thread = new Thread(ts);
}
}
AutoExpandTreeView
public class AutoExpandTreeView : TreeView
{
DelayedAction<TreeNode> _expandNode;
public AutoExpandTreeView()
{
_expandNode = new DelayedAction<TreeNode>((node) => node.Expand(), 500);
}
private TreeNode _prevNode;
protected override void OnDragOver(DragEventArgs e)
{
Point clientPos = PointToClient(new Point(e.X, e.Y));
TreeViewHitTestInfo hti = HitTest(clientPos);
if (hti.Node != null && hti.Node != _prevNode)
{
_prevNode = hti.Node;
_expandNode.RunAfterDelay(hti.Node);
}
base.OnDragOver(e);
}
}

Categories