簡體   English   中英

顯示新表單不起作用

[英]Show new form doesn't work

我為此工作了幾天。 現在我還有另一個問題。 所以我在我的zzz.cs有這個:

private void button3_Click_1(object sender, EventArgs e)
{
    scraper zzz = new scraper();
    zzz.Show();
}

出於一個原因,它不想打開名為scraper.cs的其他窗體。 zzz.csscraper.cs具有相同的命名空間。 這怎么行不通? 什么是修復?

編輯:

問題已解決,但現在從 login.cs 沒有轉到 zzz.cs。 我的代碼是:

    MessageBox.Show("You are logged in successfully");
    zzz zzz = new zzz();
    zzz.Show();
    this.Close();

但這現在行不通了。 如何解決? 以前有用,現在不行了……

我又試了一次。 我看到 zzz.cs 打開。 然后直接關閉代碼:0。這是我的 zzz.cs 再次:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Login
{
    public partial class zzz : Form
    {
        public static List<string> proxies { get; set; } = new List<string>();
        public static List<string> Links { get; set; } = new List<string>();
        public static string path;
        public zzz()
        {
            InitializeComponent();
        }
        private void button1_Click_1(object sender, EventArgs e)
        {
            Logs.Items.Clear();
            if (radioButton1.Checked == true)
            {
                bool useproxies = true;
                Logs.Items.Add("Using proxies enabled!");
                scrape();
            }
            else
            {
                bool useproxies = false;
                Logs.Items.Add("Using proxies disabled!");
            }
            void scrape ()
            {
                int omg = proxyscraper();
            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        public int proxyscraper()
        {
            return 0;
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            //Form1 aaa = new Form1();
            //aaa.Show();
        }
    }
}

這是我的 inlogin.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Login
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

        }
        //Enter code here for your version of username and userpassword
        Login login = new Login("admin", "1234");


        private void button1_Click(object sender, EventArgs e)
        {
            //define local variables from the user inputs
            string user = nametxtbox.Text;
            string pass = pwdtxtbox.Text;
            //check if eligible to be logged in

            string login(string lol,string lel)
            {
                try
                {
                    var request = (HttpWebRequest)WebRequest.Create("http://SNIP/mama.php?user=" + lol + "&password=" + lel);
                    var response = (HttpWebResponse)request.GetResponse();
                    string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
                    return responseString;
                }
                catch
                {
                    string responseString = "NO";
                    return responseString;
                }
            }
            string wat = "YES";
            if (login(user, pass) == wat)
            {
                MessageBox.Show("You are logged in successfully");
                zzz aaa = new zzz();
                aaa.Show();
                this.Close();
            }
            else
            {
                //show default login error message
                MessageBox.Show("Login Error!");
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}

哇....

你試過編譯你的代碼嗎? 它不應該這樣做。

原因之一是您將文件名本身和類名混合在一起。 這是不同的名稱。

你必須創建你的類的一個實例。 在這種情況下,您的 Form 使用其構造函數。

private void button3_Click_1(object sender, EventArgs e)
{
    Form1 aaa = new Form1()
    aaa.Show();
}

您還可以將您的類Form1重命名為Scraper並調用

private void button3_Click_1(object sender, EventArgs e)
{
    Scraper aaa = new Scraper()
    aaa.Show();
}

回答您的編輯

您正在使用zzz.Show()它只是打開zzz的非模態對話框並執行 click 方法中的下一個方法。 這是this.Close(); . 這將關閉您當前的登錄表單,這也會關閉您的zzz 您可能應該使用zzz.ShowDialog(); 打開zzz 現在登錄對話框正在“等待”,直到您的 sxrapper 對話框關閉。 之后,登錄屏幕通過調用this.Close();自行關閉this.Close(); .

我建議您更改應用程序流程。 首先打開您的登錄屏幕,在它成功關閉后,您可以打開刮板。

例如,您可以將Program.cs文件中Program類中的Main方法更改為:

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    var loginForm = new YourLoginDialog();
    var result = YourLoginDialog.ShowDialog();
    if (result == DialogResult.Yes)
    {
        Application.Run(new Scraper());
    }
    else
    {
        MessageBox.Show("Login failed");
    }
}

在它仍然不起作用的情況下:

檢查您的button3_Click_1是否連接到您的button3 有很多方法可以做到這一點,但常見的方法是使用 VisualStudio 的屬性窗口按F4選擇button3並選擇bolt /Event 選項卡並為單擊事件選擇button3_Click_1方法。
或者在調用InitializeComponent();之后將下面這行添加到您的構造函數中InitializeComponent();

this.button3.Click += new System.EventHandler(this.button3_Click_1);

從上面的評論中回答您的問題:

刮板是一個 Windows 窗體 idk 如何找到它

您的Scraper ( Form1 ) 類繼承System.Windows.Forms.Form因為類定義中的: Form

public partial class Form1 : Form
{
    // ...
}

也許你應該看看Classes and Structs (C# Programming Guide)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM