Disable Copy/Paste/Cut in WPF VB.NET - wpf

I am trying to disable Copy/Paste/Cut feature in textbox in WPF VB.NET
This code works fine :
Private Sub textbox_PreviewExecuted(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
If e.Command Is ApplicationCommands.Copy OrElse e.Command Is ApplicationCommands.Cut OrElse e.Command Is ApplicationCommands.Paste Then
e.Handled = True
End If
End Sub
But disabling CTRL+C/CTRL+V using this code doesn't work :
DataObject.AddPastingHandler(control, AddressOf Me.OnCancelCommand)
DataObject.AddCopyingHandler(control, AddressOf Me.OnCancelCommand)
private void OnCancelCommand(Object sender, DataObjectEventArgs e)
e.CancelCommand()
How to make the 2nd code work ?

You can re-bind the commands. Here is a C# version, VB.Net should be similar
<TextBox>
<TextBox.CommandBindings>
<CommandBinding Command="ApplicationCommands.Copy" CanExecute="CommandBinding_CanExecute"/>
<CommandBinding Command="ApplicationCommands.Paste" CanExecute="CommandBinding_CanExecute"/>
</TextBox.CommandBindings>
</TextBox>
Handler:
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = false;
e.Handled = true;
}
Consider adding ApplicationCommands.Cut to the party, unless you have different plans for it.

Related

Close a window with an image on Visual Studio 2010

I want to have an image (or a button) that, when the user click on it, it closes the window, like ALT+F4.
Okay...this is an extremely simple operation.
For a Button, handle the Click() event and Close() the Form. You can simply double click the Button to have the method stub inserted for you. The exact same thing can be done with a PictureBox:
VB.Net:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Close()
End Sub
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
Me.Close()
End Sub
End Class
C#:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
this.Close();
}
}

How to redirect events from a WPF UserControl to WinForm?

I have a winform with a wpf usercontrol on it (ElementHost1). The usercontrol contains only a button. How can I know when the wpf button has been clicked in my winform? How can I "redirect" the events from wpf usercontrol to winform?
Thanks.
This link might be helpful to you.
Or a simple event handling in VB.NET
Public Event ClickMe()
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
RaiseEvent ClickMe()
End Sub
Then in your actual window you can have this:
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler SampleClick1.ClickMe, AddressOf Sample_Click
End Sub
Private Sub Sample_Click()
MessageBox.Show("This is a proof!")
End Sub
That SampleClick1 variable is from the designer code generated available to the form for your use.
Friend WithEvents ElementHost1 As System.Windows.Forms.Integration.ElementHost
Friend SampleClick1 As WindowsApplication1.SampleClick
Here is the one solution i found
in UserControl1.Xaml.cs
public static RoutedEvent ChkBoxChecked = EventManager.RegisterRoutedEvent("CbChecked", RoutingStrategy.Bubble,
typeof(RoutedEventHandler), typeof(CheckBox));
public event RoutedEventHandler CbChecked
{
add
{
AddHandler(ChkBoxChecked, value);
}
remove
{
RemoveHandler(ChkBoxChecked, value);
}
}
private void cbTreeView_Checked(object sender, RoutedEventArgs e)
{
RoutedEventArgs args = new RoutedEventArgs(ChkBoxChecked);
RaiseEvent(args);
}
Now in MainForm Form1 shown event we can add CbChecked event
private void Form1_Shown(object sender, EventArgs e)
{
this.elemetHost1.CbChecked += new System.Windows.RoutedEventHandler(wpfusercontrol_CbChecked);
//elementHost1 is the name of wpf usercontrol hosted in Winform
}
void elementHost1_CbChecked(object sender, System.Windows.RoutedEventArgs e)
{
//This event will raise when user clicks on chekbox
}
i am facing an issue here.i am rasing the same event in Form1 for all checkbox clickevents in UserControl1.so i want to know which checkbox is clicked in the mainform.i tried to see in the RoutedEventArgs e....but doesnt help
how to know which checkbox is clicked in the mainform

WPF How do I find which ListBox item was clicked

