I've been breaking my head over a problem I have (I just started coding in C#)
I have a Main Screen (Hereafter reffered to as Form 1) and a Video Options Form (hereafter reffered to as Form 2) (Picture Included). Now, when f.e. I change the Radiobuttons in Form 2 to "Windowed" and Select a Resolution, I want some options to change in Form 1.
This is as far as I got so far, it goes to the Form 1 code, but says I can't change anything there.
Code Snippet Main Form
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void btnVideo_Click(object sender, EventArgs e)
{
Visual_Options options = new Visual_Options();
options.Show();
}
Code Snippet Options Form
public partial class Visual_Options : Form
{
frmMain Main;
public Visual_Options()
{
InitializeComponent();
}
private void Visual_Options_Load(object sender, EventArgs e)
{
switch (Main.FormBorderStyle) //Check the Borderstyle of frmMain with Switch to determine current state
{
case FormBorderStyle.None: // if BorderStyle of frmMain = "None"
if (Main.WindowState == FormWindowState.Maximized) //Check if frmMain = Maximizes
{
rbFullscrn.Checked = true;
}
else
{
rbBorderless.Checked = true;
};
break;
case FormBorderStyle.Fixed3D:
rbWindow.Checked = true;
break;
}
switch (Main.Width) //Check Width to determine current value
{
case 800:
rb8x6.Checked = true;
break;
case 1024:
rb10x7.Checked = true;
break;
case 1280:
rb12x7.Checked = true;
break;
}
}
private void btnAccept_Click(object sender, EventArgs e)
{
if (rbFullscrn.Checked == true)
{
Main.WindowState = FormWindowState.Maximized;
Main.FormBorderStyle = FormBorderStyle.None;
}
else if (rbBorderless.Checked == true && rb8x6.Checked == true)
{
Main.WindowState = FormWindowState.Normal;
Main.FormBorderStyle = FormBorderStyle.None;
Main.Height = 600;
Main.Width = 800;
}
else if (rbBorderless.Checked == true && rb10x7.Checked == true)
{
Main.WindowState = FormWindowState.Normal;
Main.FormBorderStyle = FormBorderStyle.None;
Main.Height = 768;
Main.Width = 1024;
}
else if (rbBorderless.Checked == true && rb12x7.Checked == true)
{
Main.WindowState = FormWindowState.Normal;
Main.FormBorderStyle = FormBorderStyle.None;
Main.Height = 720;
Main.Width = 1280;
}
else if (rbWindow.Checked == true && rb8x6.Checked == true)
{
Main.WindowState = FormWindowState.Normal;
Main.FormBorderStyle = FormBorderStyle.Fixed3D;
Main.Height = 600;
Main.Width = 800;
}
else if (rbWindow.Checked == true && rb10x7.Checked == true)
{
Main.WindowState = FormWindowState.Normal;
Main.FormBorderStyle = FormBorderStyle.Fixed3D;
Main.Height = 768;
Main.Width = 1024;
}
else if (rbWindow.Checked == true && rb12x7.Checked == true)
{
Main.WindowState = FormWindowState.Normal;
Main.FormBorderStyle = FormBorderStyle.Fixed3D;
Main.Height = 720;
Main.Width = 1280;
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
I've looked on the internet for a bit now, and, I found a few solutions, but, from the few solutions I found, none seemed to work so far.
Ty in advance,
Me ^_^
If you use ShowDialog, it will open your options Form as a Modal Dialog, that way once you make changes you can check the DialogResult and read a Public Property from your Option Form in your Main Form, using it to set your size. Otherwise you could subscribe to the Options Form's Closed Event and use that event to set your Forms Size.
Fist Option:
private void button1_Click(object sender, EventArgs e)
{
Visual_Options options = new Visual_Options();
if ( options.ShowDialog() == DialogResult.OK)
this.Size = options.getFormSize; //This is a public property returning a size
}
2nd Option using same property:
private void button1_Click(object sender, EventArgs e)
{
Visual_Options options = new Visual_Options();
options.FormClosed+=new FormClosedEventHandler(options_FormClosed);
options.Show();
}
void options_FormClosed(object sender, FormClosedEventArgs e)
{
this.Size = ((Visual_Options)sender).getFormSize;
((Visual_Options)sender).FormClosed -= new FormClosedEventHandler(options_FormClosed); //Remove handler to prevent leaks
}
You might want to do this:
options.Show(this); passing the this reference will make "options" form as the child of your mainform.
Then frmMain Main can be set as:
frmMain Main = (frmMain)this.Owner;
After this, you can alter any and all properties of your main form.
Related
I'm try make program to open other apps, to open this program i wan't use keybinds and for hide too.
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (Dark == 0)
{
if (e.Key == Key.LeftCtrl)
{
if (e.Key == Key.LWin)
{
this.Topmost = true;
Dark = 1;
}
}
}
else if(Dark == 1)
{
if (e.Key == Key.LeftCtrl)
{
if (e.Key == Key.LWin)
{
this.Topmost = false;
Dark = 0;
WindowState = WindowState.Minimized;
}
}
else if (e.Key == Key.Escape)
{
this.Topmost = false;
Dark = 0;
WindowState = WindowState.Minimized;
}
}
Outside the program, keybinds do not work at all, but if the program is open, then they work, why?
you can refer to my class, used to global hook key event
https://github.com/nhochjkaru/JEOrbwalk/blob/master/UserActivityHook.cs
declare: UserActivityHook actHook;
Main function:
actHook = new UserActivityHook();
actHook.KeyDown += new KeyEventHandler(Window_KeyDown);
I have a WPF custom control and I needs to open it from WinForm. I have followed all steps mentioned in http://weblogs.asp.net/jdanforth/open-a-wpf-window-from-winforms and Open WPF window in WindowsForm APP
But still it gives me an object reference not set to an instance of exceptions.
Winform:
private void button1_Click(object sender, EventArgs e)
{
var notificatioinapp = new WpfCustomControlLibrary1.Window1();
ElementHost.EnableModelessKeyboardInterop(notificatioinapp);
notificatioinapp.Show();
}
WPF custom control:
public partial class Window1 : Window
{
public Window1() : base()
{
InitializeComponent();
this.Closed += this.NotificationWindowClosed;
}
public new void Show()
{
this.Topmost = true;
base.Show();
this.Owner = System.Windows.Application.Current.MainWindow;
this.Closed += this.NotificationWindowClosed;
var workingArea = Screen.PrimaryScreen.WorkingArea;
this.Left = workingArea.Right - this.ActualWidth;
double top = workingArea.Bottom - this.ActualHeight;
foreach (Window window in System.Windows.Application.Current.Windows)
{
string windowName = window.GetType().Name;
if (windowName.Equals("NotificationWindow") && window != this)
{
window.Topmost = true;
top = window.Top - window.ActualHeight;
}
}
this.Top = top;
}
private void ImageMouseUp(object sender,
System.Windows.Input.MouseButtonEventArgs e)
{
this.Close();
}
private void DoubleAnimationCompleted(object sender, EventArgs e)
{
if (!this.IsMouseOver)
{
this.Close();
}
}
private void NotificationWindowClosed(object sender, EventArgs e)
{
foreach (Window window in System.Windows.Application.Current.Windows)
{
string windowName = window.GetType().Name;
if (windowName.Equals("NotificationWindow") && window != this)
{
// Adjust any windows that were above this one to drop down
if (window.Top < this.Top)
{
window.Top = window.Top + this.ActualHeight;
}
}
}
}
}
Appreciate any support.
Application.Current is Specific for WPF Application actually. So I think since you are trying to open WPF application from WinForms Application you need to initialize instance of WPF Application first before accessing it.
if ( null == System.Windows.Application.Current )
{
new System.Windows.Application();
}
If this doesn't work try setting Application.Current.MainWindow = this; in loaded event of WPF window.
This should do the fix.
EDIT :
private void button1_Click(object sender, EventArgs e)
{
if (null == System.Windows.Application.Current)
{
new System.Windows.Application();
}
var wpfwindow = new Window();
wpfwindow = new WpfCustomControlLibrary1.Window1();
ElementHost.EnableModelessKeyboardInterop(wpfwindow);
wpfwindow.Show();
}
I have a border less form that I created my own buttons for window functions. I'm working with the maximize button. I got it to max fine but I need it to return to normal when clicked again.
What it does is it just flickers and doesn't maximize, if I remove the "if" statement it will maximize but I don't get the return to normal feature that I need.
I've tried:
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
if(this.WindowState == FormWindowState.Maximized)
{
this.WindowState = System.Windows.Forms.FormWindowState.Normal;
}
Is there something else I should be using? Any help is appreciated.
Cannot reproduce the behavior. I tried the following and works as expected.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.WindowState= FormWindowState.Maximized;
}
private void button1_Click(object sender, EventArgs e)
{
WindowState = WindowState == FormWindowState.Maximized
? FormWindowState.Normal
: FormWindowState.Maximized;
}
}
private void form_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (this.WindowState == FormWindowState.Normal)
{
this.WindowState = FormWindowState.Maximized;
}
else
{
this.WindowState = FormWindowState.Normal;
}
}
}
I've a ScreenLocker winform application. It's full-screen and transparent. It unlocks the screen when user presses Ctrl+Alt+Shift+P.
But I want it more dynamic. I would like to let a user set his own password in a config file.
For example, he set his password mypass. My problem is- how can I track whether he typed 'mypass' on that form?
I don't want to have any textbox or button on the form. Help please.
Here is my current code-
public frmMain()
{
InitializeComponent();
this.KeyPreview = true;
this.WindowState = FormWindowState.Normal;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Bounds = Screen.PrimaryScreen.Bounds;
this.ShowInTaskbar = false;
double OpacityValue = 4.0 / 100;
this.Opacity = OpacityValue;
}
private void OnKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.P && e.Modifiers == (Keys.Control | Keys.Shift | Keys.Alt))
{
this.Close();
}
}
You can store the typed letters in a variable, then check it on enter keydown event. Here is the working sample-
public partial class Form1 : Form
{
String pass = String.Empty;
public Form1()
{
InitializeComponent();
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
string value = e.KeyChar.ToString();
pass += value;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode==Keys.Enter)
{
//Now check the password with your config file.
//You may have to reset the variable if the pass does not match with the config.
}
}
}
I think the most elegant and robust solution is to use the reactive extension framework.
PM> Install-Package Rx-Main
Key pressed are stored in a buffered observable sequence. When the buffer matches the password an action is taken (in the example it's a message box)
private void Form1_Load(object sender, EventArgs e)
{
string password = "test";
var keypressed = Observable.FromEventPattern<KeyPressEventHandler, KeyPressEventArgs>(
handler => handler.Invoke,
h => this.KeyPress += h,
h => this.KeyPress -= h);
var keyDownSequence = keypressed.Select(p => p.EventArgs.KeyChar);
var checkPasswordSequence = from n in keyDownSequence.Buffer(password.Length, 1)
where string.Join("", n.ToArray()) == password
select n;
checkPasswordSequence.Subscribe(x => MessageBox.Show(string.Join("", x.ToArray())));
}
This is working
public partial class LockScreen : Form
{
Timer checkTimer;
string curPass = "";
string pass = "mypass"; // take it from your config file
public LockScreen()
{
InitializeComponent();
this.KeyPreview = true;
this.WindowState = FormWindowState.Normal;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Bounds = Screen.PrimaryScreen.Bounds;
this.ShowInTaskbar = false;
double OpacityValue = 4.0 / 100;
this.Opacity = OpacityValue;
// Add a keypress event
this.KeyPress += LockScreen_KeyPress;
checkTimer = new Timer();
checkTimer.Interval = 1000;
checkTimer.Tick += checkTimer_Tick;
}
void LockScreen_KeyPress(object sender, KeyPressEventArgs e)
{
checkTimer.Stop();
curPass += e.KeyChar.ToString();
if (curPass == pass)
this.Close();
else
checkTimer.Start();
}
void checkTimer_Tick(object sender, EventArgs e)
{
curPass = ""; //resets every second
}
}
private const string Password = "Foobar";
private readonly StringBuilder _builder = new StringBuilder();
private void LockForm_KeyPress(object sender, KeyPressEventArgs e)
{
//Check if the key pressed is a control character (e.g. alt/enter et cetera)
//and return if that is the case
//Also make sure to reset the stringbuilder's buffer now and then,
//if too much input is generated
_builder.Append(e.KeyChar);
if (_builder.ToString().EndsWith(Password))
{
//Correct password has been entered
}
}
I have a winform application using C#. I have tried everything to have a transition effect from form1 to form2.(opacity and height width change base on timer) But it seems that using parent-child container (MdiParent), it is not possible. Am I right? Or there are other way?
Here is my code..
public static System.Windows.Forms.Form gMdiForm;
public static Boolean fCheckFormNotUsed(string tMdiChildFormName)
{
foreach (System.Windows.Forms.Form childform in gMdiForm.MdiChildren)
{
if (childform.Name == tMdiChildFormName)
{
childform.Visible = true;
childform.Activate();
return false;
}
}
return true;
}
if (fCheckFormNotUsed("frm2"))
{
frm2 mMDIChild = new frm2();
mMDIChild.MdiParent = cls0G.gMdiForm;
mMDIChild.WindowState = FormWindowState.Maximized;
mMDIChild.Opacity = 0;
mMDIChild.Show();
}
Here is the code for timer in frm2
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Opacity <= 1.0)
{
this.Opacity += 0.07;
this.Height += 1;
this.Width += 3;
}
else
{
timer1.Stop();
}
}
Here is the load for form2
this.Opacity = 0;
timer1.Start();