CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2010
    Posts
    1

    Exclamation Calling a Control

    Hey,

    So i've got an issue with something i've been designing for our company that should basically help us run a series of tests and take screenshots of these tests.

    I've decided to revamp this program by making some changes on the way the tests actually run.

    Basically the problem i'm having is that Visual C# is saying that "web" does not exist in the current context. Technically that's true because "web" doesn't get created until a button click. Here's my code for the button click.

    TabPage tab = new TabPage();
    tab.Text = "Test 1";
    tabcontrol1.TabPages.Add(tab);
    WebBrowser web = new WebBrowser();
    tab.Controls.Add(web);
    web.Dock = System.Windows.Forms.DockStyle.Fill;
    web.Location = new System.Drawing.Point(0, 0);
    web.Url = new System.Uri(test1, System.UriKind.Absolute);
    web.ScriptErrorsSuppressed = true;

    All this does is create a tabPage on button click, fairly simple.

    Now, there is another function that run's my Screenshots on another button click here's the code for that:

    if (SnagImg.CaptureState == SNAGITLib.snagCaptureState.scsIdle)
    {
    donelabel.Text = "Taking Screenshot";
    tabcontrol1.SelectTab(1);

    IntPtr wbHandle = FindWindowEx(web.Handle, IntPtr.Zero,
    "Shell Embedding", null);
    wbHandle = FindWindowEx(wbHandle, IntPtr.Zero,
    "Shell DocObject View", null);
    wbHandle = FindWindowEx(wbHandle, IntPtr.Zero,
    "Internet Explorer_Server", null);
    SnagImg.AutoScrollOptions.AutoScrollMethod = SNAGITLib.snagAutoScrollMethod.sasmVertical;
    SnagImg.AutoScrollOptions.StartingPosition =
    SNAGITLib.snagAutoScrollStartingPosition.sasspTopLeft;

    This isn't all the code but it's enough to understand the situation....

    as you can see where it says "web.handle" is where it's trying to make the call to web, but unfortunatly because it hasn't been created yet, it says it doesn't exist. So i can't even build the program for testing unless this is commented out. How can I get this to recognize the "web" webBrowser control from the button click, I have an if statement in place that tells this code not to run if the test has not been run too so that's not an issue.

    Thanks for the help,

    Plano.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Calling a Control

    Local variables created in one method can't be used in another method.

    If you need to do this, then make the local variable a class field.

    For example.

    Code:
     
    WebBrowser web = new WebBrowser();
    should be declared on the class level as:

    Code:
    private WebBrowser _web = new WebBrowser();
    then it can be accessed in class methods as:

    Code:
    IntPtr wbHandle = FindWindowEx( _web.Handle, IntPtr.Zero,
    "Shell Embedding", null );

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured