Panel Events not firing? (C#) - winforms

I have a Form and I have some defined methods that are different from the standard events provided by winforms. I have a panel overlaying my form and I want to add custom mouse_up and mouse_down events.
What I've tried (which works for the form but not for the outer panel) is:
public Form1()
{
InitializeComponent();
OuterPanel.MouseUp += delegate(object sender, MouseEventArgs e) { OnMouseEvent_UpDown(sender, e, "custom param"); };
OuterPanel.MouseDown += delegate(object sender, MouseEventArgs e) { OnMouseEvent_UpDown(sender, e, "custom param"); };
}
where OuterPanel is the name of the panel which overlays my main form. When I switch "OuterPanel.event += OnEvent(args)" to "this.event += OnEvent(args)" and remove the panel entirely, I get the functionality that I want.

Related

Programatically fire Treview item selction event in wpf

I'm working on WPF project where I populate a tree view (MainWindow.xaml)using a Class called SampleTreeItem (SampleTreeView class). This tree view shows files from the selected folder. when user clicks the tree item (mainWindow.cs), the code in the background opens the file, serializes it and extract the data from it.
Now I'm adding a checkbox to this tree through the class using header template.
There is event called OnItemSelected (in Mainwindow.cs) which gets fired when I select item from tree view.
I'm trying to handle the click event of the checkbox in Sampletree.cs and I want to fire OnItemSelected from mainwindow.cs when user clicks on the check box.
MainWindow.xaml.cs
private void filesTreeView_OnItemSelected(object sender, RoutedEventArgs e)
{
SampleFileTreeItem treeViewItem = e.Source as SampleFileTreeItem;
if (treeViewItem == null)
return;
OpenSampleFile(treeViewItem.FilePath);
}
This is the click event in the SampleTree.cs file.
public void CompareSamples_Click(object sender, RoutedEventArgs e)
{
//RoutedEvent routedEvent = e.RoutedEvent;
//e.Handled= true;
//e.Source = this;
//RoutedEventArgs args = new RoutedEventArgs(routedEvent, e);
RoutedEvent routedEvent = TreeView.SelectedItemChangedEvent;
e.Source = this;
RoutedEventArgs args = new RoutedEventArgs(routedEvent, e);
if (compareSamplesCheckbox.IsChecked == true)
{
OnSelected(args);
}
else
{
//object sender, RoutedEventArgs e
}
}
I don't know what I'm missing here but I cant fire OnItemSelected (mainWinodw) from SampleTree.cs.
Let me know if anyone has any thoughts on it. Appreciate any help.
I assume, that OnSelected(..) is an event handler. So in your code you need not to call an event handler, but to raise an event for the UIElement:
public void CompareSamples_Click(object sender, RoutedEventArgs e)
{
RoutedEvent routedEvent = TreeView.SelectedItemChangedEvent;
e.Source = this;
RoutedPropertyChangedEventArgs<object> args = new RoutedPropertyChangedEventArgs<object>(oldVal, newVal, routedEvent);
if (compareSamplesCheckbox.IsChecked == true)
{
yourTreeView.RaiseEvent(args);
}
else
{
//object sender, RoutedEventArgs e
}
}

Adding tabpage to a tabcontrol which is on another form on click of button

I have a winform called form1 which has tabcontrol with some tabpages and one button on clicking of which I open another form called form2. I want to add tabpage to form1 tabcontrol on click of button which is present on form2.
Assumed that you have not created a tabcontrol at runtime in the Firstform.
In FirstForm.cs
private void button1_Click(object sender, EventArgs e)
{
SecondForm obj = new SecondForm(this);
obj.Show();
}
public void addTabpage()
{
TabPage tbpg = new TabPage();
tbpg.Text = "New tabpage";
tabControl1.TabPages.Add(tbpg);
this.BringToFront();
}
In SecondForm.cs, change the constructor as follows
static FirstForm objpub;
public SecondForm(FirstForm obj)
{
InitializeComponent();
objpub = obj;
}
private void button1_Click(object sender, EventArgs e)
{
if (objpub != null)
{
objpub.addTabpage();
}
}
If this answer does not meet your problem, then provide your code.
Cheers !

Change textblock text on click of rectangle using XAML triggers

void rectForeHead_MouseDown(object sender, MouseButtonEventArgs e)
{
txtLableBlock.Text += rectForeHead.ToolTip;
}
void rectChest_MouseDown(object sender, MouseButtonEventArgs e)
{
txtLableBlock.Text += rectChest.ToolTip;
}
I am changing textblock text property on click of number of rectangles.
How can I achieve the same with WPF Event triggers

Wpf, I need to hide and show a groupbox?

How I can show or hide a groupbox from xaml.cs. I try to do this in the checkbox's event:
private void cbDaily_Checked(object sender, RoutedEventArgs e)
{
gbCalendar.Visibility = Visibility.Visible;
}
But this don't work.
It must be work on checked/unchecked events of checkbox like this way :
private void chkTest_Checked(object sender, RoutedEventArgs e)
{
grpTest.Visibility = System.Windows.Visibility.Visible;
}
private void chkTest_Unchecked(object sender, RoutedEventArgs e)
{
grpTest.Visibility = System.Windows.Visibility.Hidden;
}
It is working fine in my sample application. Can you please give more details of your problem, so I can have better idea. Is event fired properly ? Make sure name of groupbox in code behind is correct or not ?

Focus the controls in a Tabcontrol

How can focus the controls when select a tabitem in a tabcontrol?
You should capture the Selection changed event of the TabControl and inside that you focus the control you need. Just as
private void TabControl1_SelectionChanged(object sender, EventArgs e)
{
control1.Focus();
}
Not sure I understand what you're asking completely, but you probably want to capture the SelectionChanged event for the TabControl:
public Window1()
{
InitializeComponent();
TabControl1.SelectionChanged += TabControl1_SelectionChanged;
}
private void TabControl1_SelectionChanged(object sender, EventArgs e)
{
TabItem item = (TabItem)TabControl1.SelectedItem;
// Find the first control in the TabItem content and focus it?
}

Resources