0

Many people have asked how to get screen width and height in vb.

The answers included using My.Computer.Screen.Bounds, Screen.PrimaryScreen.Bounds, System.Windows.SystemParameters.PrimaryScreenWidth, etc.

But for me, they all come back undefined.

One said I want the Screen class located at System.Windows.Forms.Screen. I added a reference to system.windows.forms. Didn't work. Is that something to import? That didn't work either.

Do I import something? Add a reference? Inherits?

And many solutions use javascript. It's easy to get screen width and height with javascript. But the timing is wrong. I want the screen size before I generate my VB code. But I have to put my javascript in an "onload=", which makes it run AFTER the VB code is generated.

The only way I've found is to let the javascript determine the dimensions, then redirect, passing these values as parms. And that's not bullet proof.

There must be a simpler way.

I've never found a COMPLETE example, including declarations, even on the microsoft pages, that works.

Thanks in advance.

  • If you are trying to format a web application, you might want to look at [Responsive design](https://en.wikipedia.org/wiki/Responsive_web_design). It's mostly done with CSS. Don't forget, the code behind is run on the server, so technically the vb code would give you the server screen size, not the client. – the_lotus Aug 07 '15 at 12:01

1 Answers1

1

My.Computer.Screen.Bounds, etc are only valid for WinForms applications. You are using a Web application framework so this is not valid.

The only way to determine this is to use something that runs on the client e.g. Javascript as you have mentioned. I suggest you have a look at this question: Get the size of the screen, current web page and browser window

I suppose the question really is what are you trying to achieve as there is probably a better way of doing it

Community
  • 1
  • 1
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143