Why won't this simple messagebox work? - vb.net

Module Module1
Public cccounter = 9
End Module
Public Class frmNim
Private Sub btnSelect_Click(sender As System.Object, e As System.EventArgs) Handles btnSelect.Click
MsgBox(cccounter)
End Sub
End Class
Why does this generate errors? I can't figure out any other way to make a simple counter go up by clicking on a button. This is frustrating me to no end. Is there something very simple that I'm obviously missing?

Use MessageBox.Show(ccounter)
I think you're using the old VB6 coding. This won't work in VB.NET.
MSDN
If you need your counter to go up, you do need an extra line of code:
ccounter += 1
EDIT:
Missed the declaration in the module (VB.Net bit rusty now a days)
You should declare the ccounter as a variable as mentioned by #Eddie Paz)
I've made a quick sample that adds 1 at every click on the button.
Module Module1
Public ccounter As Integer = 9
End Module
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
ccounter += 1
MessageBox.Show(ccounter)
End Sub
End Class

You're declaring cccounter as a variant in Module1. You should specify the type such as integer. In the btnSelect:
cccounter = cccounter + 1
MessageBox.Show(cccounter)
I think MsgBox still works in VB.Net, but I don't remember. I try to use the .NET way now.

Related

Basic math functions in VB.net

So i am currently in process of adding a basic calculator that allows the user to define 2 variables and then press a button that puts the variables into a basic math equation and presents the result but i think i have gone about it completely wrong.
this is my first time using math functions in VB and would appreciate it if someone can show me where im going wrong.
this is my code so far:
Imports System.Math
Public Class SOGACALC
Dim soga As String = Math.Abs(72 - months.Text) * opp.Text
Private Sub SOGACALC_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SOGAValue.Text = soga
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
HOME.Show()
Me.Close()
End Sub
End Class
Where you have written
Dim soga As String = Math.Abs(72 - months.Text) * opp.Text
I suspect that you are anticipating that soga will be a function of the properties referred to in that and will change when those properties change. It does not work that way.
The way to get a value which varies depending on its parameters is to define a function, so you might have:
Friend Function Soga(monthValue As Control, oppThing As Control) As String
Dim month As Integer = CInt(monthValue.Text)
Dim opp As Decimal = CDec(oppThing.Text)
Return (Math.Abs(72 - month) * opp).ToString()
End Function
and call it like:
'TODO: Give Button1 a meaningful name.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SOGAValue.Text = Soga(months, opp)
End Sub
where there are controls names "months" and "opp" on the form.
I strongly recommnend that you use Option Strict On - it points out problems in code and suggests corrections for you.
Notice that I used the Decimal type for opp - I had to guess at a suitable type because nowhere in the code you showed us was there any indication of what type it needs to be.
An improvement would be to use TryParse methods instead of CInt/CDec, so that you can inform the user if they have made a simple typing error.

cannot read from combo box to use as a variable in a sub-routine

