Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Loop through richtextbox

Hey there,

Let's see if I can explain this properly.

Basically I have a richtextbox connected with a webbrowser. Each line navigates to a new webpage but I want it to loop.
Right now, each button click is a new line and thus, new page. However, I want it that by 1 press on the button all pages get visited, preferably with a small delay in between.

Current code:
Code
WebBrowser1.Navigate(RichTextBox1.Lines(i))
i = i + 1

Please excuse me, I'm a big noob if it comes to VB.net but curious to learn.
  • Look into the basic principles of for loops, while loops and loops in general.
  • Instead of using a WebBrowser component, you could opt to utilize something that's a little less performance hungry
  • You should make sure that your variable naming is consistent and meaningful. As it stands, the name RichTextBox1 doesn't really mean anything.
  • A shorter way to increment a variable with 1 is to use "+= 1"
  • Why would you want to introduce an artificial delay?

Resources regarding loops in VB.NET:
https://docs.microsoft.com/en-us/dotnet/...structures
[Image: vp6iypF.gif]
(May 1, 2020 01:58 PM)MavenGradle Wrote:  
  • Look into the basic principles of for loops, while loops and loops in general.
  • Instead of using a WebBrowser component, you could opt to utilize something that's a little less performance hungry
  • You should make sure that your variable naming is consistent and meaningful. As it stands, the name RichTextBox1 doesn't really mean anything.
  • A shorter way to increment a variable with 1 is to use "+= 1"
  • Why would you want to introduce an artificial delay?

Resources regarding loops in VB.NET:
https://docs.microsoft.com/en-us/dotnet/...structures

Regarding the webbrowser, I need to use it to sign into a website. Considering the website uses randomised ids/names in their elements, I thought it'd be easier to make it like this.

I have altered the increment to the variable, thanks for the tip.

The artificial delay needs to be there because I want it to browse each X seconds.
(May 1, 2020 02:04 PM)Perileos Wrote:  Regarding the webbrowser, I need to use it to sign into a website. Considering the website uses randomised ids/names in their elements, I thought it'd be easier to make it like this.

You should probably be using something like Selenium from what I understand:
https://www.selenium.dev/
https://www.youtube.com/watch?v=8Ok7AoZceEE
https://www.toolsqa.com/selenium-c-sharp/

You should also look into the possibility of making use of the API system of whatever service it is that you're trying to automate something for. RESTful-API's are available for most major sites (e.g. Facebook has them, Reddit has them, etc.). Using the API system has advantages such as direct communication with the backend server, instead of having to load the website and essentially downloading a bunch of files that you don't really need. Additionally, error handling can be performed in a much cleaner way, reducing the complexity of your application in general.

(May 1, 2020 02:04 PM)Perileos Wrote:  The artificial delay needs to be there because I want it to browse each X seconds.

That's not really a valid reason in my opinion. You are artificially slowing something down and making your application more complex. The only reason why I could see you doing this would be to circumvent some kind of spam filter, in which case you're doing something that you're not supposed to be doing. Regardless of that, it is never a good idea to introduce delays by using something like:
Code
Thread.Sleep(x)

This is just bad practice and the only time it could arguable be useful is when looking at unit tests.
[Image: vp6iypF.gif]
(May 1, 2020 02:12 PM)MavenGradle Wrote:  You should probably be using something like Selenium from what I understand:
https://www.selenium.dev/
https://www.youtube.com/watch?v=8Ok7AoZceEE
https://www.toolsqa.com/selenium-c-sharp/

You should also look into the possibility of making use of the API system of whatever service it is that you're trying to automate something for. RESTful-API's are available for most major sites (e.g. Facebook has them, Reddit has them, etc.). Using the API system has advantages such as direct communication with the backend server, instead of having to load the website and essentially downloading a bunch of files that you don't really need. Additionally,error handling can be performed in a much cleaner way, reducing the complexity of your application in general.


That's not really a valid reason in my opinion. You are artificially slowing something down and making your application more complex. The only reason why I could see you doing this would be to circumvent some kind of spam filter, in which case you're doing something that you're not supposed to be doing. Regardless of that, it is never a good idea to introduce delays by using something like:
Code
Thread.Sleep(x)

