2

I'm writing an application with C# Winforms and using a WebBrowser object to display text files.

I have a DataGridView which shows me all the files I have added before, and every click on a Data Row activates webBrowser.Navigate(FilePath).

The thing is it shows me the text file properly in the first time after clicking the first .txt in the DataGrid, but after it its just writing me:

the page could not be displayed

...in the WebBrowser panel.

I tried to added this code before it loaded another text file:

                    webBrowser.Navigate("about:blank");
                    System.Windows.Forms.Application.DoEvents();

but it doesn't help.

Edit: After clicking on a RowCell in DataGridView I'm calling this function with the file full path:

    private byte LoadDoc(string sFileName)
    {
        fInfo = new FileInfo(sFileName);
        if (fInfo.Extension.ToUpper().StartsWith(".TXT"))
        {
            pnlBrowser.Visible = true;
            webBrowser.Navigate(fInfo.FullName);

            if (webBrowser.Document != null)
            {
                 webBrowser.Document.Encoding = "iso-8859-8-i";
            }   
            return 0;
        }
     }

Edit: This is the DataGridView CellClick code:

    private void dgvDetails_CellClick(object sender, DataGridViewCellEventArgs e)
    {

        if (e.RowIndex >= 0 && dgvDetails.Rows.Count > 0)
        {
            if (e.ColumnIndex == 0)
            {
                DataGridViewCell myCell = dgvDetails.Rows[e.RowIndex].Cells[0];
                if (Convert.ToBoolean(myCell.Value) == true)
                    myCell.Value = false;
                else
                    myCell.Value = true;

            }
            this.Cursor = Cursors.WaitCursor;

            txtFileDesc.Text = dgvDetails.Rows[e.RowIndex].Cells["C90FILDS"].Value.ToString();

            // clear temp values
            ClearTempVals();

            this.Cursor = Cursors.Default;
            System.Windows.Forms.Application.DoEvents();
            this.Cursor = Cursors.WaitCursor;

            // store temp values in case update required
            sd_CustNum = Convert.ToInt32(dgvDetails.Rows[e.RowIndex].Cells["C90CSTID"].Value);
            sd_ProcId = Convert.ToInt64(dgvDetails.Rows[e.RowIndex].Cells["C90CSTID"].Value);
            sd_ActivityId = Convert.ToInt32(dgvDetails.Rows[e.RowIndex].Cells["C90CSTID"].Value);
            sd_FileId = Convert.ToString(dgvDetails.Rows[e.RowIndex].Cells["C90FILID"].Value);
            sd_FileDesc = Convert.ToString(dgvDetails.Rows[e.RowIndex].Cells["C90FILDS"].Value);
            sd_Mutag = Convert.ToString(dgvDetails.Rows[e.RowIndex].Cells["C90MUTAG"].Value);
            sd_Dealer = Convert.ToString(dgvDetails.Rows[e.RowIndex].Cells["C90DEAL"].Value);
            sd_DocNum = Convert.ToInt32(dgvDetails.Rows[e.RowIndex].Cells["C90DOCID"].Value);
            sd_DocType = Convert.ToByte(dgvDetails.Rows[e.RowIndex].Cells["C90DOCTP"].Value);
            sd_ScanDate = Convert.ToInt32(dgvDetails.Rows[e.RowIndex].Cells["C90OPDT"].Value);
            sd_ScanTime = Convert.ToInt32(dgvDetails.Rows[e.RowIndex].Cells["C90OPTM"].Value);
            sd_ScanUser = Convert.ToString(dgvDetails.Rows[e.RowIndex].Cells["C90OPUS"].Value);
            sd_SibatIdkun = Convert.ToString(dgvDetails.Rows[e.RowIndex].Cells["C90UPSBA"].Value);
            sd_IdkunDate = Convert.ToInt32(dgvDetails.Rows[e.RowIndex].Cells["C90UPDT"].Value);
            sd_IdkunTime = Convert.ToInt32(dgvDetails.Rows[e.RowIndex].Cells["C90UPTM"].Value);
            sd_IdkunUser = Convert.ToString(dgvDetails.Rows[e.RowIndex].Cells["C90UPUS"].Value);



            if (LoadDoc(dgvDetails.Rows[e.RowIndex].Cells["C90FILID"].Value.ToString()) > 0)
            {
                ClearTempVals();
                _ErrMsg = "Error Accrues";
                return;
            }
            System.Threading.Thread.Sleep(250);
            BtnSave.Enabled = true;
            btnDelete.Enabled = true;

        }
        else // trying to sort columns by clicking headers
        {
            // clear webbrowser and selected rows
            webBrowser1.Navigate("about:blank");
            dgvDetails.ClearSelection();
            dgvDetails.CurrentCell = null;
            txtFileDesc.Text = "";
            BtnSave.Enabled = false;
            ClearTempVals();
        }

    }

Edit: This is what WebBrowser object contains (in debug mode) after calling the second text file:

enter image description here

Please help me.

Orionlk
  • 278
  • 2
  • 3
  • 17
  • After a click on a `DataGridView` I'm just calling a function `LoadDoc` which uses the `WebBrowser` object. I edited my original question. – Orionlk Aug 31 '15 at 08:19
  • 1
    First of all: stop swallowing exceptions! Remove the try/catch block too see what happens. – Micke Aug 31 '15 at 08:30
  • @Micke, Ok you are right, I removed the try\catch block, but as expected nothing happens and it's still not working. – Orionlk Aug 31 '15 at 08:37
  • Are all the text files stored in the same location/folder as the one you are able to display successfully? What if you make multiple copies of the that displays OK. Can you display those (copies) as well? – Micke Aug 31 '15 at 08:42
  • What if you call `webBrowser.Refresh()` after `Navigate()`? Does it change anything? – Micke Aug 31 '15 at 08:44
  • 1
    Considering we're no closer to solving what should be rather straight-forward problem, I think you should post your handler code for `RowCell` click where you call `LoadDoc()`. Just in case your doing all this in a _worker thread; neglected planetary alignment_ or _forgot to sacrifice a chicken_. Cheers –  Aug 31 '15 at 08:59
  • Yes, all the text files stores in the same folder. The problem is that the same text file couldn't shows at all after clicking it second or third time...Also, `webBrowser.Refresh()` does not help. I have added a picture of the `WebBrowser` object in debug mode which shows the problem. – Orionlk Aug 31 '15 at 09:02
  • 1
    _[You should not use `System.Windows.Forms.Application.DoEvents();`](http://stackoverflow.com/questions/5181777/use-of-application-doevents). It can lead to _re-entrancy_ in your UI-handlers. Plus it's unkind to kittens –  Aug 31 '15 at 09:08
  • If in your click-handler, you are calling `LoadDoc()`, then looping around with `Application.DoEvents();` - then **there is your problem right there** –  Aug 31 '15 at 09:10
  • I added the `RowCell click` code – Orionlk Aug 31 '15 at 09:12
  • 1
    Thanks. Just do a `this.Cursor = Cursors.WaitCursor` and remove the prior 2 lines including the `DoEvents()`. Also, what's the `Thread.Sleep()` for? –  Aug 31 '15 at 09:14
  • @MickyDuncan I added this `Thread.Sleep()` to add small delay before I enables the buttons in the form, in order to prevent clicking errors from the user. – Orionlk Aug 31 '15 at 09:20
  • @MickyDuncan I deleted `DoEvents()` but still nothing.,, – Orionlk Aug 31 '15 at 09:21

0 Answers0