Error in declaring variables in VB.Net 2019 - vb.net

I'm not familiar with the syntax of the code in VB.NET and I want to use it to build the prototype of our system. I'm trying to practice some lines before I go with it and I'm having this error:
private void button1_Click(object sender, EventArgs e)
{
Dim Price As Integer
}
I got the code from the tutorials and the websites that I have visited but I don't know why it's showing errors, any ideas why?

Your code has mix C# and Vb.net script. The part private void button1_Click(object sender, EventArgs e) is a C# code while the Dim price as Integer is a vb.net code.
To Fix your code please try this solution in C#.
private void button1_Click(object sender, EventArgs e)
{
int price;
}
In vb.net code should like this.
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim price As Integer
End Sub

Related

problem with textbox TextChanged inside userControl (C# code to vb.net)

I'm trying to create a custom textbox with a userControl, my problem lies in the part of setting the default event of the userControl to handle the textChanged of the textbox. I currently have the userControl with only one textbox.
In c # I have this code that works perfectly (UserControl3.cs file):
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 custom_controls
{
[DefaultEvent("_TextChanged")]
public partial class UserControl3 : UserControl
{
public UserControl3()
{
InitializeComponent();
}
public event EventHandler _TextChanged;
private void TextBox1_TextChanged(object sender, EventArgs e)
{
if (_TextChanged != null)
{
_TextChanged.Invoke(sender, e);
}
}
}
}
But I have not been able to translate it correctly to vb.net where I need the control. Any help or suggestion is greatly appreciated.
This is the code resulting from the conversion from C # to VB.net:
<DefaultEvent("_TextChanged")>
Public Class UserControl3
Public Event _TextChanged As EventHandler
Private Sub textBox1_TextChanged(sender As Object, e As EventArgs) Handles textBox1.TextChanged
If _TextChanged IsNot Nothing Then 'error here: _TextChanged
_TextChanged.Invoke(sender, e) 'error here: _TextChanged
End If
End Sub
End Class
Error image:
You can't raise events the same way in vb and c#. vb has a keyword RaiseEvent to do that
<DefaultEvent("_TextChanged")>
Public Class UserControl3
Public Event _TextChanged As EventHandler
Private Sub textBox1_TextChanged(sender As Object, e As EventArgs) Handles textBox1.TextChanged
RaiseEvent _TextChanged(sender, e)
End Sub
End Class

Dealing with an error related to deleting a directory

Following is a very simple program I wrote, using Visual Studio 2012, that clearly shows the error:
Imports System.Drawing.Imaging
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Using bmp As New Bitmap(800, 200)
Dim gr As Graphics = Graphics.FromImage(bmp)
For n = 0 To 3
Dim fileName As String = "D:\Users\Gerry\Desktop\TmpImages\SmileFace" + CStr(n) + ".jpg"
Using tmpBmp As Bitmap = New Bitmap(Image.FromFile(fileName))
gr.DrawImage(tmpBmp, n * 200, 0)
End Using
Next
gr.Dispose()
Dim finalName As String = "D:\Users\Gerry\Desktop\Combined.jpg"
bmp.Save(finalName, ImageFormat.Jpeg)
End Using
Directory.Delete("D:\Users\Gerry\Desktop\TmpImages", True)
Application.Exit()
End Sub
End Class
The problem, which happens both in debug and stand alone modes, is that the folder TmpImages does not get deleted. The following is the error text:
System.IO.IOException: The process cannot access the file
'SmileFace0.jpg' because it is being used by another process. at
System.IO.Directory.DeleteHelper(String fullPath, String userPath,
Boolean recursive, Boolean throwOnTopLevelDirectoryNotFound,
WIN32_FIND_DATA& data) at System.IO.Directory.Delete(String fullPath,
String userPath, Boolean recursive, Boolean checkHost) at
Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectoryInternal(String
directory, DeleteDirectoryOption onDirectoryNotEmpty, UIOptionInternal
showUI, RecycleOption recycle, UICancelOption onUserCancel) at
Microsoft.VisualBasic.MyServices.FileSystemProxy.DeleteDirectory(String
directory, DeleteDirectoryOption onDirectoryNotEmpty) at
bmpSave.Form1.Form1_Load(Object sender, EventArgs e) in
C:\Users\Gerry\Documents\Visual Studio 2012\Projects\Learning and
Testing\bmpSave\bmpSave\Form1.vb:line 22 at
System.EventHandler.Invoke(Object sender, EventArgs e)
One experiment I tried was to add the statement Close after the statement gr.Dispose. The error is no longer thrown, but the directory is not deleted. Note that in the code, I tried both Directory.Delete and FileSystem.DeleteDirectory and had the same result both times. The error relates to one of the files in the folder TmpImages that is simply opened for reading.
Please suggest that which I should do to resolve this issue. Thanks.

public event itemcheck() is an event and cannot be called directly. use a 'raiseevent' statement to raise an event

Hi i converted the following c# code to vb.net.
public Dropdown(CheckedComboBox ccbParent)
{
this.ccbParent = ccbParent;
InitializeComponent();
this.ShowInTaskbar = false;
this.cclb.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.cclb_ItemCheck);
}
private void cclb_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (ccbParent.ItemCheck != null)
{
ccbParent.ItemCheck(sender, e);
}
}
Visual Basic
Private cclb As CustomCheckedListBox
Public Event ItemCheck As Windows.Forms.ItemCheckEventHandler
Public Sub New(ByVal ccbParent As PlexisCheckedComboBox)
MyBase.New()
Me.ccbParent = ccbParent
InitializeComponent()
Me.ShowInTaskbar = False
AddHandler cclb.ItemCheck, AddressOf cclb_ItemCheck
End Sub
Private Sub cclb_ItemCheck(ByVal sender As Object, ByVal e As
Windows.Forms.ItemCheckEventArgs)
If (Not (ccbParent.ItemCheck) Is Nothing) Then
ccbParent.ItemCheck(sender, e)
End If
End Sub
In the converted vb.net code im getting error in the following line as
""
If (Not (ccbParent.ItemCheck) Is Nothing) Then
ccbParent.ItemCheck(sender, e)
please help me how to resolve it .
Well, as the error message told you, you have to use the RaiseEvent keyword to raise events.
But even then it would not work, since you can't raise events outside of the class the event is declared in (in contrast to C#, where nested classes can raise events of the outer class).
So to solve this add a new method to the outer class called OnItemCheck which raises the ItemCheck event, and call that method in cclb_ItemCheck instead of trying to raise the event directly.