This is just bad practice and the only time it could arguable be useful is when looking at unit tests.

I'm going to research Selenium more.

Basically I intend to scrape information from a website and if that information is present I want the program to do an action. Not necessarily meant for spam. I'm a noob when it comes to programs hence why I can't/don't know how to use an api.
(May 1, 2020 02:18 PM)Perileos Wrote:  I'm going to research Selenium more.

Basically I intend to scrape information from a website and if that information is present I want the program to do an action. Not necessarily meant for spam. I'm a noob when it comes to programs hence why I can't/don't know how to use an api.

Feel free to ask any questions you have here. Using an API system can be as simple as calling using a WebRequest to make a HTTP POST call with (serialized) JSON data. You could make use of Json.NET to serialize .NET objects into JavaScript Object Notation (JSON).

In any case, using the API approach will save you a lot of system resources and speed up your development process in general when you get the hang of it and it should always be your first choice of action when looking at automation in regards to webdevelopment. It's very useful to look into those things using the links I've included throughout this post. Happy learning!
[Image: vp6iypF.gif]
(May 1, 2020 02:25 PM)MavenGradle Wrote:  Feel free to ask any questions you have here. Using an API system can be as simple as calling using a WebRequest to make a HTTP POST call with (serialized) JSON data. You could make use of Json.NET to serialize .NET objects into JavaScript Object Notation (JSON).

In any case, using the API approach will save you a lot of system resources and speed up your development process in general when you get the hang of it and it should always be your first choice of action when looking at automation in regards to webdevelopment. It's very useful to look into those things  using the links I've included throughout this post. Happy learning!

Thank you again. You're a very helpful member.
I'm trying to learn from this instead of hiring someone.
(May 1, 2020 02:27 PM)Perileos Wrote:  Thank you again. You're a very helpful member.
I'm trying to learn from this instead of hiring someone.

Don't ever listen to MavenGradle, this guy is known to not help people he's here to spew a bunch of shít he doesn't know and stroke his ego.

Here's the solution to your question OP.

Code
       For i As Integer = 0 To RichTextBox1.Lines.Count
           WebBrowser1.Navigate(RichTextBox1.Lines(i))
           Threading.Thread.Sleep(1000)
       Next

   End Sub

Code Explained:
Spoiler (Click to View)
(May 2, 2020 03:25 PM)Who Soup Wrote:  Don't ever listen to MavenGradle, this guy is known to not help people he's here to spew a bunch of shít he doesn't know and stroke his ego.

Here's the solution to your question OP.

Code
       For i As Integer = 0 To RichTextBox1.Lines.Count
           WebBrowser1.Navigate(RichTextBox1.Lines(i))
           Threading.Thread.Sleep(1000)
       Next

   End Sub

Code Explained:
Spoiler (Click to View)

Cheers.
That was something I was looking for. It's something I understand hahaha.

Thanks man! 💯
actually in this case it is better to use foreach loop instead of for loop especially when it comes to the larger amount of data contained in RichTextBox.In for loop you accessing lines on every iteration,so i recommend foreach loop in this case unless you just want to work with indexes.
buy me a coffee eth:0x88621A52aa5Eb3b6503628369687111810186c58
(May 2, 2020 03:25 PM)Who Soup Wrote:  Don't ever listen to MavenGradle, this guy is known to not help people he's here to spew a bunch of shít he doesn't know and stroke his ego.

Here's the solution to your question OP.

Code
       For i As Integer = 0 To RichTextBox1.Lines.Count
           WebBrowser1.Navigate(RichTextBox1.Lines(i))
           Threading.Thread.Sleep(1000)
       Next

   End Sub

Code Explained:
Spoiler (Click to View)

As pointed out, the code you shared is exactly what you're supposed to avoid doing. You do not make use of any viable variable names, you have no error handling and you make an entire application unresponsive for 1000 milliseconds on every iteration, not to mention that you make use of a component that utilizes Internet Explorer in its core, of all things. A component that, by the way, has a very clear remark on the official documentation that it is resource heavy.

All of this garbage could have been avoided if you simply wrote clean code.