I am trying to have a variable input come from a combobox. i had thought it would be a simpler task but i am stuck on this and would be grateful for some help.
i am working with pre-packaged code that connects to an API, connects to a server and does stuff. I am customizing it to do a few additional calculations, all of which now work fine, but the variables i use to perform those calcs, inside of one of the sub-routines, are hard coded, and i want to be able to read them in instead using a combobox. i had done this many times using VB6 and VBA, but i am new to vb.net (2010) and even though i have the combobox on my form, every reference to the selected number in the combobox ends up with an empty result
in a simpler application, like the one below, i am able to get the data from the combo box:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim divisor As Integer
Dim res1 As Integer
If Int32.TryParse(ComboBox1.Text, divisor) Then
MsgBox(ComboBox1.Text)
Else
MsgBox("error" & ComboBox1.Text)
End If
res1 = divisor - 9
MsgBox(res1)
End Sub
End Class
Unfortunately the code i am working with is not cooperating with the code above. without posting all the code, here is the basic structure, maybe this will help you help me figure out where to write the code above so it will grab the value in the combobox where the 'divisor' in the sub13 (see below) will be the number from the combobox instead of it being hard coded
and every reference you see below to the combobox was 'auto-generated'. that is, once i placed the combobox on the form all of that code appeared.
Imports X.API
Public Class frmMain
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Protected Sub Dispose(ByVal disposing As Boolean)
End Sub
Private components As System.ComponentModel.IContainer
Friend WithEvents Panel1 As System.Windows.Forms.Panel
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ComboBox1 = New System.Windows.Forms.ComboBox()
Me.ComboBox1.FormattingEnabled = True
Me.ComboBox1.Location = New System.Drawing.Point(710, 117)
Me.ComboBox1.Name = "ComboBox1"
Me.ComboBox1.Size = New System.Drawing.Size(121, 21)
Me.ComboBox1.TabIndex = 3
End Sub
#End Region
#Region " Member Variables "
Private mTable As DataTable
#End Region
#Region " Form and Control Events "
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
End Sub
Private Sub s1()
End Sub
Private Sub s2()
End Sub
Private Sub s3()
End Sub
Private Sub s4()
End Sub
#End Region
#Region " Operations "
Private Sub s5()
End Sub
Private Sub s6()
End Sub
Private Sub s7()
End Sub
Private Sub s8()
End Sub
#End Region
#Region " API Events "
Private Sub s8()
End Sub
Private Sub s9()
End Sub
Private Sub s10()
End Sub
Private Sub s11()
End Sub
Private Sub s12()
End Sub
#End Region
Private Sub s13()
Dim divisor As Integer = 1
'[this is where i want the divisor to draw from the combobox]
'so instead of "Dim divisor As Integer = 1"
i want "Dim divisor As Integer = contents of combobox
End Sub
' i have no idea why this code appears here
Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
Private Class Item1
Public Sub s14()
End Sub
End Class
Private Class Item2
Public Sub s15()
End Sub
End Class
Private Class Item3
Public Sub s16()
End Sub
End Class
End Class
the sub i am working on is "s13()", but when i try to read from the combobox i get a blank.
exactly where to place the code that works as shown in my first example in the structure i show immediately above is unknown to me. i would have thought it would be much easier to read from a combobox, but i am stumped.
Given the names of your functions, it's difficult to tell exactly what you're trying to do. But you have a couple of options here. The function itself can access the value from the combo box if it's called while the combo box has a value in it, or the function can require the value as a function argument and whatever calls the function can pass the combo box's value.
For the first, it would look something like this:
Private Sub s13()
Dim divisor as Int32 = 1
If Int32.TryParse(Me.ComboBox1.Text, divisor) Then
' Perform your logic
Else
' The input wasn't a valid integer, maybe show an error?
End If
End Sub
Whereas the second approach might look like this:
Private Sub s13(ByVal divisor as Integer)
' Perform your logic
End Sub
And the code which calls that would need to get the value:
Dim divisor as Int32 = 1
If Int32.TryParse(Me.ComboBox1.Text, divisor) Then
s13(divisor)
Else
' The input wasn't a valid integer, maybe show an error?
End If
The main thing to note in all of this is that you should use Int32.TryParse() to determine if the inputted value in the combo box is actually an integer, and handle the error condition when it isn't.
You have to use the selected item :
If Int32.TryParse(ComboBox1.selectedItem, divisor) Then
MsgBox(ComboBox1.selectedItem)
Else
MsgBox("error" & ComboBox1.selectedItem)
End If
for anybody stuck with the same problem, here is the solution.
the code i had to work with is mammoth, so it would not let me pull in a combobox variable into any of the subs, esp as this is a multi-threaded prog. i knew, however, the solution had to be simple. and that is always something to keep in mind -- the solution is never that complicated, and you never need as much code as you think.
in this case i went down the path of a public variable,
Public Shared divisor As Integer
i placed this line underneath the very first line
Public Class frmMain
since there were other drop-down lists on the form that were clearly being drawn from, i went to the code associated with the button that initiated the steps to get the data from the server, just another button_click sub
if that sub pulled in the other combo boxes, then it had to grab the data in the one i wanted. all i did then was add this line of code to that button_click sub
divisor = ComboBox1.Text
since divisor is now a public variable, what was recorded in the button_click sub easily passes to another private sub that runs other routines. so anywhere in the code i want to use the variable 'divisor' all i have to do is mention it :)
if divisor > 0 then
'do something
end if

How is it possible to parse the URL of the desired popup to the popup-form AND show hints/tooltips in the WebKit-Component?

