how to make a web browser grid responsive c# windows forms - c#

i have been tasked to make a layout of 4 web browsers, that later could be used for security cameras.
the web browser part was easy, but i have been stuck at making it responsive, because when you run it and maximize the program, the resolution of the web browsers stay the same. and since this program will be running on a big flat screen it has to respond to the resolution.
i have looked all over the internet and have not found a solution. i have tried the anchoring but when i do this and i enlarge the program the browsers start over lapping each other. i have tried putting them in a flow grid and a table grid. other things i found was "this.AutoSize = true;" but i am kinda new to c# forms and do not understand this.
can anyone help?
the code and a few photo's of what happens
how it is now
what happens when enlarged
the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace webspace
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("www.nos.nl");
webBrowser2.Navigate("www.google.com");
webBrowser3.Navigate("www.facebook.com");
webBrowser4.Navigate("www.google.com/maps");
this.AutoSize = true;
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
this.Text = e.Url.ToString() + "is loading...";
}
private void webBrowser2_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
this.Text = e.Url.ToString() + "is loading...";
}
private void webBrowser3_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
this.Text = e.Url.ToString() + "is loading...";
}
private void webBrowser4_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
this.Text = e.Url.ToString() + "is loading...";
}
private void webBrowser2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
private void webBrowser3_DocumentCompleted_1(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
}
}

Have a layout that is two Orientation=Horizontal SplitContainers, inside another SplitContainer that is Orientation=Vertical (or vice versa, two verticals one on either side of a horizontal)
When the form resizes, set all the SplitContainer's SplitterDistances to 50% of the width/height as appropriate.. Unless the user's customised them (in which case decide what to do, like have a proportional resize, thereby allowing the user to have some views bigger than others)
Make the web browser controls Dock=Fill their panels
Note that the WebBrowser control is quite old now, and should probably be replaced with a WebView2 (Chrome-based Edge)

Related

c# Detect mouse clicks anywhere (Inside and Outside the Form)

Is this possible to detect a mouse click (Left/Right) anywhere (Inside and Outside the Form) in an if statement? And if it's possible, how?
if(MouseButtons.LeftButton == MouseButtonState.Pressed){
...
}
Here is a starter, if I understood your needs of "clicking from outside the window" and Hans Passant's suggestion doesn't fit your needs. You might need to add an event handler for Form1_Click.
CAUTION: This code is provided to illustrate the concept. The threading synchronization in this sample is not 100% correct. Check the history of this answer for an attempt at a more "threading correct" one that sometimes throws exceptions. As an alternative, to get rid of all threading issues, you could have the task in StartWaitingForClickFromOutside be instead always running (aka be always in "listen" mode) as opposed to trying to detect the "within the form" or "outside the form" states and starting/stopping the loop accordingly.
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.MouseLeave += Form1_MouseLeave;
this.Leave += Form1_Leave;
this.Deactivate += Form1_Deactivate;
this.MouseEnter += Form1_MouseEnter;
this.Activated += Form1_Activated;
this.Enter += Form1_Enter;
this.VisibleChanged += Form1_VisibleChanged;
}
private AutoResetEvent are = new AutoResetEvent(false);
// You could create just one handler, but this is to show what you need to link to
private void Form1_MouseLeave(object sender, EventArgs e) => StartWaitingForClickFromOutside();
private void Form1_Leave(object sender, EventArgs e) => StartWaitingForClickFromOutside();
private void Form1_Deactivate(object sender, EventArgs e) => StartWaitingForClickFromOutside();
private void StartWaitingForClickFromOutside()
{
are.Reset();
var ctx = new SynchronizationContext();
var task = Task.Run(() =>
{
while (true)
{
if (are.WaitOne(1)) break;
if (MouseButtons == MouseButtons.Left)
{
ctx.Send(CLickFromOutside, null);
// You might need to put in a delay here and not break depending on what you want to accomplish
break;
}
}
});
}
private void CLickFromOutside(object state) => MessageBox.Show("Clicked from outside of the window");
private void Form1_MouseEnter(object sender, EventArgs e) => are.Set();
private void Form1_Activated(object sender, EventArgs e) => are.Set();
private void Form1_Enter(object sender, EventArgs e) => are.Set();
private void Form1_VisibleChanged(object sender, EventArgs e)
{
if (Visible) are.Set();
else StartWaitingForClickFromOutside();
}
}
}
If I understood you incorrectly, you might find this useful: Pass click event of child control to the parent control
When user clicks outside the form control, it losses the focus and you can make use of that.which means you have to use the _Deactivate(object sender, EventArgs e) event of the form control to make this work. Since which will trigger when the form loses focus and is no longer the active form. Let Form1 be the form, then the event will be like the following:
private void Form1_Deactivate(object sender, EventArgs e)
{
// Your code here to handle this event
}
One method is to cover the entire screen with a borderless form with the properties set to transparent (a few percent above completely transparent, not sure if total transparency works but you won't notice the difference) and also set to topmost. Then use the events from the form. As soon as a click is detected this will not affect anything underneath the form (which in my application is something I want to happen) but the form could be closed and another mouse click simulated a fraction of a second later to activate the controls that are underneath. I had no problem using the windows API to use mouse hooks in VB6 but cannot seem to find something that works in c# with the 2019 version of .NET so this is a good workaround. Of course to be really clever you could use an irregular forms method to make the transparent form the same shape as the mouse and follow it.
Note: I have just found the complete code to do it using hooks that mere mortals can get up and running at once! KeyboardMouseHooks C# Library - CodePlex Archive
PS if you use my (dumb) method remember to create an escape key or button or you will have to restart your computer unless the form is programmed to disappear for real clicks as suggested!
I know this is late but maybe it helps someone. Using the MouseEventArgs of the MouseUp event of any control you can check for mouse button and wheel among other things. Here is an example.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.MouseUp += Form1_MouseUp;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
DoSomething_LeftClick();
}
else if(e.Button == MouseButtons.Right)
{
DoSomething_RightClick();
}
}
private void DoSomething_LeftClick()
{
//Here some code
}
private void DoSomething_RightClick()
{
//Here some code
}
}

