How can I emulate a PictureBox control in a console application - vb.net

Is there any way to emulate a picturebox in a console application? I tried this way but the image always returned completely black:
Using P As New PictureBox
P.Size = New Point(255, 255)
P.Image = New Bitmap(255, 255) 'I did set a real image, but I didn't for the sake this example
End Using

Assuming you've imported everything needed to show the form, don't use the form.Show method. Instead, use Application.Run(New Form1) to show the form. This post has a more complete answer.
You can design the form in the designer,use it as is, or declare it as a new object and change any properties and pass the new object to the Run method.
Imports System.Drawing
Imports System.Windows.Forms
Module Module1
Sub Main()
Dim newform1 As New Form1
newform1.PictureBox1.Image = New Bitmap("MyImageFile")
Application.Run(newform1)
Console.ReadLine()
End Sub
End Module

Related

vb.net Parameters Startup Form not hiding?

I am creating a application to be used with a touch panel device. The touch panel device comes with a standard windows OSK (On screen Keyboard). Whilst testing its been concluded that the standard OSK is to large and too complex for what we need it in. So I have built my own OSK. some of the feilds though only requier numeric inputs so I though of futher simplifying the process by creating a new form which hosts a numeric pad. so far this is all working. the idea is then to have the app which ask for diffrent inputs then to trigger the OSK application, say that the user wants to enter a phonenumber in one textbox I then want to start the OSK app using a parameter that trigers the OSK to start the NumericForm form first... this too I have working but the thing I can't get right is to hide the AlphabetForm I have tried the following method but am a little stumpt on how to get this right
In short its the Me.hide which isnt working as expected?
Private Sub AlphabetForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
#Region "Recive startup parameters (if any)"
Try
Dim OSKParameters As String = Command()
If OSKParameters = "OSKNUM" Then
NumericForm.Show()
Me.Hide()
Else
ShiftSelect = 0
End If
Catch ex As Exception
'Do nothing
End Try
#End Region
End Sub
Three possible setups, as described in comments:
► Using the Application Framework, override OnStartup and set Application.MainForm to a Form object determined by a command-line argument:
To generate ApplicationEvents.vb, where Partial Friend Class MyApplication is found, open the Project properties, Application pane, click the View Application Events Button: it will add the ApplicationEvents.vb file to the Project if it's not already there.
Imports Microsoft.VisualBasic.ApplicationServices
Partial Friend Class MyApplication
'[...]
Protected Overrides Function OnStartup(e As StartupEventArgs) As Boolean
Application.MainForm = If(e.CommandLine.Any(
Function(cmd) cmd.Equals("OSKNUM")), CType(NumericForm, Form), AlphabetForm)
Return MyBase.OnStartup(e)
End Function
'[...]
End Class
► Disabling Application Framework, to start the Application from Sub Main().
In the Project->Properties->Application pane, deselect Enable application framework and select Sub Main() from the Startup form dropdown.
If Sub Main() doesn't exist yet, it can be added to a Module file. Here, the Module is named Program.vb.
Module Program
Public Sub Main(args As String())
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(True)
Dim startForm as Form = If(args.Any(
Function(arg) arg.Equals("OSKNUM")), CType(New NumericForm(), Form), New AlphabetForm())
Application.Run(startForm)
End Sub
End Module
► If the OSK can be moved to UserControls, similar to what jmcilhinney suggested, run the default container Form and select the UserControl to show using the same logic (inspecting the command-line arguments):
Public Class AlphabetForm
Public Sub New()
InitializeComponent()
Dim args = My.Application.CommandLineArgs
Dim uc = If(args.Any(
Function(arg) arg.Equals("OSKNUM")), CType(New NumericUC(), UserControl), New AlphabetUC())
Me.Controls.Add(uc)
uc.BringToFront()
uc.Dock = DockStyle.Fill
End Sub
End Class

Dinamically load XAML in UserControl for Excel Add In (VB)

I am developing an Excel Add In in which I intend to dinamically load a XAML canvas to a WPF UserControl.
The code below worked fine in a full WPF template...
Imports Microsoft.Win32
Imports System.IO
Class MainWindow
Private Sub btnLoadXAML_Click(sender As Object, e As RoutedEventArgs)
Try
Dim FlDialog As OpenFileDialog = New OpenFileDialog()
FlDialog.ShowDialog()
Dim lFlName As String = FlDialog.FileName
'load selected file
Dim fs As FileStream = New FileStream(lFlName, FileMode.Open)
Dim gridToLoad As New Canvas
With gridToLoad
.Height = 300
.Width = 300
End With
gridToLoad = System.Windows.Markup.XamlReader.Load(fs)
grdLoadXAML.Children.Add(gridToLoad)
fs.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
...but when I use it on a Excel Add In template, I can't refer to a XAML object via VB code, such as:
Dim gridToLoad As New Canvas
I mean "New Canvas" doesn't seem to be "imported" into this kind of template. I couldn't find which library to refer to make this work.
Thanks in advance.
Sorry,
Just a few minutes after I've tried to build the project and Visual Basic itself gave me the answer!
Just had to add reference for System.Xaml (dumb!).
Thanks anyway!! :)
Best.

