textbox1 + textbox2 in Textbox3 - struct

Ok so what I am trying to do is create a program that will do this:
Text Box 1: ASDFGHJK
Text Box 2: ZXCVBNMO
Result Text Box: AZSXDCFVGBHNJMKO
In a windows application using text boxes and buttons. Can somewhere give me a starting point on this? I don't even know how to research how to do this. My online teacher sucks so any help is appreciated. I am supposed to make this program in a struct and in a class. If you could help me figure out the difference, that would be awesome too! TIA

It's been 11 days since you posted this question so I'm hoping you did get more answers from your teacher. If not, I can't do your homework for you but I can give you some hints.
The process you described above is "string concatenation" (a String is a variable with characters in it)
You tagged your question with windows-applications and basic so I assume you're using Visual Studio to write a VB.NET Windows Forms Application
Here's an example of concatenation
' Letters ("string" variables)
Dim a = "abc"
Dim b = "def"
Dim c = a & b ' concatenation, c = "abcdef"
The other aspects you mentioned were struct and class. Structures and classes are two different ways to make complex variables, where a variable can have multiple properties. Do searches on these terms for more information (e.g. "vb.net strucure class"). For example, I could create a Structure that has properties corresponding to the three variables listed above like this:
Public Structure ExampleStructure
Public a As String
Public b As String
Public c As String
End Structure
Then, to use it in my concatenation example it would look like:
Dim s As New ExampleStructure
s.a = "abc"
s.b = "def"
s.c = s.a & s.b ' concatenation, s.c = "abcdef"
As far as getting started with a Windows Forms application with a textbox and a button, check out http://www.tutorialspoint.com/vb.net/vb.net_textbox.htm

Related

Use String text as Code in Visual Basic for Excel

For various reasons, I need to concatenate a text of the form [NAME].Value by changing the value of NAME to an inputbox entry.
Something like this:
Sub example()
data_in = InputBox("Give me something: ")
mystring1 = "[" & data_in & "].Value"
a = Evaluate(mystring1) 'I know this is wrong, but I don't know how to do so.
End Sub
I know it can be done in other ways, and the example in which I want to use this code is not exactly this one, and while it can be done in several ways here, in the original code it can only be done this way.
I want, based on the input in the imputbox, to concatenate the string in whatever way, and subsequently cast that string as code to store the value in another variable, to be used later in the code.
I am not able to get VBA to read the string text as code. I have seen that there is a way that consists of creating a macro from this first macro, execute it, and then delete the recently created macro. The problem with this solution is that doing that I can't save the variable when returning to the initial macro (I don't want to use global variables).
Surely there must be a way?
Thank you very much.
EDIT: The code above returns Error 2015
In order to use a string as if it was code, you can use the evaluate function (exists in most languages)
The official documentation mentions this example:
[a1].Value = 25
Evaluate("A1").Value = 25
trigVariable = [SIN(45)]
trigVariable = Evaluate("SIN(45)")
Set firstCellInSheet = Workbooks("BOOK1.XLS").Sheets(4).[A1]
Set firstCellInSheet = _
Workbooks("BOOK1.XLS").Sheets(4).Evaluate("A1")
I have figured out the easiest way to do it, sorry for posting the question so soon.
Thanks to #Andreas for the solution. I'll write it here in case than could be useful to someone.
Sub example()
data_in = InputBox("Give me something: ")
a = Range(data_in).Value
Debug.Print a
End Sub
In the end the simplest thing is the last thing you try...

How do I extract a series of numbers along with a single letter followed by another series of numbers?

The problem that I'm facing is that I have an entire column that has text separated by _ that contains pixel size that I want to be able to extract but currently can't. For example:
A
Example_Number_320x50_fifty_five
Example_Number_One_300x250_hundred
Example_Number_two_fifty_728x49
I have tried using Substitute function to grab the numbers which works but only grabs the numbers when I need something like: 320x50 instead I'm getting 0, as I'm not sure how to exactly extract something like this. If it was consistent I could easily do LEFT or RIGHT formula's to grab it but as you can see the data varies.
The result that I'm looking for is something along the lines of:
A | B
Example_Number_320x50_fifty_five | 320x50
Example_Number_One_300x250_hundred | 300x200
Example_Number_two_fifty_728x49 | 728x49
Any help would be much appreciated! If any further clarification is needed please let me know and I'll try to explain as best as I can!
-Maykid
I would probably use a Regular Expressions UDF to accomplish this.
First, open up the VBE by pressing Alt + F11.
Right-Click on VBAProject > Insert > Module
Then you can paste the following code in your module:
Option Explicit
Public Function getPixelDim(RawTextValue As String) As String
With CreateObject("VBScript.RegExp")
.Pattern = "\d+x\d+"
If .Test(RawTextValue) Then
getPixelDim = .Execute(RawTextValue)(0)
End If
End With
End Function
Back to your worksheet, you would use the following formula:
=getPixelDim(A1)
Looking at the pattern \d+x\d+, an escaped d (\d) refers to any digit, a + means one or more of \d, and the x is just a literal letter x. This is the pattern you want to capture as your function's return value.
Gosh, K Davis was just so fast! Here's an alternate method with similar concept.
Create a module and create a user defined function like so.
Public Function GetPixels(mycell As Range) As String
Dim Splitter As Variant
Dim ReturnValue As String
Splitter = Split(mycell.Text, "_")
For i = 0 To UBound(Splitter)
If IsNumeric(Mid(Splitter(i), 1, 1)) Then
ReturnValue = Splitter(i)
Exit For
End If
Next
GetPixels = ReturnValue
End Function
In your excel sheet, type in B1 the formula =GetPixels(A1) and you will get 320x50.
How do you create a user defined function?
Developer tab
Use this URL to add Developer tab if you don't have it: https://www.addintools.com/documents/excel/how-to-add-developer-tab.html
Click on the highlighted areas to get to Visual Basic for Applications (VBA) window.
Create module
Click Insert > Module and then type in the code.
Use the user defined function
Note how the user defined function is called.