I have a WPF application in which there's a listbox filled with items of type 'Match'.
How do I make the button(contained within the item) actually select the item so that I might extract the value?
Here is my code: neither works since clicking the button doesn't actually select the item
private void LayButton_Click(object sender, RoutedEventArgs e)
{
var x = (Market)ListBoxSelectedMarket.SelectedItem;
var y = (sender as ListBoxItem);
}
Thanks
You should be able to use the DataContext from the clicked Button and get the ListBoxItem container from there, and then select it.
private void LayButton_Click(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
var dataContext = button.DataContext;
ListBoxItem clickedListBoxItem = ListBoxSelectedMarket.ItemContainerGenerator.ContainerFromItem(dataContext) as ListBoxItem;
clickedListBoxItem.IsSelected = true;
}
If you are binding to an object an alternative method could be (in VB)
This then gives you an instance of your object to play with and saves you having any mapping fields on the listbox
Private Sub OTC_Settled_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Dim pr_YourObject As New YourObject
Dim btn As Button = CType(sender, Button)
OTC = DirectCast(btn.DataContext, pr_YourObject)
End Sub
I haven't done much WPF programming, but you could try getting the parent of the button if it works the same as a WinForms container object.

Retrieve Value on Mouse Click for WPF Column Series

I have a simple chart with two column series containing all months in the year. I want to filter a list view that show detailed information for the selected month. I can capture the event via MouseDown on the ColumnSeries but I'm not sure how to get to the month in the column series.
<DVC:ColumnSeries Title=" Expenditures" IndependentValueBinding="{Binding Path=Month}"
DependentValueBinding="{Binding Path=Amt}"
ItemsSource="{Binding Path=ActivityExpenditureSeries}"
MouseDown="ColumnSeries_MouseDown" />
I'm sure I could do some fancy WPF databinding to the selected ColumnSeries for the listviews ItemsSource but this is where I'm heading:
Private Sub ColumnSeries_MouseDown(ByVal sender As System.Object,
ByVal e As System.Windows.Input.MouseButtonEventArgs)
' This is the functionality I'm looking for...
Dim selectedColumn As String
FilterListView(selectedColumn)
End Sub
Set the IsSelectionEnabled=True on the series and added a SelectionChanged event to the same series.
Private Sub colSeries_adjExpenditure_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
Dim cs As ColumnSeries = CType(sender, ColumnSeries)
Dim dp As MyDataPoint = CType(cs.SelectedItem, MyDataPoint)
End Sub
Set the IsSelectionEnabled=True on the series and added a SelectionChanged event to the same series.
System.Windows.Controls.DataVisualization.Charting.ColumnSeries cs = (System.Windows.Controls.DataVisualization.Charting.ColumnSeries)sender;
System.Data.DataRowView dp = (System.Data.DataRowView)cs.SelectedItem;
tbkName.Text = dp.Row[1].ToString();
tbkSalary.Text = dp.Row[0].ToString();
Example in C#:
Set the IsSelectionEnabled=True on the series and added a SelectionChanged event to the same series.
Name Space:
using System.Windows.Controls.DataVisualization.Charting;
Method:
private void ColumnSeries_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ColumnSeries cs = (ColumnSeries)sender;
KeyValuePair<string, int> kv = (KeyValuePair<string, int>)cs.SelectedItem;
Debug.WriteLine(kv.Key);
Debug.WriteLine(kv.Value);
}
[In C#]
Previous answers only allow clicking when selections are changed. Following code will enable clicking on columns independently of where you clicked earlier. It will also allow right clicking if needed (change event type)
<chartingToolkit:ColumnSeries DependentValuePath="Value" IndependentValuePath="Key" IsSelectionEnabled="True">
<chartingToolkit:ColumnSeries.DataPointStyle>
<Style TargetType="chartingToolkit:ColumnDataPoint">
<EventSetter Event="MouseLeftButtonUp" Handler="ColumnSeries_ColumnLeftClicked"/>
</Style>
</chartingToolkit:ColumnSeries.DataPointStyle>
</chartingToolkit:ColumnSeries>
private void ColumnSeries_ColumnLeftClicked(object sender, MouseButtonEventArgs e)
{
var key = ((ColumnDataPoint)sender).IndependentValue;
//etc
}

WPF DataContext syntax - Convert from C# to VB

Currently working through a Teach Yourself WPF tutorial. Usually I can mentally convert from C# to VB without any problem but this C# syntax is unfamiliar. How is it written in VB?
Private void Button_Click(object sender, RoutedEventArgs e)
{
((Person)DataContext).FirstName = "blah blah"
}
My usual fave online converters are choking on this ... perhaps because they don't do WPF?
try this
Private Sub Button_Click(ByVal sender As System.Object, ByVal e as RoutedEventArgs) Handles Button.Click
DirectCast(DataContext, Person).FirstName = "blah blah"
End Sub

Resources