Calling a separate Windows Form using its name as a String

I need to be able to create a Button that can link to a different Windows Form when I click the Button. However, this Button is dynamically generated and can sometimes link to different Forms as per required. For example:
My Button can link to either FormA.vb or FormB.vb. I can make the Button create the String "FormA" or "FormB" as necessary, but I don't know how to call FormA.vb or FormB.vb to the screen.
Thus far, I have been changing Windows Forms by using:
FormA.MdiParent = MainForm //My main form window
FormA.Show()
Me.Close()
But this obviously will not work with:
"FormA".MdiParent = MainForm
"FormA".Show()
Simply because they are Strings and not classes.
Is there a way to make my Button link correctly?
Thanks in advance.
Try this, you have to import System.Windows.Forms and System.Reflection
First get the form name into the strCreatedFromButton then find it.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strCreatedFromButton As String = "Form3"
Dim frm As New Form
frm = DirectCast(CreateObjectInstance(strCreatedFromButton), Form)
frm.Show()
End Sub
Public Function CreateObjectInstance(ByVal objectName As String) As Object
Dim obj As Object
Try
If objectName.LastIndexOf(".") = -1 Then
objectName = [Assembly].GetEntryAssembly.GetName.Name & "." & objectName
End If
obj = [Assembly].GetEntryAssembly.CreateInstance(objectName)
Catch ex As Exception
obj = Nothing
End Try
Return obj
End Function
Use a Form type variable to store the reference of either FormA or FormB. Then through polymorphism you can call the Show() method that will execute the appropriate instance's method. For example:
Dim frm as Form
If <SomeCondition> Then
frm = New Form1()
Else
frm = New Form2()
End If
frm.Show()
This is just the core concept. You can extend it to match your exact needs.
Edit
Reading the comments, I'd suggest you just code a large switch (Select Case in VB.NET) for your existing forms and then add new cases for new forms as they're added. You could implement the Factory design pattern to pass your string (e.g. "FormA") to the Factory method and let the factory method return appropriate child class object (again using a switch). To minimize deployment effort, you could keep this Factory class and all new form classes in a separate assembly that will work using simple xcopy deployment.
If you must code it once for all future forms, Reflection is the only way you can do it. However, I'd recommend against it.

Using button.enabled outside the Button_Click sub

I am just starting out with Visual basic .Net. I am designing an application where each button should disable itself after a registering a single click on it.
I am trying to add an additional sub, which when called, disables all of the buttons. (Disable_all())
Public Sub disable_all()
MsgBox("Testing disable_all")
Button_eight.Enabled = False
End Sub
One of the subs is calling it from the main module.
Private Sub disable()
Dim variab As Form1 = New Form1
variab.disable_all()
End Sub
The disable_all() sub however does not disable Button_eight when called. I placed a message box to check if the sub (disable_all) is getting executed, which it is. I get a message box having the text "Testing disable all" but the Button_eight is not disabled for some weird reason.
I'd really appreciate some help, thanks.
It's simple, just do Form1.disable_all().
That is, instead of variab.disable_all(), call: form1.disable_all() from the main module. Because as mentioned in the comments, new form1 creates a new instance of Form1.
You can also create a variable that point to the form dim var = form1 or dim variab as form = form1, but since you can access form1 directly from your module, it is unnecessary.

Show a WinForms from a Console application

I have an existing Console Application project.
I have added a Windows Form to the project called myForm
When the project runs it goes to the Console's Main method - in this method how do I activate/show myForm ?
I assume I need to import the library System.Windows.Forms so the top of my console code looks like the following:
Imports System.Windows.Forms
Module Module1
Sub Main()
myForm. '<<<<not sure how to activate form
...
You have to add the reference System.Windows.Forms, and then show the form:
myForm.Show()
Or
myForm.ShowDialog()
myForm has to be a Form type. Maybe you need to instanciate you form first:
Dim myForm as new FormName
Try this:
Sub Main()
'Your code goes here...
System.Windows.Forms.Application.Run(New myForm)
'Your code goes here...
End Sub
No need to import the forms library (I've tested) and the working code that I now have is:
My main problem was not declaring and creating an instance of the windows form.
Module Module1
Sub Main()
Dim xForm As myForm = New myForm
xForm.ShowDialog()
Call this function.
Application.Run(myForm)
It runs even from the console app.
From the documentation,
Begins running a standard application message loop on the current thread, and makes the specified form visible.
EDIT: Declare it like this.
Public Class MyForm
Inherits Form
' Make the code here
End Class
Dim form As MyForm = New MyForm
Application.Run(form)