Making a global variable
-
Hi, first post on this site. I've just started C# really and have been using some of these tutorials and microsoft's video series. But, I got bored of the video series and started just playing around. I decided, I wanted to create a web browser, so I added the web browser object to my form and a textbox and button to type in the address and initiate the search respectively. Then I decided I wanted to add a bookmark feature, so I added a menuStrip and a Bookmark tab to it. I made it create a new instance of Bookmark.cs which I had created. On the Bookmark.cs form I added a listbox and button. The listbox displayed the data from a database that I had added to the project, I entered some example data and this all worked. But, when I click the button on the Bookmark.cs form, I want it to set the webBrowser1.Navigate() function to the SelectedValue of the listbox on Bookmark.cs. Yet, I cant because webBrowser1 isn't public. What I am really asking is, how do I make webBrowser1 public, so that I can access it from Bookmark.cs? Thanks, Any reply is appreciated.
-
Hi, first post on this site. I've just started C# really and have been using some of these tutorials and microsoft's video series. But, I got bored of the video series and started just playing around. I decided, I wanted to create a web browser, so I added the web browser object to my form and a textbox and button to type in the address and initiate the search respectively. Then I decided I wanted to add a bookmark feature, so I added a menuStrip and a Bookmark tab to it. I made it create a new instance of Bookmark.cs which I had created. On the Bookmark.cs form I added a listbox and button. The listbox displayed the data from a database that I had added to the project, I entered some example data and this all worked. But, when I click the button on the Bookmark.cs form, I want it to set the webBrowser1.Navigate() function to the SelectedValue of the listbox on Bookmark.cs. Yet, I cant because webBrowser1 isn't public. What I am really asking is, how do I make webBrowser1 public, so that I can access it from Bookmark.cs? Thanks, Any reply is appreciated.
The problem is not that it's not public, it's that it's not static, so you need a copy of the form that contains the active control, for you to be able to access it. This is the wrong way to do it anyhow, you don't want to make the control public, you want to either expose a method that calls the Navigate function, or, better yet, set up a delegate so that your bookmark form can call into your main form and do this for you.
-
The problem is not that it's not public, it's that it's not static, so you need a copy of the form that contains the active control, for you to be able to access it. This is the wrong way to do it anyhow, you don't want to make the control public, you want to either expose a method that calls the Navigate function, or, better yet, set up a delegate so that your bookmark form can call into your main form and do this for you.
-
As I said, I'm a beginner at C# so could you please go into more detail on that because I don't know what you are talking about, except the static bit. What is the right way to do it? Any reply appreciated.
The way i usually do global variables is with a class, like this: internal class Globalvars { internal static int variable; Globalvars(){} } You just mark all the variables as internal static, and you call them like this: Globalvars.variable Just store variables there and access them from somewhere alse later on. You should be able to access them from anywhere. I don't know if this helps but.. there you go.
-
As I said, I'm a beginner at C# so could you please go into more detail on that because I don't know what you are talking about, except the static bit. What is the right way to do it? Any reply appreciated.
Well, the first step is to not create global variables if you can help it. www.codeproject.com/csharp/passdatadelegate.asp would be one example of how to use delegates. Have the bookmark page define a delegate which is hooked up to a method on the main form, and passes through the URL to browse to.
-
Hi, first post on this site. I've just started C# really and have been using some of these tutorials and microsoft's video series. But, I got bored of the video series and started just playing around. I decided, I wanted to create a web browser, so I added the web browser object to my form and a textbox and button to type in the address and initiate the search respectively. Then I decided I wanted to add a bookmark feature, so I added a menuStrip and a Bookmark tab to it. I made it create a new instance of Bookmark.cs which I had created. On the Bookmark.cs form I added a listbox and button. The listbox displayed the data from a database that I had added to the project, I entered some example data and this all worked. But, when I click the button on the Bookmark.cs form, I want it to set the webBrowser1.Navigate() function to the SelectedValue of the listbox on Bookmark.cs. Yet, I cant because webBrowser1 isn't public. What I am really asking is, how do I make webBrowser1 public, so that I can access it from Bookmark.cs? Thanks, Any reply is appreciated.
Hi I have a suggestion to make.How about writing a method in you web browser from that gets a bookmark and ask the web browser to nvaigate to it. for example:
public void NavigateToBookmark(Bookmark bm) { this.webBrowser1.Navigate(bm.Url); }
Anyway,I don't think that declaring the web browser control as public is a good idea,because if you do so,you make your bookmark dependent to a specific webbrowser and that's not good if you want to use the bookmark in some other code.Instead you can make your bookmark holds a reference to a webborwser(not to use a specific webbrowser) or you can pass a webbrowser object to your bookmarkpublic class BookMark { WebBrowser browser; public BookMark(WebBrowser browser) { this.browser=browser; } public void Navigate() { this.browser.Navigate(this.url); } }
Or//In BookMark class public void Navigate(WebBrowser browser) { browser.Navigate(this.url); }
Regards -
Hi I have a suggestion to make.How about writing a method in you web browser from that gets a bookmark and ask the web browser to nvaigate to it. for example:
public void NavigateToBookmark(Bookmark bm) { this.webBrowser1.Navigate(bm.Url); }
Anyway,I don't think that declaring the web browser control as public is a good idea,because if you do so,you make your bookmark dependent to a specific webbrowser and that's not good if you want to use the bookmark in some other code.Instead you can make your bookmark holds a reference to a webborwser(not to use a specific webbrowser) or you can pass a webbrowser object to your bookmarkpublic class BookMark { WebBrowser browser; public BookMark(WebBrowser browser) { this.browser=browser; } public void Navigate() { this.browser.Navigate(this.url); } }
Or//In BookMark class public void Navigate(WebBrowser browser) { browser.Navigate(this.url); }
Regards -
Hi I have a suggestion to make.How about writing a method in you web browser from that gets a bookmark and ask the web browser to nvaigate to it. for example:
public void NavigateToBookmark(Bookmark bm) { this.webBrowser1.Navigate(bm.Url); }
Anyway,I don't think that declaring the web browser control as public is a good idea,because if you do so,you make your bookmark dependent to a specific webbrowser and that's not good if you want to use the bookmark in some other code.Instead you can make your bookmark holds a reference to a webborwser(not to use a specific webbrowser) or you can pass a webbrowser object to your bookmarkpublic class BookMark { WebBrowser browser; public BookMark(WebBrowser browser) { this.browser=browser; } public void Navigate() { this.browser.Navigate(this.url); } }
Or//In BookMark class public void Navigate(WebBrowser browser) { browser.Navigate(this.url); }
RegardsOk, I don't know what to do now. I am creating a TextEditor (because I saw it in another post ;P). Anyway, I have created the main form, but when the program starts it displays another form called newDlg.cs It has buttons on, with labels saying "Create a Text File:" or "Create a CSV File:" etc. When the button is clicked, it opens the main form and assigns a variable called fileFormat to 1 or 2 (for Text File and CSV respectively). The fileFormat variable is then used to determine whether the file can be saved as a .txt or a .csv when the user clicks Save. I need to somehow make fileFormat global because it is used in newDlg.cs and Main.cs I tried the method of creating a globalVars.cs class but that didn't work. Anyone got any idea? Thanks.