C# Webbrowser will not fully load webpage

Was creating a browser and was testing how different websites load and noticed on some website the C# web browser will not fully load specific websites. Seems to default to mobile maybe?
For example, Apartmentbutler.com
Compared to full browser
Another example, cleancloudapp, gets stuck here.
For sake of details - very basic.
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load_1(object sender, EventArgs e)
{
webBrowser1.ScriptErrorsSuppressed = true;
}
private void button1_Click(object sender, EventArgs e)
{
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
string webPage = textBox1.Text.Trim();
webBrowser1.Navigate(webPage);
}
}
}
Is this something because of the webdeveloper? If it's because mobile, why not loading properly? Can I use useragent to force to desktop?
Would appreciate any help, thank you.
By default, the WebBrowser control runs in compatibility view mode. Compatibility view mode is basically document mode 7. That most likely caused the rendering issue.
You can change the IE version for webbrowser control by adding HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION\yourapp.exe and set its DWord value to e.g. 11001 (IE 11)
For 32 bits machine add under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION\
Details at https://msdn.microsoft.com/library/ee330730(v=vs.85).aspx#browser_emulation

mousewheel event not triggered after clicking off the canvas

thanks to http://www.eqqon.com/index.php/Piccolo_Snippets, i had mousewheel zooming working well until i added winform widgets to the form outside of the canvas; see pic of a test form below:
i found that if i clicked on button1, and moused back onto the canvas, i no longer get mousewheel events. Other mouse events (e.g. PNode entry/leave) still work however. even after clicking on the canvas, the mousewheel is still dead. the canvas's mousedown event works fine also. so only the mousewheel breaks. below is minimalist code to demonstrate what i'm seeing.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UMD.HCIL.Piccolo;
using UMD.HCIL.Piccolo.Event;
using UMD.HCIL.Piccolo.Nodes;
namespace piccolo_wheel_test {
public partial class Form1 : Form {
int mdown_count = 0;
int mwheel_count = 0;
public Form1() {
InitializeComponent();
PNode rect = PPath.CreateRectangle(40, 40, 20, 50);
rect.Brush = Brushes.Blue;
pCanvas1.Layer.AddChild(rect);
pCanvas1.Camera.MouseWheel += new PInputEventHandler(Camera_MouseWheel);
pCanvas1.Camera.MouseDown += new PInputEventHandler(Camera_MouseDown);
}
void Camera_MouseWheel(object sender, PInputEventArgs e) {
Debug.WriteLine("got mouse wheel: " + (mwheel_count++).ToString());
}
void Camera_MouseDown(object sender, PInputEventArgs e) {
Debug.WriteLine("got mouse down: " + (mdown_count++).ToString());
}
private void pCanvas1_Enter(object sender, EventArgs e) {
Debug.WriteLine("enter pcanvas");
}
private void pCanvas1_Leave(object sender, EventArgs e) {
Debug.WriteLine("leave pcanvas");
}
private void button1_Enter(object sender, EventArgs e) {
Debug.WriteLine("enter button");
}
private void button1_Leave(object sender, EventArgs e) {
Debug.WriteLine("leave button");
}
}
}
as an aside, i see that the canvas does not raise "enter"/"leave" events consistently; i see one "enter" when the form loads and one "leave" if i click button1 but no more "enter"/"leave" if i go back and forth. further, when i click on button1, i raises its "enter" event but when i click back on the canvas, "button1" doesn't raise its "leave" event (which it does if i clicked on other winform widgets, such as the trackbar.) thanks.

