I'm looking for solution to add snapping/sticky windows functionallity (winamp-like) to existing WPF application. Same thing as it was asked here, just I need it for WPF.
It doesn't have to have docking function, just to snap to border of other windows inside same application and edge of screen (including taskbar) if possible. Preferably open source solution.
Thanks
Here is the Solution you actually asked for:
Let's say we have 2 Xaml windows named MainWindow and Window2:
MainWindow:
Window2 windows2;
public void RealodPos()
{
if (windows2 == null) { windows2 = new Window2(this); this.Top = 300; }
windows2.Top = this.Top;
windows2.Left = this.Width + this.Left - 15;
windows2.Show();
}
private void Window_Activated(object sender, EventArgs e)
{
RealodPos();
}
private void SizeChenged(object sender, SizeChangedEventArgs e)
{
RealodPos();
}
private void LocationChange(object sender, EventArgs e)
{
RealodPos();
}
Window2:
public partial class Window2 : Window
{
MainWindow Firstwin;
public Window2(MainWindow FirstWindow)
{
InitializeComponent();
Firstwin = FirstWindow;
}
void RealodPos()
{
this.Top = Firstwin.Top;
this.Left = Firstwin.Width + Firstwin.Left - 15;
}
private void Window_Activated(object sender, EventArgs e)
{
RealodPos();
}
private void Window_LocationChanged(object sender, EventArgs e)
{
RealodPos();
}
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
RealodPos();
}
}
My suggestion as a Software engineer:
Hint1: I don't know where you will use this but it's better to convert it to a reusable component that is not hardcoded with only 2 windows.
Hint2: Convert the
public Window2(MainWindow FirstWindow)
's MainWindow argument to a Window class formant to have a more flexible pointer for reusing it in the other applications.
Here is my suggested Solution for pro-WPF developers:
instead of doing this in that way you can make your own customized windows on XAML and use UserControls instead of other windows that you need.
Thanks for reading, please ask me if you want anything else or if you need the code as a project file.
The WPF Docking Library may provide some of what you are looking for, but I'm unsure if it works on the entire screen or just on your application window.
Related
I have an application that sends information from two forms (Form 2 to Form 1) and I have a menu screen that enters Form 2 but when I try to open form two I get the error (CS7036). Basically, I have the error that says
"Error CS7036 There is no argument given that corresponds to the
required formal parameter 'incoming' of 'difficulty.difficulty(Easy)"
(difficulty is "form 2" and easy is "form 1"
FROM Menu
private void btnStart_Click(object sender, EventArgs e)
{
this.Close();
difficulty diff = new difficulty();
diff.ShowDialog();
}
FROM DIFFICULTY
public partial class difficulty : Form
{
public difficulty(Easy incoming)
{
InitializeComponent();
}
private void btnEasy_Click(object sender, EventArgs e)
{
this.Close();
Easy easy = new Easy();
easy.ShowDialog();
}
}
How do I make it work so I can enter the menu then difficulty then easy without any errors? Any help appreciated.
You haven't passed in the parameter you added to your constructor while instantiating the class.
Here's a fixed version of your code:
private void btnStart_Click(object sender, EventArgs e) {
//use whatever easy you have here
Easy easy = new Easy();
difficulty diff = new difficulty(easy);
diff.ShowDialog();
this.Hide();
}
You need to have an Easy class created beforehand to pass in. And if you want to use that instance in the class, you can do this:
public partial class difficulty : Form {
private Easy easy;
public difficulty(Easy incoming)
{
easy = incoming;
InitializeComponent();
}
private void btnEasy_Click(object sender, EventArgs e) {
....
}
}
Then in any of the class functions, reference the easy variable to get data.
I am building a simple web browser in C#. In order to have all of my buttons such as the go, forward, back, refresh button, along with textbox input in a single tab, I have decided to put a tool strip and web browser control in a single user control that I created. This will enable me to just drop 1 control into a tab. Unfortunately when I try to use my user control it does not work. I know that my code inside the user control is fine, because when I had it in my main form, it functioned properly. I think the main piece that I am missing is I do not understand how to properly call the user control from the main form. Can someone guide me in the right direction here?
The main form.
namespace WebBrowser.UI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("random text.");
}
}
}
And the User Control
namespace WebBrowser.UI
{
public partial class adkinsBrowser : UserControl
{
public adkinsBrowser()
{
InitializeComponent();
}
private void toolStripTextBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
webBrowser1.Navigate(toolStripTextBox1.Text.ToString());
}
}
private void toolStripTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
}
}
private void toolStripButton5_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(toolStripTextBox1.Text.ToString());
}
}
}
I am trying to create a sample for showing System Tray Notifications in a simple windows forms application. Basically, it looks like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void showToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Show();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Form1_Move(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide();
notifyIcon1.ShowBalloonTip(1000, "Important Notice", "Something important has come up. Click to view more", ToolTipIcon.Info);
}
}
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
this.Show();
}
}
After execution and minimizing, I can't find the icon on the right side in notification window.
Can you help me in order to visualize where I am wrong?
Thank you!
You have to set an icon manually to your NotifyIcon, otherwise it will not be shown (usually we think its default icon will be enough but unfortunately it is not!!)
Can someone please illustrate for me how to set up a logic like this:
I have a WPF Control. When a button is pressed it does one of the two possible things.
A. It checks if a different WPF Window has been loaded. If it was, it triggers that window's Print method.
B. It checks if a different WPF Window has been loaded. If it was not, it instantiates that window and then triggers its Print method.
I struggle to understand the events system between two WPF Controls/Windows. It's a relatively new thing for me, so I would appreciate if someone walked me through this.
Ps. This is not a homework assignment, but rather a new hobby of mine. If its a totally noob question then just point me to a resource so I can educate myself.
Cheers!
First of all, what is the way by which you will check if new Window opened is what you need it to be ?
You might do this by comparing their Handle or their Type (public class MyWindowWithPrintMethod : Window).
There can be multiple ways of doing this.
I suggest my simple way, focusing on the WPF way, to solve your purpose in easiest way possible.
MyWindowWithPrintMethod obj_MyWindowWithPrintMethod;
private void btnNewWindow_Click(object sender, RoutedEventArgs e)
{
obj_MyWindowWithPrintMethod = new MyWindowWithPrintMethod();
obj_MyWindowWithPrintMethod.Show();
}
private void btnCheckNewWindow_Click(object sender, RoutedEventArgs e)
{
WindowInteropHelper tgtWindow = new WindowInteropHelper(obj_MyWindowWithPrintMethod);
foreach (Window w in Application.Current.Windows)
{
// Compare Handle
WindowInteropHelper wih = new WindowInteropHelper(w);
if (wih.Handle == tgtWindow.Handle)
{
((MyWindowWithPrintMethod)w).Print();
}
// Compare Type
if (w.GetType() == typeof(MyWindowWithPrintMethod))
{
((MyWindowWithPrintMethod)w).Print();
}
}
}
MyWindowWithPrintMethod.cs
public class MyWindowWithPrintMethod : Window
{
public void Print()
{
MessageBox.Show("Print invoked !");
}
}
This answer from this question about events from 2 windows may help:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Child childWindow = new Child();
childWindow.MyEvent += new EventHandler(childWindow_MyEvent);
childWindow.ShowDialog();
}
void childWindow_MyEvent(object sender, EventArgs e)
{
// handle event
MessageBox.Show("Handle");
}
}
Child window
public partial class Child : Window
{
// define event
public event EventHandler MyEvent;
protected void OnMyEvent()
{
if (this.MyEvent != null)
this.MyEvent(this, EventArgs.Empty);
}
public Child()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Child_Loaded);
}
void Child_Loaded(object sender, RoutedEventArgs e)
{
// call event
this.OnMyEvent();
}
}
The above code shows how to set up an event from one window to another. But, you might want to simply call a method in that other window instead. For example:
public void AddNewUser()
{
Window2 window = new Window2();
if (window.ShowDialog() == true)
{
// Update DataGrid
RefreshDataGrid();
}
}
If you are determined to stick with events, then you should read up on WPF routed events.
I am have trouble using multiple windows in WPF and switching between them using a button. In theory my application should have 2 buttons, one forward and one back each on respectively changing the window to the previous and next window.
Unfortunately I get a Stackoverflow error, through my research I feel that it has something to do with me creating new windows that are creating the window again when the previous window is created, thus making a horrible loop. However I don't know where I can put the window creation code to stop this problem or if there are other ways to fix this.
Here is code for my windows:
First Window
public partial class Presenation_1 : Window
{
Presentation_2 p2 = new Presentation_2();
MainWindow M = new MainWindow();
public Presenation_1()
{
InitializeComponent();
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
this.Close();
p2.Show();
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
this.Close();
M.Show();
}
}
Second Window
public partial class Presentation_2 : Window
{
Presentation_3 p3 = new Presentation_3();
Presenation_1 p1 = new Presenation_1();
public Presentation_2()
{
InitializeComponent();
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
this.Close();
p3.Show();
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
this.Close();
p1.Show();
}
}
Third Window
public partial class Presentation_3 : Window
{
Presentation_4 p4 = new Presentation_4();
Presentation_2 p2 = new Presentation_2();
public Presentation_3()
{
InitializeComponent();
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
this.Close();
p4.Show();
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
this.Close();
p2.Show();
}
}
Fourth Window
public partial class Presentation_4 : Window
{
Presentation_3 p3 = new Presentation_3();
MainWindow M = new MainWindow();
public Presentation_4()
{
InitializeComponent();
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
this.Close();
M.Show();
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
this.Close();
p3.Show();
}
}
Thanks in advance
Don't create your Windows before the button is clicked, you can create them in the event handler:
private void btnForward_Click(object sender, RoutedEventArgs e)
{
var p2 = new Presentation_2();
this.Close();
p2.Show();
}
When you create a windows, you create 2 other windows with
new Presentation_X()
This new windows is automaticaly show and itself open 2 other windows.
You can create this windows once in the Mainwindow (auto hide this one), pass the reference in constructor and not close these windows. Quick example (not tested) :
public partial class Presenation_X : Window
{
private Window preview;
private Window next;
public Presenation_X(Window w1, Window w2)
{
this.preview = w1;
this.next = w2;
InitializeComponent();
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
this.next.Show();
this.Hide();
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
this.preview.Show();
this.Hide();
}
}