Unfortunately, some people who lack the required skill for writing decent code or simple comprehension skills will opt for a solution like this but if anyone's genuinly interested in learning proper programming techniques, this is a prime example on how not to write code and how to lose your job as a programmer in no time. That is, if you work for a respectable company that actually verifies (which is called a code review) the code its employees produce.

The reasoning behind this has been explained in this and in previous posts.
[Image: vp6iypF.gif]
(May 2, 2020 08:17 PM)MavenGradle Wrote:  As pointed out, the code you shared is exactly what you're supposed to avoid doing. You do not make use of any viable variable names, you have noerror handling and you make an entire application unresponsive for 1000 milliseconds, not to mention that you make use of a component that utilizes Internet Explorer in its core, of all things. A component that, by the way, has a very clear remark on the official documentation that it is resource heavy.

All of this garbage could have been avoided if you simply wrote clean code.

Unfortunately, some people who lack the required skill for writing decent code or simple comprehension skills will opt for a solution like this but if anyone's genuinly interested in learning proper programming techniques, this is a prime example on how not to write code and how to lose your job as a programmer in no time. That is, if you work for a respectable company that actually verifies (which is called a code review) the code its employees produce.

The reasoning behind this has been explained in this and in previous posts.

Your posts seriously bring down the quality of this forum it's honestly a shame that you're not banned yet. People will take notice that your posts do nothing for them while mine provides a precise working answer.  If you absolutely can't help being an ass hole at least put more effort into your posts and post working code. No one wants to be bombarded with links that don't even apply to what's being asked.

The fact that you write an entire essay in your post but can't write 5 lines of code says everything about you and your credibility.
(May 2, 2020 08:55 PM)Who Soup Wrote:  Your posts seriously bring down the quality of this forum it's honestly a shame that you're not banned yet. People will take notice that your posts do nothing for them while mine provides a precise working answer.  If you absolutely can't help being an ass hole at least put more effort into your posts and post working code. No one wants to be bombarded with links that don't even apply to what's being asked.

The fact that you write an entire essay in your post but can't write 5 lines of code says everything about you and your credibility.

Your personal inquiries and insults are laughable and besides the point.

Your code provides the OP with a solution that is not appropriate, as I've already pointed out.

Instead of this post you should have been posting a proper solution to the question at hand or at the very least correct your previous post.

If anything brings down the quality of this subforum, it would be the code you've posted above, but apparantly you lack the insight to realize that it is garbage code. If you took the time of day to read, you'd understand exactly why it is garbage code.
[Image: vp6iypF.gif]
(May 2, 2020 03:25 PM)Who Soup Wrote:  Don't ever listen to MavenGradle, this guy is known to not help people he's here to spew a bunch of shít he doesn't know and stroke his ego.

LMFAO, when i started with React, he was the only one who helped me thru the process. So saying that he is "known to not help people" is just irrelevant. I would've tell you this or that, you're just not worth my time.
Жизнь это не те дни, что прошли, а те, что запомнились
(May 2, 2020 08:17 PM)MavenGradle Wrote:  As pointed out, the code you shared is exactly what you're supposed to avoid doing. You do not make use of any viable variable names, you have noerror handling and you make an entire application unresponsive for 1000 milliseconds on every iteration, not to mention that you make use of a component that utilizes Internet Explorer in its core, of all things. A component that, by the way, has a very clear remark on the official documentation that it is resource heavy.

All of this garbage could have been avoided if you simply wrote clean code.

Unfortunately, some people who lack the required skill for writing decent code or simple comprehension skills will opt for a solution like this but if anyone's genuinly interested in learning proper programming techniques, this is a prime example on how not to write code and how to lose your job as a programmer in no time. That is, if you work for a respectable company that actually verifies (which is called a code review) the code its employees produce.

The reasoning behind this has been explained in this and in previous posts.

Not everyone can write the correct thing on their first try man. Your code evolves as you get better.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by Iera NamhorunJun 26, 2019 04:28 AM 4 236 Jun 26, 2019 04:28 AM
Last Post: Iera Namhorun
[Java] by ohxerxeFeb 4, 2018 03:26 PM 3 302 Feb 4, 2018 03:26 PM
Last Post: matt
[.NET] by BapeZOct 1, 2017 11:44 AM 2 150 Oct 1, 2017 11:44 AM
Last Post: Barret