I've a C# WPF application which show uses Grid control in the xaml(P screen).For every row in the grid, I've a column called Details.Clicking on item in this column shows a pop-up windows which also has a grid in the xaml(C screen).
My item click event in the P's viewmodel has the following code:
var myChildWindow = new MyGridView();
myChildWindow.Show();
If the user clicks on the item multiple times, I just want to highlight the existing C pop-up window.If there's no existing windows open, then only I want to open a new windows.
I've worked on a similar requirement for Winforms applicaiton.How do I go about this for a WPF application please?
Thanks.
First you'd need to declare myChildWindow outside of the click event so that it is accessible from multiple events. So,
MyGridView myChildWindow;
goes outside the click event, probably as a private variable.
Then, in your click event see if it's null, and if it is, create it.
if (myChildWindow == null)
{
myChildWindow = new MyGridView();
myChildWindow.Show();
}
You could keep a reference to the window and get rid of this when the window is closed:
MyGridView myChildWindow;
private void Button_Click(object sender, RoutedEventArgs e)
{
if (myChildWindow == null)
{
myChildWindow = new MyGridView();
myChildWindow.Closed += MyChildWindow_Closed;
myChildWindow.Show();
}
else
{
myChildWindow.Activate();
}
}
private void MyChildWindow_Closed(object sender, EventArgs e)
{
myChildWindow.Closed -= MyChildWindow_Closed;
myChildWindow = null;
}
Related
Inside my Canvas I have a ContextMenu with the expected functionality of closing when you select an option in the menu, or when you click outside of it. However I want my program to respond differently based on which of those methods was used to close the ContextMenu. From what I can tell the Closed Event on the ContextMenu fires in both scenarios. Is there any way to make that distinction?
Could you please explain more about problem?
I think it's obvious that you have to scenario. 1: click on ContexMenu options you created which will invoke an event fore sure and clicking outside it which will cancel the context Menu. by the way I made an answer base on my understanding of you problem. If I got it wrong please tell me.
The bellow code is Just a demo code , The logic Behind it is important.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
// Windows Load event
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
// Very Important Because by default it's null
this.ContextMenu = new ContextMenu();
// Making 3 sample Menu Item for ContextMenu
MenuItem firstMenuItem = new MenuItem()
{
Header = "FirsMenu"
};
//1st of three way to give event to Controls.
// Giving click Event to firstMenuItem to seprate it's click behavior from Other Menu Items
firstMenuItem.Click += (s, e) =>
{
// if Tag be Null on context Menu closing it's get that non of item selected so the click must be out side or lost focused
this.ContextMenu.Tag = 1;
MessageBox.Show("First Menu Clicked");
};
MenuItem secondMenuItem = new MenuItem()
{
Header = "SecondMenu"
};
//2nd of three way to give event to Controls.
// Giving click Event to secondMenuItem to seprate it's click behavior from Other Menu Items
secondMenuItem.Click += delegate
{
// if Tag be Null on context Menu closing it's get that non of item selected so the click must be out side or lost focused
this.ContextMenu.Tag = 1;
MessageBox.Show("Second Menu Clicked");
};
MenuItem thirdMenuItem = new MenuItem()
{
Header = "ThirdMenu"
};
//3rd of three way to give event to Controls.
// Giving click Event to thirdMenuItem to seprate it's click behavior from Other Menu Items
thirdMenuItem.Click += ThirdMenuOnClick;
this.ContextMenu.Items.Add(firstMenuItem);
this.ContextMenu.Items.Add(secondMenuItem);
this.ContextMenu.Items.Add(thirdMenuItem);
this.ContextMenu.Closed += ContextMenuOnClosed;
}
private void ThirdMenuOnClick(object sender, RoutedEventArgs e)
{
// if Tag be Null on context Menu closing it's get that non of item selected so the click must be out side or lost focused
this.ContextMenu.Tag = 1;
MessageBox.Show("Third Menu Clicked");
}
// Event for opening contextmenu on right mouse button click
private void MainWindow_OnMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
this.ContextMenu.IsOpen = true;
}
private void ContextMenuOnClosed(object sender, RoutedEventArgs e)
{
// if null means click must be out side or lost focused
if (((ContextMenu)sender).Tag == null)
{
MessageBox.Show("You Clicked OutSide");
}
// very Importnt code , because it will reset the context menu tag logically
((ContextMenu)sender).Tag = null;
}
}
Wish You bests, Heydar.
I am developing a form with multiple options that simulates a signup form, and I want to display some tips and descriptions in a RichTextBox located by the options when the user's mouse hover by it's GroupBoxes.
Since I am fairly new to programming, I don't know if getting all the controls names one by one is the optimal, so I want to grab the controls' names inside of the tabControl control that I am using to organize everything.
private void TabControl1_MouseHover(object sender, EventArgs e)
{
foreach(Control c in this.Controls)
{
string name = c.Name;
TooltipText(name);
}
}
And I also have a method where I will write the text that will be displayed in the RichTextBox.
private string TooltipText(string name)
{
if(name == "Name:")
{
return "blabla";
}
else
{
return "none";
}
}
I've tried a generic method to show a message box if the control was detected and, as I suspected, nothing showed up:
private void TooltipText(string name)
{
if(name == "LBL_Name")
{
MessageBox.Show("hey");
return;
}
}
How can I properly detect the Groupboxes or other types of Controls inside of the TabControl control, and also display the text in the box beside it?
You don't have to create your own Tool Tips. The .net WinForms provides a ToolTip class. https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.tooltip?view=netframework-4.8
I added 2 radio buttons to a group box in design view.
Try it and see.
private void Form1_Load(object sender, EventArgs e)
{
ToolTip tip = new ToolTip();
tip.AutoPopDelay = 5000;
tip.InitialDelay = 1000;
tip.ReshowDelay = 500;
tip.SetToolTip(radioButton1, "Choose to Add Onions");
tip.SetToolTip(radioButton2, "Choose to Add Pickles");
}
In my lightswitch app i need to add a small image control to every row, when clicking on it , it should send you to a detail screen. I made the silverlight control and added it to the lightswitch app.
partial void VidContentItemRessources_Loaded(bool succeeded) {
int index = 0;
foreach (VidContentItemRessourcesItem cust in this.VidContentItemRessources) {
this.FindControlInCollection("ImageLinkIcon", this.VidContentItemRessources.ElementAt(index))
.ControlAvailable += (s, e) => {
MyLinkImage = (e.Control as LinkImage);
MyLinkImage.MouseLeftButtonDown += MyLinkImage_MouseLeftButtonDown;
};
index++;
}
}
But if i do this i get an error that the event control available can't be used on controls in collection ..
So i tried to do it like this
LinkImage neco = this.FindControlInCollection("ImageLinkIcon", this.VidContentItemRessources.ElementAt(index)) as LinkImage;
neco.MouseLeftButtonDown += MyLinkImage_MouseLeftButtonDown;
But then neco is allways null because it is trying to load the control before it's available... any solution pls ?
These types of initialisations should go in the screen's Created method, that way you're guaranteed that all of the controls on the screen have been created. If any of the screen's methods (like InitializeDataWorkspace, Created, Saving etc) aren't in your code file, you can create them by using the Write Code dropdown box in the screen editor.
I solved it like this:
I catching click event in silverlight control than i invoking method in lightsiwtch
public void MouseClick(object sender, MouseButtonEventArgs e) {
var objDataContext = (IContentItem)this.DataContext;
var Screen = (Microsoft.LightSwitch.Client.IScreenObject)objDataContext.Screen;
Screen.Details.Dispatcher.BeginInvoke(() => {
Screen.Details.Methods["DoImageLinkEvent"]
.CreateInvocation(null).Execute();
});
}
I have a Windows form named Form1 and panel within this form named panel1. I use the panel only to place buttons there so that I can group them and work with them separately from the other buttons in my Form1. For the purpose of my program I need to handle every button click made from the buttons inside panel1. For this purpose I use the same code snippet:
public Form1()
{
InitializeComponent();
// Set a click event handler for the button in the panel
foreach (var button in panel1.Controls.OfType<Button>())
{
button.Click += HandleClick;
}
}
What I need to do is to have a way to identify which button exactly has been clicked. For this purpose I played a little bit with my handler method:
private void HandleClick(object o, EventArgs e)
{
MessageBox.Show("HI" + o.ToString());
}
which gave me some hope because I get this:
It's the second part - Text: button4 which is actually enough information to continue with my work. But I can't find a way to get this piece of information without some complicated string manipulations. So is there a way to get this or other unique information about the button been clicked given the way I have written my code?
private void HandleClick(object sender, EventArgs e)
{
var btn = sender as Button;
if (btn != null)
{
MessageBox.Show(btn.Text);
}
}
One option is to cast the object to a Button, but rather than doing the casting you can change how the event handler is assigned so that you don't need to cast in the first place:
foreach (var button in panel1.Controls.OfType<Button>())
{
button.Click += (_,args)=> HandleClick(button, args);
}
Then just change the signature of HandleClick to:
private void HandleClick(Button button, EventArgs e);
You need to cast sender to the Button class so you can access its properties:
Button b = (Button)sender;
MessageBox.Show(b.Text);
In my WPF application, I have a main window (Branch.xaml) which has a button that will open an other window (Location.xaml). Once this Location window is open, how do I prevent another instance of this Location window from opening, when the user clicks the same button again?
Or how can I re-focus the same open window, when the user clicks the button again?
The button click code is auto-generated code when you double-click on a button in xaml.
In the "Branch.xaml.cs" file, the code for button click is as follows:
private void rbtn_Location_Click(object sender, RoutedEventArgs e)
{
Location location = new Location();
location.Show();
}
Location is a custom class which opens a window with 3 list boxes
Thanks, any help is appreciated.
I'm using WPF application on C# 4.0 & Visual Studio 2010.
You could create a field in your main window which holds a reference to the location window if there is any, in the button click handler check if the field is null and if it is create a new window and store it in the field, if not call Activate on the window in the field. You also would have to subscribe to the Closed event of the location window to clear out the reference again when the location window is gone.
Edit: Concrete example:
private LocationWindow locationWindow;
private void Button1_Click(object sender, RoutedEventArgs e)
{
if (locationWindow == null)
{
locationWindow = new LocationWindow();
locationWindow.Closed += (s, _) => locationWindow = null;
locationWindow.Show();
}
else
{
locationWindow.Activate();
}
}
Application.Current.Windows collection holds reference for all windows for current AppDomain. You can check for your window in that collection and if it founds your window then call Activate for that window else create new Window. This will get you going -
private void rbtn_Location_Click(object sender, RoutedEventArgs e)
{
Window window = Application.Current.Windows.OfType<Window>().Where(win => win.Name == "LocationWindow").FirstOrDefault();
if(window == null)
{
Location location = new Location();
location.Show();
}
else
{
window.Activate();
}
}
Make sure you provide your window a x:Name as LocationWindow to make it work.
<Window x:Name="LocationWindow">
</Window>
Also include namespace System.Linq in your code behind.