neodammer Posted August 11, 2004 Posted August 11, 2004 Here is a question that bugs me because as easy as it seems in virtualy every text program I dont believe there is a simple VB.net command for "find this text" and "replace it with this text". or is there? For example I want to take this string "Hello the # 24 test in running" and upon the click of a button swap out the 24 for say 25.. is this easier than it sounds? Quote Enzin Research and Development
Denaes Posted August 11, 2004 Posted August 11, 2004 [/color][/size] [size=2][color=#0000ff]Dim[/color][/size][size=2] myString [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String[/color][/size][size=2] = "Stuff" myString.Replace(OldText, NewText) So you might have it say "Hello, this is button btnNumber." and use myString.Replace("btnNumber", strButtonNumber) Quote
Arch4ngel Posted August 11, 2004 Posted August 11, 2004 You might also take a look at RegularExpression. It's a bit hard to learn at first ... but it worth the work. It can make validation, replacement and a lot of string operation. Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
Ontani Posted August 11, 2004 Posted August 11, 2004 Here is a question that bugs me because as easy as it seems in virtualy every text program I dont believe there is a simple VB.net command for "find this text" and "replace it with this text". or is there? For example I want to take this string "Hello the # 24 test in running" and upon the click of a button swap out the 24 for say 25.. is this easier than it sounds? Try This Dim oldNumber As Integer = 24 Dim newNumber As Integer = 25 Dim oldString As String = "Hello the # " & oldNumber & " test in running" Dim newString As String = Replace(oldString, oldNumber, newNumber) TextBox1.Text = newString Greetz Quote www.purevision.be :: www.devpoint.be
Denaes Posted August 11, 2004 Posted August 11, 2004 Try This Dim oldNumber As Integer = 24 Dim newNumber As Integer = 25 Dim oldString As String = "Hello the # " & oldNumber & " test in running" Dim newString As String = Replace(oldString, oldNumber, newNumber) TextBox1.Text = newString Greetz Those are poor programming habbits that can result in errors down the line. Always insure that Option Strict is turned on (it's in your project properties). This won't allow to use an integer as a string. your code would then look like this: Dim oldNumber As Integer = 24 Dim newNumber As Integer = 25 Dim oldString As String = "Hello the # " & oldNumber.toString() & " test in running" Dim newString As String = Replace(oldString, oldNumber.toString(), newNumber.toString()) TextBox1.Text = newString Next, the method I used above (String.Replace())is the .Net method. I'm not sure what Replace is, probobly a VB compatability wrapper. doing this the .net way like you did would be like this: Dim oldNumber As Integer = 24 Dim newNumber As Integer = 25 Dim oldString As String = "Hello the # " & oldNumber.toString() & " test in running" Dim newString As String = oldString.Replace(oldNumber.toString(), newNumber.toString()) TextBox1.Text = newString Not trying to be mean, just don't want to pass around sloppy habbits like not using Option Strict. If you don't use it, a compile error will never appear and down the road - especially on a more complex project - errors can be introduced. I think Int -> Strings are always ok, but there are other conversions that VB will "let you get away with" that can cause a LOT of problems. Quote
Ontani Posted August 11, 2004 Posted August 11, 2004 hey so when i enable this option what does it do? i'm a realy newby on programming and have never realy used the toString() function i'm more someone that programs for fun and just dimension all as strings :) never learn to program like in lessons. learned myself everything Greetz Quote www.purevision.be :: www.devpoint.be
neodammer Posted August 11, 2004 Author Posted August 11, 2004 Thanks, my goal is to be able to just click a button and it will increase the # of whatever I choose in the string. I do have one off topic question which is probably too simple to start a thread over: Do I need to run a shell command or is there something built into the webbrowser control that will just open up IE in a new window with an attached string url? Quote Enzin Research and Development
Denaes Posted August 11, 2004 Posted August 11, 2004 hey so when i enable this option what does it do? i'm a realy newby on programming and have never realy used the toString() function i'm more someone that programs for fun and just dimension all as strings :) never learn to program like in lessons. learned myself everything Greetz The only beef I have with VB is that it lets you convert things (string to int, int to string, long to int, etc) automatically. If you put Option Strict = On, it won't let you do this automatically. Like you did, you had 3 arguments (String, String, String) and you passed in (String, Integer, Integer). That isn't allowed in most other programming languages and won't be allowed if you turn Option Strict On. But Integers and Strings are pretty straight forward. an int, ie a whole number, should always have a string equivilent. 19 = "19" right? how about the opposite? [size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] strNumber [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String[/color][/size][size=2] = "19" [/size][size=2][color=#0000ff]Dim[/color][/size][size=2] intNumber [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Integer[/color][/size][size=2] = strNumber[/size] With Option Strict Off, this is allowed and works. 19 can become an integer with no problem. So what's the big deal? Something goes funny in your math and this is what you end up with: [size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] strNumber [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String[/color][/size][size=2] = "19.5" [/size][size=2][color=#0000ff]Dim[/color][/size][size=2] intNumber [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Integer[/color][/size][size=2] = strNumber MessageBox.Show(intNumber) [/size] I added the messagebox to show what intNumber is. So what do you think the messagebox would say? 20. It rounded it up for you. If you needed 19.5, you just corrupted your data. How about if your string gets messed up with a REAL string? [/size] [size=2][size=2][color=#0000ff]Dim[/color][/size][size=2] strNumber [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String[/color][/size][size=2] = "pie" [/size][size=2][color=#0000ff]Dim[/color][/size][size=2] intNumber [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Integer[/color][/size][size=2] = strNumber MessageBox.Show(intNumber) [/size][/size][size=2] This throws an exception and crashes your program. You can't convert "Pie" to an integer. Well actually you could if you wanted to, but VB can't. You could choose to get the ASCII values of each letter and add them together or have it as a code. But YOU have to do it. Can't trust VB to do it. Lets try with two numbers: [/size] [size=2][size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] lngNumber [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Long[/color][/size][size=2] = 28.99 [/size][size=2][color=#0000ff]Dim[/color][/size][size=2] intNumber [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Integer[/color][/size][size=2] = lngNumber MessageBox.Show(intNumber) [/size][/size][size=2] I bet you can guess what might pop up. Yep, it rounded up again. Now if you're moving numbers back and forth, dividing them then rounding up, then dividing again and you'll be REALLY far off. Can you get away without Option Strict? Yeah, if you know your types and which datatypes convert without a hitch. But it's a better habbit to throw Option Strict on, just in case. What happens if you make strName into a long (say it's a database app and instead of the name, you're now referencing the index of the name). strName looks like a string still, but it's not. I'd rather have VB throw a compile error saying "Hey wait, you're trying to use a Long as a string" then to find out 2 weeks down the line and scratch my head trying to figure it out. :D In this I speak from experience. about 11 forms, 5 classes and 2 databases into an app I realized my problem and turned Option Strict On. I spent 4 hours fixing errors. I think I had like 130 type errors :) Quote
Denaes Posted August 11, 2004 Posted August 11, 2004 Thanks' date=' my goal is to be able to just click a button and it will increase the # of whatever I choose in the string. I do have one off topic question which is probably too simple to start a thread over: Do I need to run a shell command or is there something built into the webbrowser control that will just open up IE in a new window with an attached string url?[/quote'] [size=2]System.Diagnostics.Process.Start("C:\Program Files\Internet Explorer\IEXPLORE.EXE", [url="http://www.aol.com/"]www.aol.com[/url]) stupid forum :P the link www.aol.com isn't supposed to be a naked link but "ww.aol.com" - a string inside of quotes... but of course with 3 w's, not 2. That's System.Diagnostics.Proscess.Start(ApplicationPathAndName as String, Arguments as String). In this case I provided the path for Internet Explorer (on XP home) and the argument was the webpage. [/size] Quote
*Experts* Nerseus Posted August 11, 2004 *Experts* Posted August 11, 2004 Also note that the Replace method only returns the modified string, it does not update the original. So the following will NOT work: Dim myString AsString = "Stuff" myString.Replace(OldText, NewText) You should do this instead: Dim myString AsString = "Stuff" myString = myString.Replace(OldText, NewText) -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Denaes Posted August 11, 2004 Posted August 11, 2004 Also note that the Replace method only returns the modified string, it does not update the original. So the following will NOT work: Dim myString AsString = "Stuff" myString.Replace(OldText, NewText) You should do this instead: Dim myString AsString = "Stuff" myString = myString.Replace(OldText, NewText) -ner Whoops, left that part out. I was using it like that so that "btnNumber" or whatever would always be replaced. I forgot the string on the other side of the equation. sorry.:o Quote
neodammer Posted August 12, 2004 Author Posted August 12, 2004 Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Button2 As System.Windows.Forms.Button Friend WithEvents Button3 As System.Windows.Forms.Button Friend WithEvents TextBox2 As System.Windows.Forms.TextBox Friend WithEvents Button4 As System.Windows.Forms.Button Friend WithEvents AxWebBrowser1 As AxSHDocVw.AxWebBrowser <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1)) Me.TextBox1 = New System.Windows.Forms.TextBox Me.Button1 = New System.Windows.Forms.Button Me.Button2 = New System.Windows.Forms.Button Me.Button3 = New System.Windows.Forms.Button Me.TextBox2 = New System.Windows.Forms.TextBox Me.Button4 = New System.Windows.Forms.Button Me.AxWebBrowser1 = New AxSHDocVw.AxWebBrowser CType(Me.AxWebBrowser1, System.ComponentModel.ISupportInitialize).BeginInit() Me.SuspendLayout() ' 'TextBox1 ' Me.TextBox1.Location = New System.Drawing.Point(64, 8) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(248, 20) Me.TextBox1.TabIndex = 0 Me.TextBox1.Text = "TYPE URL IN HERE" ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(56, 96) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(104, 32) Me.Button1.TabIndex = 1 Me.Button1.Text = "+ #" ' 'Button2 ' Me.Button2.Location = New System.Drawing.Point(176, 96) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(104, 32) Me.Button2.TabIndex = 2 Me.Button2.Text = "- #" ' 'Button3 ' Me.Button3.Location = New System.Drawing.Point(296, 96) Me.Button3.Name = "Button3" Me.Button3.Size = New System.Drawing.Size(104, 32) Me.Button3.TabIndex = 3 Me.Button3.Text = "Clear" ' 'TextBox2 ' Me.TextBox2.Location = New System.Drawing.Point(144, 40) Me.TextBox2.Name = "TextBox2" Me.TextBox2.Size = New System.Drawing.Size(96, 20) Me.TextBox2.TabIndex = 4 Me.TextBox2.Text = "# to change here" ' 'Button4 ' Me.Button4.Location = New System.Drawing.Point(64, 40) Me.Button4.Name = "Button4" Me.Button4.Size = New System.Drawing.Size(72, 24) Me.Button4.TabIndex = 5 Me.Button4.Text = "Set #" ' 'AxWebBrowser1 ' Me.AxWebBrowser1.Enabled = True Me.AxWebBrowser1.Location = New System.Drawing.Point(16, 152) Me.AxWebBrowser1.OcxState = CType(resources.GetObject("AxWebBrowser1.OcxState"), System.Windows.Forms.AxHost.State) Me.AxWebBrowser1.Size = New System.Drawing.Size(416, 288) Me.AxWebBrowser1.TabIndex = 6 ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(456, 469) Me.Controls.Add(Me.AxWebBrowser1) Me.Controls.Add(Me.Button4) Me.Controls.Add(Me.TextBox2) Me.Controls.Add(Me.Button3) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.TextBox1) Me.Name = "Form1" Me.Text = "Form1" CType(Me.AxWebBrowser1, System.ComponentModel.ISupportInitialize).EndInit() Me.ResumeLayout(False) End Sub #End Region Private oldNumber As Integer Private newNumber As Integer Private urls As String Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click urls = TextBox1.Text newNumber = oldNumber + 1 urls.Replace(oldNumber, newNumber) TextBox1.Text = urls AxWebBrowser1.Navigate(urls) End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click oldNumber = TextBox2.Text Button4.Enabled() = False End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click urls = TextBox1.Text newNumber = oldNumber - 1 urls.Replace(oldNumber, newNumber) TextBox1.Text = urls AxWebBrowser1.Navigate(urls) End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click urls = "" TextBox1.Text = "" TextBox2.Text = "" newNumber = 0 oldNumber = 0 Button4.Enabled() = True End Sub End Class As you can see im trying to take a typed in URL and then the user tells the program what # inside the URL to either add 1 or subtract 1 to then execute that new one in the browser. Any suggestions why its not working? lol ill attach the project.Webapp.zip Quote Enzin Research and Development
Denaes Posted August 12, 2004 Posted August 12, 2004 As you can see im trying to take a typed in URL and then the user tells the program what # inside the URL to either add 1 or subtract 1 to then execute that new one in the browser. Any suggestions why its not working? lol ill attach the project. When you have this much code, don't post what is in the Windows Form Designers Region, or anything else like that. First off I fixed some of the problems. I turned Option Strict on, fixed the Types. I have no clue what you're trying to do with the #. I put in www.aol.com and 1 into # and hit add number and nothing. I clicked the # + or # - and it loaded AOL. I did the same for Yahoo and #2 and yahoo came up. I don't see what the app is supposed to do really. Is this like making a "favorites"? what if I put in "Pie" or "Turkey" into the textbox. Are you allowing for that? How about if I add one site and type in 4 and another site and type in 500. Whats that do? Quote
neodammer Posted August 12, 2004 Author Posted August 12, 2004 I figured it out lol.. Quote Enzin Research and Development
Denaes Posted August 12, 2004 Posted August 12, 2004 I figured it out lol.. I have it where if I type a number in the address and in the box it incriments it. Example: www3.yahoo.com Number I type in: 3 I can increase this to www4.yahoo.com or lower it to www2.yahoo.com also if you have w3w3w3.ya3hoo3.com and incriment it, it becomes: w4w4w4.ya4hoo4.com is this what you're trying to do? Quote
neodammer Posted August 12, 2004 Author Posted August 12, 2004 Yeah but here is my current dilema: using this code Private oldNumber As Integer Private newNumber As Integer Private urls As String Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click urls = TextBox1.Text newNumber = oldNumber + 1 urls = urls.Replace(oldNumber, newNumber) oldNumber = oldNumber + 1 TextBox1.Text = urls System.Diagnostics.Process.Start("C:\Program Files\Internet Explorer\IEXPLORE.EXE", urls) End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click oldNumber = TextBox2.Text Button4.Enabled() = False End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click urls = TextBox1.Text newNumber = oldNumber - 1 urls = urls.Replace(oldNumber, newNumber) oldNumber = oldNumber - 1 TextBox1.Text = urls System.Diagnostics.Process.Start("C:\Program Files\Internet Explorer\IEXPLORE.EXE", urls) End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click urls = "" TextBox1.Text = "" TextBox2.Text = "" newNumber = 0 oldNumber = 0 Button4.Enabled() = True End Sub if the url has something like 0002 in it... I cant really say 00 in the box..because by default VB takes out the extra zero's and if i hit + it was just end up 3 not 0003 which is an invalid page.. of course another bug is what you stated.. im hoping the url's only have that exact # once in the url if there is more like www3.yah3.com im in trouble lol Quote Enzin Research and Development
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.