I'm trying to use the WebKit-component (http://www.webkit.org/) in VB with the help of Visual Studio 2008.
This is running without problems, except for two following two issues:
1. Hints/Tooltips are not shown (e.g. as there usually will appear one if you stay with the mouse over the Google-logo)
2. If there's a popup-window, I don't know how to get the new desired URL.
I'm already working a few days on this matter and couldn't find any solution yet :(
Maybe you know a solution to this problem.
Cheers
Markus G.
P.S.: If you need more than the following Source Code to analyze the problem, then let me know ...
Source Code Form1
Imports System.IO
Imports WebKit
Public Class frmMain
Private _url As String
Private _mode As String
Private _popupUrl As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
On Error Resume Next
Dim bLogging As Boolean
setWindowAndBrowserSettings()
_url = "http://www.google.com"
browserComp.Navigate(_url)
End Sub
Private Sub setWindowAndBrowserSettings()
Me.Text = "Test - Browser"
Me.WindowState = FormWindowState.Maximized
browserComp.Dock = DockStyle.Fill
browserComp.Visible = True
End Sub
Private Sub browserComp_NewWindowCreated(ByVal sender As Object, ByVal e As WebKit.NewWindowCreatedEventArgs) Handles browserComp.NewWindowCreated
'frmPopup.WindowState = FormWindowState.Maximized
frmPopup.Text = "ixserv - POPUP"
frmPopup.popup.Navigate(_popupUrl)
frmPopup.Show()
End Sub
Private Sub browserComp_NewWindowRequest(ByVal sender As Object, ByVal e As WebKit.NewWindowRequestEventArgs) Handles browserComp.NewWindowRequest
e.Cancel = False
_popupUrl = browserComp.Url.ToString ' WHERE can I get the clicked URL? This is the old one of the remaining window
End Sub
End Class
Code Form2
Public Class frmPopup
End Class
Following popup/new-Window-Create-function works for me:
Private Sub browserComp_NewWindowCreated(ByVal sender As Object, ByVal e As WebKit.NewWindowCreatedEventArgs) Handles browserComp.NewWindowCreated
frmPopup.Text = "POPUP"
Dim popupBrowser As WebKit.WebKitBrowser
popupBrowser = e.WebKitBrowser
frmPopup.Controls.Add(popupBrowser)
frmPopup.Show()
End Sub
whereas frmPopup is a new form.
Before I tried this I already added the Webkit-component to the new form, which might had been the problem. I assume, the trick is, to create a new WebKitBrower-element that is directly connected to the argument e.WebkitBrowser instead of overloading an existing webkitbrowser-component in the form. Don't ask me for reasons for this now (I really don't know) :P
Oh, I should add that I used the Webkit.NET component. The same trick works also for the OpenWebkitSharp-wrapper
The hint-problem still remains ...

visual basic :Help removing items from listbox

I have to make a listbox with a few(8) names in it & double clicking on a name in the listbox will removed the name from it.
I have already add the names into the form using the listbox.items.add method & would display the names in it.
Then I enter the coding for 8 names in double_click procedure(listbox) using the "listbox.items.remove" method.
However, when i try double clicking on a name in the listbox, it would remove all the names instead.
What coding do i need? help appreciated!
Option Strict On
Option Explicit On
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.Items.Clear()
ListBox1.Items.Add("1")
ListBox1.Items.Add("2")
ListBox1.Items.Add("3")
ListBox1.Items.Add("4")
ListBox1.Items.Add("5")
ListBox1.Items.Add("6")
ListBox1.Items.Add("7")
ListBox1.Items.Add("8")
End Sub
Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
Dim i As Integer = ListBox1.SelectedIndex
If i >= 0 And i < ListBox1.Items.Count Then
ListBox1.Items.RemoveAt(i)
End If
End Sub
End Class
if you are looking at dynamic deletion of items, i think you should check out Jquery,Ajax,DOM
there are a couple of nice tutorials that would help you with that. i just came across this one and found it interesting
http://www.satya-weblog.com/2010/02/add-input-fields-dynamically-to-form-using-javascript.html

Lua in Visual Basic.net

I'm trying to just get a VB.net app to be able to execute a lua script in a external file, and be able to add some functions to lua too, To do this I have this code:
Imports LuaInterface
Public Class Form1
Public luascripting As New Lua()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
luascripting.RegisterFunction("DisplayText", Me, Me.GetType().GetMethod("DisplayText"))
luascripting.DoFile("script.lua")
End Sub
End Class
But it errors on the register function, saying "Object reference not set to an instance of an object." Do you know of a example VB.net project that uses lua? Or know how to fix this?
You are registering a function but you forgot to write it. Paste this into your form code:
Public Sub DisplayText()
MsgBox("Works")
End Sub