让用户设置自己的主页无法正常工作
本文关键字:常工作 工作 设置 用户 自己的 主页 | 更新日期: 2023-09-27 18:37:25
基本上,我有2种形式。主页是在单击主窗体中的菜单项时创建的表单。菜单项弹出此表单,要求用户输入所需的URL作为他/她的主页。还包含"确定"和"取消"按钮。url 应保存在字符串中,以便我可以在按下主页按钮时从 Form1 访问它。主页按钮应加载保存到富文本框中的 URL。我收到一个错误,指出我的 URI 在调用时无效httpwebrequest.create(string URLSavedInHomepage)
public partial class Home:Form
{
//string homeURL = "";
string homepage;
public Home()
{
InitializeComponent();
}
private void OKButton_Click(object sender, EventArgs e)
{
string currentInput = url.Text;
homepage = "http://" + currentInput;
setHomepage(homepage);
this.Close();
}
private void CancelB_Click_1(object sender, EventArgs e)
{
this.Close();
}
public string getHomepage()
{
return homepage;
}
public void setHomepage(string home)
{
homepage = home;
}
private void Home_Load(object sender, EventArgs e)
{
string currentInput = url.Text;
homepage = "http://" + currentInput;
setHomepage(homepage);
}
主窗体中的一些代码:
private void HomePage_Click(object sender, EventArgs e)
{
Home home = new Home();
URLInput.Text = home.getHomepage();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URLInput.Text);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
richTextBox1.Text = sr.ReadToEnd();
}
private void ChangeHomeToolStripMenuItem_Click(object sender, EventArgs e)
//this is menu item to call
{
Home h = new Home();
h.Show();
}
请注意,我不能使用 WebBrowser 类,因为要求说我不能使用它,否则我只会使用.Navigate()
按如下方式更改代码(我假设 URLInput 是主窗体上的控件):
private void HomePage_Click(object sender, EventArgs e)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URLInput.Text);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
richTextBox1.Text = sr.ReadToEnd();
}
private void ChangeHomeToolStripMenuItem_Click(object sender, EventArgs e)
//this is menu item to call
{
using(Home h = new Home())
{
h.ShowDialog();
var homePageUrl = h.getHomePage();
if (string.IsNullOrEmpty(homePageUrl))
return;
URLInput.Text = homePageUrl;
}
}