listbox item forecolor change

Does anyone know why the Listbox1.Refresh() command may not trigger the ListBox1_DrawItem sub every time?
In Microsoft Visual Basic 2010, a listbox has a forcolor and backcolor property. These properties change the forcolour and backcolor for all the items in the listbox. By default there is no property for the forecolor and backcolor of an individual item on a listbox, I am aware there is on a list view but I would still wish to use a listbox.
I am trying to have the ability to change the forecolor and backcolor properties of individual items in the listbox.
To do this the listbox's draw item sub must be used with the listbox's drawmode property set to OwnerDrawFixed. Then using a brush colour along with the e.graphics the forecolor or backcolor can be changed.
I have seen and followed examples of how to do this for the currently selected item. Such as the one from ehow's website.
What I am tiring to do however is change the colour of the litsbox item as it is added depending on a variable.
Here is my code:
Private Sub listbox_add()
Me.ListBox1.Items.Add(listbox_text(list_num)) ' adds the line to the list box
add_item_colour = True
ListBox1.Refresh()
End Sub
Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
Dim myBrush As Brush = Brushes.Black
e.DrawBackground()
If add_item_colour = True Then
If blue_message = True Then
myBrush = Brushes.Blue
Else
myBrush = Brushes.Black
End If
e.Graphics.DrawString(ListBox1.Items.Item(list_num), ListBox1.Font, myBrush, _
New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
add_item_colour = False
End If
e.DrawFocusRectangle()
End Sub
The listbox_text is a string array that stores the string being added, the list_num is a integer that increments when new items are added to the listbox and the blue_message is a Boolean that it true when I want a blue message and false when I don't.
The problem I seem to be having is that Listbox1.Refresh() command does not seem to be triggering the ListBox1_DrawItem sub every time it is called. I found this by using brake points. Does anyone know why this might be the case and how I could fix it?
Thanks, any help on this would be much appreciated.
First of all I suggest you to use background worker instead of directly writing down your code on UI thread.
Please refer the code below:
public partial class Form1 : Form
{
Brush myBrush = Brushes.Blue;
public Form1()
{
InitializeComponent();
this.backgroundWorker1.DoWork += backgroundWorker1_DoWork;
this.backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
this.backgroundWorker1.RunWorkerAsync(this);
}
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), listBox1.Font, myBrush, new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
e.DrawFocusRectangle();
}
private void button1_Click(object sender, EventArgs e)
{
this.backgroundWorker1.RunWorkerAsync(button1);
}
private void button2_Click(object sender, EventArgs e)
{
this.backgroundWorker1.RunWorkerAsync(button2);
}
void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.listBox1.Refresh();
}
void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
if (e.Argument == this)
{
listBox1.Items.Add("p1");
listBox1.Items.Add("p2");
}
else if (e.Argument == this.button1)
{
myBrush = Brushes.Red;
listBox1.Refresh();
}
else if (e.Argument == this.button2)
{
myBrush = Brushes.Green;
if (listBox1.SelectedItem == null)
return;
var test = listBox1.Items[listBox1.SelectedIndex];
listBox1.SelectedItem = test;
var g = listBox1.CreateGraphics();
var rect = listBox1.GetItemRectangle(listBox1.SelectedIndex);
listBox1_DrawItem(listBox1, new DrawItemEventArgs(g, this.Font, rect, listBox1.SelectedIndex, DrawItemState.Default));
listBox1.Refresh();
}
}
}

How to fix this event related error in VB.net (windows phone 7)

Hi The following code gives me an error, which I am not too sure about how to fix. Can anyone help please?
Code:
Public Event hw As EventHandler
Private Sub view_hw(sender As Object, eventArgs As Object)
If hw IsNot Nothing Then
hw.Invoke(Me, New EventArgs())
End If
End Sub
Error:
Error 1 'Public Event hw(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.
Public Event hw As EventHandler
Private Sub view_hw(sender As Object, eventArgs As Object)
RaiseEvent hw(Me, New EventArgs())
End Sub