C#: How to copy the text on a webpage to a textbox?

Status
Not open for further replies.

kytro360

Member
Original poster
member
Joined
Sep 9, 2009
Posts
376
Reaction score
6
Points
75
Hey I am trying to save a certain piece of text on a page and display it in a textbox.

Here is what I am working with but its not working:

Code:
 Clipboard.Clear();
                webBrowser1.Focus();
                SendKeys.SendWait("^(c)");
                txtBoxEmail.Text = Clipboard.GetText();

Im also working with:

Code:
webBrowser1.Document.GetElementById("addyForm:addressSelect").Equals(txtBoxEmail.Text);
 

goku93

Senior
senior
member
Joined
Jun 13, 2011
Posts
465
Reaction score
81
Points
146
ok im no a c# guy, im vb.net guy :D but is almost the same
First what do you want to save?, you can use the webbrowser control and getelementby id or tagname
 

kytro360

Member
Original poster
member
Joined
Sep 9, 2009
Posts
376
Reaction score
6
Points
75
Well I want to copy the email that 10minutemail.com generates and copy it into a textbox. Whats the code for getelementbyid to save it? I tried (as you can see in my first post) but it didnt work.

You can post your code in VB.Net I can always convert it :)
 

goku93

Senior
senior
member
Joined
Jun 13, 2011
Posts
465
Reaction score
81
Points
146
Ah ok is pretty easy the code is this:
Code:
 Dim inputs As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
        For Each input As HtmlElement In inputs
            If input.GetAttribute("id") = "addyForm:addressSelect" Then
                textbox1.text = input.GetAttribute("value")
            End If
        Next
Or
Code:
  textbox1.text = WebBrowser1.Document.GetElementById("addyForm:addressSelect").GetAttribute("value")
if u want i can explain it xD well i will try :p
 
  • Like
Reactions: kytro360

kytro360

Member
Original poster
member
Joined
Sep 9, 2009
Posts
376
Reaction score
6
Points
75
Both codes didnt work, strange o.0. Im fairly sure I have the right ID because Im using a Web ID Extractor and its never given me the wrong ID before.
 

goku93

Senior
senior
member
Joined
Jun 13, 2011
Posts
465
Reaction score
81
Points
146
hmmm that's very rare because i've tested the codes before post it... and the ID is correct :) BTW you can use firebug for firefox if u want to know ids...etc or if u use chrome just right click and inspect element.

Hahaha C# is almost the same xD i created a project in C#, try it, add a webbrowser, 1 button and a textbox with the defaults name:

Code:
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://10minutemail.com/10MinuteMail/index.html");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = webBrowser1.Document.GetElementById("addyForm:addressSelect").GetAttribute("value");
        }
    }
}
 
  • Like
Reactions: kytro360

kytro360

Member
Original poster
member
Joined
Sep 9, 2009
Posts
376
Reaction score
6
Points
75
Nevermind I fixed the issue. I had the code in some if/then statements so I removed it then moved it to the documentcompleted event and now it works. Thanks ^.^
 

kytro360

Member
Original poster
member
Joined
Sep 9, 2009
Posts
376
Reaction score
6
Points
75
Hey goku93 I have another question. Im making a program that registers on sites. I have like three textfiles, firstname,lastname,dictionary. How can I make my program populate fields on my program from the text files? I hope that made sense
 

goku93

Senior
senior
member
Joined
Jun 13, 2011
Posts
465
Reaction score
81
Points
146
You mean is read a txt file and fill the info in the textbox?
 

kytro360

Member
Original poster
member
Joined
Sep 9, 2009
Posts
376
Reaction score
6
Points
75
Yes like I want my program to pick random lines from each textfile and populate it to certain fields.
 

goku93

Senior
senior
member
Joined
Jun 13, 2011
Posts
465
Reaction score
81
Points
146
Well i searched a little because i have my doubt about this too xD

But here is the code for 1 txt and 1 textbox (you said you want from 3 txt for 3 textbox, so you just need to edit)

Code:
 Dim ioFile As New StreamReader("proxies.txt")
    'Generic list for holding the lines
      Dim lines As New List(Of String)
     'Random class to generate our random number
    Dim rnd As New Random()
     'Variable to hold our random line number
    Dim line As Integer
     'Now we loop through each line of our text file
     'adding each line to our list
    While ioFile.Peek <> -1
         lines.Add(ioFile.ReadLine())
     End While

     'Now we need a random number
     line = rnd.Next(lines.Count + 1)
     'Now write out the random line to the TextBox
    TextBox1.AppendText(lines(line).Trim())
     'Close our StreamReader
    ioFile.Close()
     'Dispose of the instance
     ioFile.Dispose()
 

kytro360

Member
Original poster
member
Joined
Sep 9, 2009
Posts
376
Reaction score
6
Points
75
Hmmm Ill have to edit it a bit cause the code loads the list of "proxies" into a listbox or something then picks a random one to use. Thanks :)
 

goku93

Senior
senior
member
Joined
Jun 13, 2011
Posts
465
Reaction score
81
Points
146
what? lol no xD

the only thing you have to edit is:
Code:
 Dim ioFile As New StreamReader("proxies.txt")
proxies.txt for the text file you want to load.

and
Code:
TextBox1.AppendText(lines(line).Trim())
and change textbox1 for the name of you textbox. Thats all...

BTW all the text that is with ', is a comment . for ex:
'Generic list for holding the lines
 

kytro360

Member
Original poster
member
Joined
Sep 9, 2009
Posts
376
Reaction score
6
Points
75
Thanks lol

Can you help me with the portion of the code:

Code:
  while (ioFile.Peek !=-1)
            {
                lines.Add(ioFile.ReadLine());
            }

Its returning an error

bump...............
 

goku93

Senior
senior
member
Joined
Jun 13, 2011
Posts
465
Reaction score
81
Points
146
Post the error to see what is happening hmm
 

kytro360

Member
Original poster
member
Joined
Sep 9, 2009
Posts
376
Reaction score
6
Points
75
The error is:

Operator '!=' cannot be applied to operands of type 'method group' and 'int'
 
Status
Not open for further replies.