Assign a value? - vb.net

I have this
Public Shared Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
Dim c As Client
Dim Armor As Item = c.Inventory.GetItemInSlot(SlotNumber.Armor)
End Sub
And when I click the button, is crashes and "System.NullReferenceException" pops up , I've searched around and found out that it happens because c is used before it is assigned a value so I'd like to know, what would be the proper way to assign it a value?

try this:
instead of:
Public Shared Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
Dim c As New Client
Dim Armor As Item = c.Inventory.GetItemInSlot(SlotNumber.Armor)
End Sub
try:
Public Shared Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
Dim c As New Client.Inventory
Dim Armor As Item = c.Inventory.GetItemInSlot(SlotNumber.Armor)
End Sub

Related

How to add items from a MenuStrip event to a ListBox using a For Next Loop

That might have sounded weird so let me explain.
I have a school assignment that has me pulling my hair out. I have to get a collection of 5 facts and have them display to a ListBox using a For Next Loop. The user would use an InputBox to input the facts.
I dont know what to put in the For Next to fetch the string from the InputBox. I'm at my wits end and am falling behind.
Here is what I have so far
Public Class frmWWIIFacts
Private Property RemoveAt As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub AddFactToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddFactToolStripMenuItem.Click
Dim intFact As Integer
Dim strInputFact As String
strInputFact = InputBox("Do you want to add a fact?", "Add a fact")
For
Next
strInputFact = InputBox("Do you want to add a fact?", "Add a fact")
End Sub
Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
Application.Exit()
End Sub
Private Sub ClearListToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearListToolStripMenuItem.Click
lstFacts.Items.Clear()
End Sub
Private Sub RemoveFactToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveFactToolStripMenuItem.Click
End Sub
I've submitted a reddit post requesting some assistance but its gotten me nowhere. https://www.reddit.com/r/learnprogramming/comments/3t614u/vb2015_using_menustrip_to_addremove_items_in_a/
I would love some help on this. Please ask questions if your confused on my method or if you need to know more.
Sounds like you're trying to do this:
Private Sub AddFactToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddFactToolStripMenuItem.Click
Dim intFact As Integer
Dim strInputFact As String
lstFacts.Items.Clear()
For intFact = 1 To 5
strInputFact = InputBox("Please enter a fact:", "Add a fact")
If Not strInputFact = "" Then
lstFacts.Items.Add(strInputFact)
End If
Next
End Sub

How to save data to a text file?

