How to make this more efficient? - vb.net

Private Sub btnOne_Click(sender As Object, e As EventArgs) Handles btnOne.Click
txtAnswer.AppendText(1)
End Sub
So this is my code (It's for 0 through to 9 and a dot for decimal points).
I'm working on calculator for an assignment, my question is, can I make this more efficient so I don't have a lot code doing the same thing?
I had considered setting up a function to read the contents of the buttons and add them to the textbox, but then the operation buttons and clear buttons would just add to the textbox instead of performing the code assigned to them.

you can create one method for the numeric buttons and add their value to the Tag Object. Then you can just reference the tag object to append to the text
Private Sub NumericButtons_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim btn As Button
btn = sender
txtAnswer.AppendText(btn.Tag)
End Sub

You could create a function just like that, but it should read the number from the sender's Text property, and only assign the number buttons to that function in the OnClick event.
The other buttons will have their own OnClick methods, and you should make a generic function like in this case when you find a common behavior in some of them. Perhaps you could do another generic function for all + - / x operators, it that's appropriate.

Related

How to access data from calling object in vb.net

I have a Window-Form 'caller' in vb.net containing a datagridview with a small overview table of certain objects, each with its own ID in the first column. Now, if a row is double clicked, i want to show a dialog 'edit', where one can edit many details of that row which i do not want in the overview table.
My approach is as follows: In the caller form i wrote this to call 'edit':
Private Sub dgdata_dbclick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dg_data.CellMouseDoubleClick
Dim f_edit As New edit
f_edit.ShowDialog(Me)
End Sub
That works fine.
However, in the called Form "edit" i need to check, which ID was selected and load this data from the database to edit it. I can access some data from the calling form 'caller' using e.g.
MsgBox(CType(Me.Owner, caller).Text)
to show the window title of 'caller'. However, i want to extract the currently selected ID in the datagridview or at least some variabhle containing it. In the caller form, this could be easily done by evaluating
dg_data.Item(0, selectedRow).Value.ToString
but i cannot access any relevant information in 'caller'. I have a public class with some global variables there but i cannot access them as well.
Probably my strategy to solve this problem is not the most clever approach? Basically, i want to open a very detailed edit window when someone clicks on a line in an overviewtable but simultaniously blocking the rest of the application as long as the edit window is open.
Thanks!
The idea is to pass the data to the second form. When you create an instance of the second form (my class is called Form2, yours is called edit) with the New keyword the Sub New is called on Form2.
Private Sub OpenEditDialog()
Dim f_edit As New Form2(32) '32 is the number you retrieve from your DataGridView
f_edit.ShowDialog(Me)
f_edit.Dispose()
End Sub
You pass the ID to Form2 and set a variable at Form level. You can then use the variable anywhere in Form2.
Public Class Form2
Private ID As Long
Public Sub New(SelectedID As Long)
InitializeComponent()
ID = SelectedID
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox.Show(ID.ToString)
End Sub
End Class
You need to call InitializeComponent() so the controls will show up.
How do you usually get data into objects? You set a property or pass an argument to a method or constructor? Why should this be any different? Decide which you want to use and then write that code in your form. If it's required data, I would suggest a constructor. Just write this code in your form:
Public Sub New
and hit Enter. That will generate a little extra code automatically. You can then add a field to store the value, a parameter to the constructor and then assign the parameter to the field inside.
Thank you for pointing me to the correct route.
I solved it like this (which works fine and which is hopefully acceptable):
In the calling form:
Private Sub dgdata_dbclick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dg_data.CellMouseDoubleClick
Dim selectedRow As Integer = dg_data.CurrentCell.RowIndex
Dim f_edit As New edit
f_edit.edit(dg_data.Item(0, selectedRow).Value.ToString)
f_edit.ShowDialog(Me)
f_edit.Dispose()
End Sub
In the called form:
Public Sub edit(ByVal id As Long) 'Handles MyBase.Load
'Enter commands to prepare your form
End Sub

How to remove the most recently added control?

I Intended to display an PictureBox in my form when the mouse hovered over another control. I then wanted to use a separate event for when the mouse left the control. This event would remove the displayed PictureBox from controls. However, because my events are private subs, I can't directly access the name of the control in the latter event. A solution to this would be a method that removes the most recently added control. If no such method exists, or there is an alternative way of approaching this problem, any help would be appreciated.
I tried simply using Controls.Remove(), but this requires a parameter. The name of the control as a string did not work either, as the parameter must be a control itself.
Private Sub Tile_MouseEnter(Sender As Object, e As EventArgs)
Dim CloseUpPic As New PictureBox With {Properties}
CloseUpPic.Image = Sender.Image
Controls.Add(CloseUpPic)
Refresh()
End Sub
Private Sub Tile_MouseLeave(Sender As Object, e As EventArgs)
Me.Controls.Remove()
End Sub
The program won't compile due to .Remove() needing a parameter
I expected for the Control to be created and displayed when the mouse entered the tile, and to cease to exist when the mouse left the tile.
For future reference, controls have Tag property which allows you to store whatever you like. In this case, you can store a reference to the newly created PictureBox. Furthermore, the "Sender" parameter tells you which control was the source of the event. You can cast sender to a control, then store the reference. Then, in the leave event, you can cast sender to a control, cast the .Tag to a control, and finally remove it:
Private Sub Tile_MouseEnter(Sender As Object, e As EventArgs)
Dim ctl As Control = DirectCast(Sender, Control)
Dim CloseUpPic As New PictureBox With {Properties}
CloseUpPic.Image = Sender.Image
Controls.Add(CloseUpPic)
ctl.Tag = CloseUpPic
Refresh()
End Sub
Private Sub Tile_MouseLeave(Sender As Object, e As EventArgs)
Dim ctl As Control = DirectCast(Sender, Control)
Dim ctlToRemove As Control = DirectCast(ctl.Tag, Control)
Me.Controls.Remove(ctlToRemove)
End Sub
I ended up using the following code to solve my issue:
For Each Closeup In Controls.OfType(Of CloseUp)
Controls.Remove(Closeup)
Next
I created a new class of my own called Closeup, that inherits PictureBox. I then looped through each Closeup in controls (There was only one but this code works for multiple controls), and removed them.

Basic solution for adding list item to combo box via code on load

What I need:
I was a basic equivalent of a select box i.e. a combobox of dropdown list style (that preferably doesn't allow text input).
I need to add the list items by code rather than the property box.
What I have:
Private Sub Form_Load ()
ComboStaffMember.AddItem "John Murphy"
End Sub
...produces "...add item is not a member of system.windows.forms.comboxbox".
Private Sub Form_Load ()
ComboStaffMember.Items.Add("John Murphy")
End Sub
...produces no result.
My question:
Why is the item not adding? The form name is FrmStaffLogIn and it's in Form1.vb. Should Form_Load correspond to either of these or is my code incorrect elsewhere?
Try to put combo add statement in following format in form load event :
Private Sub Form_Load ()
Me.ComboStaffMember.Items.Add(New DictionaryEntry("Text to be displayed", 1))
End Sub
Are you sure your code line ComboStaffMember.Items.Add("John Murphy") doesn't work? it should work just fine.
The Add() method On Item collection expects object parameter and string as well can be passed as argument to it. Like below [C# code sample]:
this.comboBox1.Items.AddRange(
new string[] {"SomeText","SomeOtherText","LastText"});
Also, you probably don't see any item cause you haven't set a default selected item. Just expand the dropdown and you will see the items. To set the default selected item
this.comboBox1.SelectedIndex = 0;
Working code:
Private Sub FrmIdentCust_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboStaffMember.Items.Add("John Murphy")
End Sub
I was missing (sender As Object, e As EventArgs) Handles MyBase.Load.

How to Dynamically Update Label with TextBox As Input Changes

I have a spelling application that I am building in VB.Net, where I have a textbox receiving simple input (spelling words), and a label which will show the output. What I want to accomplish is when I enter something in the textbox, I can see it in my label - as I am typing into the textbox.
I will admit that I don't know what I'm doing, as I've never tried this before, so I don't know to begin in terms of setting up what I need to do. I know that I'll need some variable to hold my String input, and will probably need some type of loop, but beyond that, I am lost. The only other example is in C#, and doesn't help me any.
Can anyone give me a simple model to work off of, so I can put the approach into memory? For now, all I have is code stub from my TextChanged event handler:
Private Sub txtSpell_TextChanged(sender As Object, e As EventArgs) Handles txtSpell.TextChanged
'Set variables to hold values.
Dim someText As String
'Connect the label and textbox.
lblShowInput.Text = txtWordInput.Text
'Process loop to populate the label from textbox input.
for '(This is where I am lost on the approach)
End Sub
I know that I'll need some variable to hold my String input, and will
probably need some type of loop
I don't think you'll need a loop, or the variable to hold the value. You almost have it:
Private Sub txtSpell_TextChanged(sender As Object, e As EventArgs) Handles txtSpell.TextChanged
'Connect the label and textbox.
lblShowInput.Text = txtSpell.Text
End Sub
In the code you provided, you are referencing an object named txtWordInput inside your txtSpell text changed event handler. If you are entering the text in the txtWordInput input, you'll want to handle this in the txtWordInput textChanged event handler:
Private Sub txtWordInput_TextChanged(sender As Object, e As EventArgs) Handles txtWordInput.TextChanged
'Connect the label and textbox.
lblShowInput.Text = txtWordInput.Text
End Sub
Follow-up:
The TextChanged event is the correct event for this.
In your code, you are assigning lblShowInput.Text to txtWordInput.Text, but in the txtSpell TextChanged event handler.
You want to be in the TextChanged event handler for whatever TextBox you would like to use to update the label, as the text is changing.
To give a better example, I have created a simple Winforms VB application that has only a textbox named InputTextBox and a label named Output Label.
The Form:
The Code:
Public Class Form1
Private Sub InputTextBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles InputTextBox.TextChanged
OutputLabel.Text = InputTextBox.Text
End Sub
End Class
Explanation:
InputTextBox_TextChanged is the method name generated by Visual Studio for our event handler
Handles InputTextBox.TextChanged ties the method to an actual event it is handling.
When the InputTextBox text property is changed (typically by user input), whatever we have in our InputTextBox_TextChanged Sub will execute. In this case, I am assigning the Text of OutputLabel to the Text of the InputTextBox
Output:
Resources:
I've uploaded this simple demo to GitHub if you'd like a closer look.
Take a look at the TextChanged documentation

Dynamically adding and using types in vb.net

I am making a program which dynamically adds objects, such as a button or a checkbox to a form.
For each instance, a handler is added so a certain function is called for the Click event of each object.
Now, when that handler is called, how can I manipulate the object that fired off the Click event? The Sender object is useless here as I cannot change the location, text, parent, nothing at all.
Since the objects are being created dynamically, their instance name is unfortunately always going to be the same, therefore I cannot simply do things like button1.Text = "Button 1".
I really do not want to create a new subroutine for every type since the actions that would be performed are the same...so how can I manipulate these objects?
There are, at last count, 27 different object types which are being manipulated, and which I want to be manipulated by a single sub.
Thanks for the help!
It sounds like the sender is what you want, as this will be the object that fired off the Click event. You just need to figure out a way to cast it to the required type.
If you are just manipulating location, text and parent, then casting to Control will be enough:
Dim c As Control = CType(sender, Control)
Otherwise you will need to cast to the specific type which means you will need different routines per type.
Another option is to turn on late binding, which I believe in VB is Option Strict Off. Then you can refer to control properties even without the casting -- .NET will look for the property at runtime (and, be warned, will throw an exception if the property's not there).
Cast the sender to Control & you can do what you want (all your objects are Controls right?)
If you know the type of input that called the handler, then you can use typecasting to solve your issue:
Sub General_OnClick(ByVal sender As Object, ByVal e As EventArgs)
Dim b As Button = sender
b.Text = "Hello World!"
End Sub
If you don't, which you don't seem you, you might try casting to Control instead, this may give you enough control, depending on what you need to do. If not, you can always do something like:
Sub General_OnClick(ByVal sender As Object, ByVal e As EventArgs)
If TypeOf sender Is Button Then
Dim b As Button = sender
b.Text = "Hello World!"
Else If TypeOf sender Is TextBox Then
Dim tb As TextBox = sender
tb.Text = "Goodbye cruel world!"
End If
End Sub
EDIT: Updated to translate into VB.Net