Multiple space delimiter split string

I have a string like
string = "computer prog <5spaces> data mining <5spaces> oops concept"
As we can see clearly computer prog, data mining etc., are one continuous string and the delimiter is 5 spaces between the strings " ".
I need to split based on this in vb.net - so far I tried regex.split which works but results in giving 2 empty strings additionally and it's tedious to remove those additional strings.
I also tried using the string.split method but again it's taking even single white space also delimiters.
Below are the tried options:
regex.split
string.split
None give me the required result. I am not sure what I need to use. I even tried the option of stringsplitoption.removesapceentry (something like that) to get the desired result inside the split method, but none worked.
Dim array_keyskills As String() = res.Split(" ".ToCharArray,StringSplitOptions.RemoveEmptyEntries)
system.Windows.MessageBox.Show(array_keyskills(2) & array_keyskills.Length & " key skills") 'Display
The following short program:
Module Module1
Sub Main()
Dim s = "computer prog data mining oops concept"
Dim parts = s.Split({" "}, StringSplitOptions.None)
For Each p In parts
Console.WriteLine(p)
Next
Console.ReadLine()
End Sub
End Module
outputs:
computer prog
data mining
oops concept
If your data does not work that way then you should examine it to find which whitespace characters are in it which appear to be spaces but are not.
This did the trick:
array_keyskills = System.Text.RegularExpressions.Regex.Split(res," ").Where(
Function(s) Not String.IsNullOrWhitespace(s)
).ToArray()

Can I use a string as a name in Visual Basic to change an objects properties?

My Idea is not too hard. I have a button, a string called "progname", a TextBox and three Progressbars.
When I enter a number into the TextBox and press the button, the following code run's through.
Dim progname As String
progname = "Progressbar" & TextBox1.Text
Now I have a string called "progname" with relevant value.For an example "Progressbar2".
What I want to achieve is write something like:
progname.Value += 1
Which can't be done, as "Value" is not a Member of "String". How can I do this?
Overall what I want, is to be able to select one of the three progressbars by typing one of the numbers 1-3 into the TextBox and then change that ones porperties.
Yes you can.
A basic example is this, which searches your form for controls with the name matching your string. It then changes the type to a ProgressBar so you can access all the methods ..
Dim progbar As ProgressBar = CType(Me.Controls.Find(progName, False)(0), ProgressBar)
progbar.Value += 1

New to Visual Basic

I'm completely new to visual basic and somewhat new to programming in general. I'm trying to teach myself how to use visual basic because I was told it was easier to learn. My question is how do I connect an execute button to a text box to calculate a simple math problem? Do I double click the button I want to make the execution and then put in the code? Can someone give me a simple sample code that when you enter a number in a text box and press the execute button, the number entered would be multiplied by another number like 2 or whatever and then be displayed in another box? I just need a way to understand how the coding works and connects the gui buttons and text boxes so I can understand it better. I'm good and learning on my own when I see how things work and then I can put things together and figure them out. Thanks
That's pretty much it...
...of course the devil is in the details. The main problem you'll run into in this scenario is that your TextBox holds a String, but you want to manipulate it as a Number. First you need to convert the string to a number, then do the math. This also allows you to determine if the String in the TextBox is not a number, such as if the user entered "Unicorns are real!" in it:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim x As Integer
Dim strInput As String = TextBox1.Text
If Integer.TryParse(strInput, x) Then
Dim result As Integer = x * 2
Label1.Text = x.ToString & " * 2 = " & result.ToString
Else
Label1.Text = "Invalid Integer: " & strInput
End If
End Sub
End Class

Resources