Trying to override NavigationMode.Back

I'm trying to develop a simple app for Windows Phone 8, and there are many requirements for the use of the Back Button. As I don't want the Back Button to simply GoBack in back stack, I'd like to pop up a message box to warn the user that this action will bring him back to main menu.
Problem is, this page has to be reloaded some times, and the following code stop working properly after 1 reload. The messagebox opens multiple times. And the more times I reload, the more MessageBox appears.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using BackButtonTests.Resources;
namespace BackButtonTests
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
NavigationService.Navigating += NavigationService_Navigating;
}
void NavigationService_Navigating(object sender, NavigatingCancelEventArgs e)
{
if (e.NavigationMode == NavigationMode.Back)
{
e.Cancel = true;
MessageBox.Show("Quit");
}
}
private void Restart_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/MainPage.xaml?reload=" + DateTime.Now.ToString(), UriKind.RelativeOrAbsolute));
//Use this fake reload query with unique value as a way to "deceive" the system, as windowsphone does not support NavigationService.Reload, and using simply the Uri of the same page will not properly load everything
}
private void Quit_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Quit");
}
}
}
This is just a test code I wrote, that shows exactly the problem I'm experiencing in my actual project. Of course there are 2 buttons written in xaml.
And the code won't work until you first reload the page, as it's not NavigatedTo when it's the front page (not a problem in my actual project).
Any clues of what I'm doing wrong?
NOTE: I'm not interested in changing the event handler (to OnBackKeyPress, for instance). I'm interested in understanding what's going on with the handler I chose (NavigationService.Navigating, NavigationMode.Back). Thanks
Updated following additional information that clarifies the questing
Changing your navigating event handler to will mean the event isn't fired on every page in the stack
void NavigationService_Navigating(object sender, NavigatingCancelEventArgs e)
{
NavigationService.Navigating -= NavigationService_Navigating;
if (e.NavigationMode == NavigationMode.Back)
{
e.Cancel = true;
MessageBox.Show("Quit");
}
}
No longer neccessary
Override OnBackKeypress instead of navigating
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
var DoYouWantToQuit = MessageBox.Show("Are you sure you want to Quit", "Quit", MessageBoxButtons.OkCancel);
if (DoYouWantToQuit != MessageBoxButton.Ok)
{
e.Cancel = true
}
base.OnBackKeyPress(e);
}

Search Button in C#

I know this question has been asked many of times about how to create a search button. I am very new to C# programming and I am having a hard time creating a search and just haven't found what I am looking for from other posts. So I hope someone can help me.
I have created a Windows Form Application and I have a form setup using "Details" view from my DataSet and the data shows up correctly in the application when I scroll from record to record. My data is stored in a sdf file. I want to have people either enter in an "account number" or a persons "last name" and then be able to hit the search button. And after the search button the prearranged fields would update with the information. For the ability to either choose the "last name" or the "account number" I can have the items listed in a combo box if need be.
I have included a copy of the code some of the naming of the items have been changed as to not disclose my profession. Any help is greatly appreciated.
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void custtableBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.custtableBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.custDataSet);
}
private void label1_Click(object sender, EventArgs e)
{
}
private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
}
private void custtableBindingNavigatorSaveItem_Click_1(object sender, EventArgs e)
{
this.Validate();
this.custtableBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.custDataSet);
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'custDataSet.custtable' table. You can move, or remove it, as needed.
this.custtableTableAdapter.Fill(this.custDataSet.custtable);
}
private void file_Name_12TextBox_TextChanged(object sender, EventArgs e)
{
}
private void fillByToolStripButton_Click(object sender, EventArgs e)
{
try
{
this.custtableTableAdapter.FillBy(this.custDataSet.custtable);
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
private void btnfind_Click(object sender, EventArgs e)
{
}
}
}
What is the custtableBindingNavigatorSaveItem_Click and custtableBindingNavigatorSaveItem_Click_1 ? Thet are the same, if they are two buttons doing the same thing then you can use the same method from both buttons (though why two buttons to do the same thing I do not know).
Anyway, you have some choices and it depends on the datasize, speed needed, whether the data should be offline or db locked and so on...
Set you queries/stored procedures (sprocs are safer) to allow for limiting the result. This lets the DB do all the hard work - it is what DB's are designed for.
Read everything into memory (your table fill) and select off the table - not to disimilar really with Linq queries. This means all the data is offline and you will need to decide when and how you write it back to the database (if at all).
PS: I am intrigued, what is your profession that your pseudonym here is not enough to hide behind? Imagination gone wild !

Categories

Resources