Basically, I have this program, where you click a link and it opens it. But it doesn't save all the links I put in. There is a name list and link list. You can add links and names with the buttons, and then open link with 'watch'. I was primarily going to use this for youtubers and streamers. Here is the code.
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim NamePath As String = ("C:\Favoriter\NameList.txt\")
Dim nsr As StreamReader
nsr = New StreamReader(NamePath)
Do Until nsr.EndOfStream
lstName.Items.Add(nsr.ReadLine)
Loop
nsr.Close()
Dim URLPath As String = ("C:\Favoriter\URLList.txt\")
Dim usr As StreamReader
usr = New StreamReader(URLPath)
Do Until usr.EndOfStream
lstURL.Items.Add(usr.ReadLine)
Loop
usr.Close()
End Sub
Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim NamePath As String = ("C:\Favoriter\NameList.txt\")
Dim nsw As StreamWriter
nsw = File.CreateText(NamePath)
Dim NameItems As String
Do Until lstName.Items.Count.Equals(0)
NameItems = lstName.Items.Item(0)
lstName.Items.RemoveAt(0)
nsw.WriteLine(NameItems)
Loop
nsw.Flush()
nsw.Close()
Dim URLPath As String = ("C:\Favoriter\URLList.txt\")
Dim usw As StreamWriter
usw = File.CreateText(URLPath)
Dim URLItems As String
Do Until lstURL.Items.Count.Equals(0)
URLItems = lstURL.Items.Item(0)
lstURL.Items.RemoveAt(0)
usw.WriteLine(URLItems)
Loop
usw.Flush()
usw.Close()
End Sub
Private Sub lblTitle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblTitle.Click
End Sub
Private Sub lstName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstName.SelectedIndexChanged
End Sub
Private Sub lstURL_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstURL.SelectedIndexChanged
End Sub
Private Sub txtName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtName.TextChanged
End Sub
Private Sub txtURL_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtURL.TextChanged
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
lstName.Items.Add(txtName.Text)
lstURL.Items.Add(txtURL.Text)
End Sub
Private Sub btnWatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWatch.Click
If lstURL.SelectedItem = True Then
Process.Start(lstURL.SelectedItem)
End If
End Sub
Private Sub lblName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblName.Click
End Sub
Private Sub lblURL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblURL.Click
End Sub
End Class
As first look i see you are using File.CreateText(Path), this function will overwrite the old file what ever it has data , so everytime after you write your data you delete them by overwriting, you can use
if File.Exists(Path) then
File.Open(Path)
else
File.CreateText(Path)
End If
like this you will not overwrite each time you write the text .

IF statement not working as I expected

Hi so im playing with microsoft visual studio 2010 edition and I need to make an if statement work on it.
What im doing is putting a number into a textbox, it then goes into my listbox (however many times I put numbers in like 3-4 numbers). Then it is meant to add them all up and put it into the label after.
Heres the program so far
Public Class Form1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim Result As Integer
For Each Item As Integer In ListBox1.Items
Result = Result + Item
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Value As String
Value = TextBox1.Text
ListBox1.Items.Add(Value)
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
Label1.Text = "You have a Balance of " + DialogResult.ToString
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If (String.a
End Sub
End Class
In Visual Basic .NET (which is what your code looks to be in), If statements are formatted like this:
If condition Then
DoSomeCodeHere()
End If
C# code is formatted as
if (condition)
{
DoSomeCodeHere();
}
Do you mean you wan the sum all the amount in the list box and show it in a label after clicking button 2?
If yes add the below should do.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim Result As Integer
For Each Item As Integer In ListBox1.Items
Result = Result + Item
Next
Label1.Text = "You have a Balance of " + Result.ToString
End Sub

Get selected row of inactive datagridview

I have situation that have to know which exact row of datagridview1 (MultiSelect = False) is selected on another (inactive) datagridview1 when I am in some event handler of datagridview2 which is currently active.
I try a bunch of attempts but without correct result.
Private Sub DataGridView2_CellDoubleClick(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
Handles DataGridView2.CellDoubleClick
Dim myindex1 As Integer = DataGridView1.CurrentCell.RowIndex
OR
Dim myindex1 As Integer = DataGridView1.CurrentRow.Index
None of this don't work like it works when datagridview1 is active and I get index from their event handlers.
What should I do and how accuratelly get selected row of first datagridview from event handler of second datagridview?
EDIT:
Added code:
Private Sub datagridview1_RowLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
selrow1 = e.RowIndex
End Sub
Private Sub datagridview1_RowPrePaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint
e.PaintParts = e.PaintParts And Not DataGridViewPaintParts.Focus
End Sub
Private Sub datagridview1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.GotFocus
DataGridView1.RowsDefaultCellStyle.SelectionBackColor = Color.FromKnownColor(KnownColor.Highlight)
End Sub
Private Sub datagridview1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.LostFocus
DataGridView1.RowsDefaultCellStyle.SelectionBackColor = Color.FromKnownColor(KnownColor.InactiveCaption)
End Sub

Adding Multiple Items From One Listbox to Another VB.Net

I am able to only move single items from one listbox to another with this code. I tried with both MultiSimple & MultiExtended SelectionMode.
How do I select multiple items and then move them?
Private Sub cmdAdd_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs
) Handles cmdAdd.Click
Dim i As Integer = Listbox1.SelectedIndex
If i = -1 Then
Exit Sub 'skip if no item is selected
End If
Listbox2.Items.Add(Listbox1.Items(i))
Listbox1.Items.RemoveAt(i)
End Sub
You need to use SelectedIndices or SelectedItems.
Private Sub cmdAdd_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs
) Handles cmdAdd.Click
Dim selectedItems = (From i In ListBox1.SelectedItems).ToArray()
For Each selectedItem In selectedItems
Listbox2.Items.Add(selectedItem)
Listbox1.Items.Remove(selectedItem)
Next
End Sub
Note the use of a Linq query to get list of selected items as an array. Using an array is required to prevent "Collection changed" exceptions. You may need to add a reference to System.Linq.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.Items.Add("SanDiego")
ComboBox1.Items.Add("BeverlyHills")
ComboBox1.Items.Add("Florida")
ComboBox1.Items.Add("NewYork")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim s As String
s = ComboBox1.SelectedItem
ListBox1.Items.Add(s)
ComboBox1.Items.Remove(s)
End Sub
Private Sub cmdAdd_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs
) Handles cmdAdd.Click
For Each selectedItem In (From i In ListBox1.SelectedItems).Tolist()
Listbox2.Items.Add(selectedItem)
Listbox1.Items.Remove(selectedItem)
Next
End Sub