当应用程序中的窗口打开时,如何使dateTimePicker无法聚焦?

时间:2015-10-01 18:54:59

标签: c# winforms datetimepicker

我创建了一个Windows窗体应用。我希望焦点位于“商家名称”文本框中。但是,当表单打开时,重点是发现的日期,特别是10月份的#10;'。如何将焦点更改为“商家名称”而不是“发现日期”?

谢谢!

        this.panBusinessInfo = new System.Windows.Forms.Panel();
        this.tbxEmail = new System.Windows.Forms.TextBox();
        this.lblEmail = new System.Windows.Forms.Label();
        this.tbxPhone = new System.Windows.Forms.TextBox();
        this.lblPhone = new System.Windows.Forms.Label();
        this.tbxContactPerson = new System.Windows.Forms.TextBox();
        this.lblContactPerson = new System.Windows.Forms.Label();
        this.tbxWayBackMachURL = new System.Windows.Forms.TextBox();
        this.tbxBusinessURL = new System.Windows.Forms.TextBox();
        this.tbxZipCode = new System.Windows.Forms.TextBox();
        this.cbxState = new System.Windows.Forms.ComboBox();
        this.tbxCity = new System.Windows.Forms.TextBox();
        this.tbxSuite = new System.Windows.Forms.TextBox();
        this.cbxIndustry = new System.Windows.Forms.ComboBox();
        this.tbxAddress = new System.Windows.Forms.TextBox();
        this.tbxBusinessName = new System.Windows.Forms.TextBox();
        this.lblIndustry = new System.Windows.Forms.Label();
        this.lblWayBackMachURL = new System.Windows.Forms.Label();
        this.lblBusinessURL = new System.Windows.Forms.Label();
        this.lblZipCode = new System.Windows.Forms.Label();
        this.lblState = new System.Windows.Forms.Label();
        this.lblCity = new System.Windows.Forms.Label();
        this.lblSuite = new System.Windows.Forms.Label();
        this.lblAddress = new System.Windows.Forms.Label();
        this.lblBusinessName = new System.Windows.Forms.Label();
        this.pnDates = new System.Windows.Forms.Panel();
        this.dtpProjFinish = new System.Windows.Forms.DateTimePicker();
        this.dtpProjStart = new System.Windows.Forms.DateTimePicker();
        this.dtpDateDisc = new System.Windows.Forms.DateTimePicker();
        this.lblProjFinishDate = new System.Windows.Forms.Label();
        this.lblProjStartDate = new System.Windows.Forms.Label();
        this.lblDateDiscovered = new System.Windows.Forms.Label();
        this.pnTerms = new System.Windows.Forms.Panel();
        this.btnCalculate = new System.Windows.Forms.Button();
        this.lblTMCost = new System.Windows.Forms.Label();
        this.lblTotMaint = new System.Windows.Forms.Label();
        this.lblSBCost = new System.Windows.Forms.Label();
        this.lblSiteBuildCost = new System.Windows.Forms.Label();
        this.chbxMaintOnly = new System.Windows.Forms.CheckBox();
        this.chbxBuildOut = new System.Windows.Forms.CheckBox();
        this.lblMaintOnly = new System.Windows.Forms.Label();
        this.lblMaintenacneFeeOnly = new System.Windows.Forms.Label();
        this.lblGTotal = new System.Windows.Forms.Label();
        this.lblGrandTotal = new System.Windows.Forms.Label();
        this.lblMonthlyPaymentCalc = new System.Windows.Forms.Label();
        this.lblMonthlyPayment = new System.Windows.Forms.Label();
        this.tbxNumMonths = new System.Windows.Forms.TextBox();
        this.lblNumMonths = new System.Windows.Forms.Label();
        this.tbxMaintFee = new System.Windows.Forms.TextBox();
        this.lblMaintenanceFee = new System.Windows.Forms.Label();
        this.tbxSBCost = new System.Windows.Forms.TextBox();
        this.lblTotContract = new System.Windows.Forms.Label();
        this.chbxPayments = new System.Windows.Forms.CheckBox();
        this.chbxFull = new System.Windows.Forms.CheckBox();
        this.lblTerms = new System.Windows.Forms.Label();
        this.pnNotes = new System.Windows.Forms.Panel();
        this.tbxNotes = new System.Windows.Forms.TextBox();
        this.lblNotesReq = new System.Windows.Forms.Label();
        this.pnButtons = new System.Windows.Forms.Panel();
        this.btnEmail = new System.Windows.Forms.Button();
        this.btnCreatePDF = new System.Windows.Forms.Button();
        this.btnSave = new System.Windows.Forms.Button();
        this.btnClear = new System.Windows.Forms.Button();
        this.btnClose = new System.Windows.Forms.Button();
        this.panBusinessInfo.SuspendLayout();
        this.pnDates.SuspendLayout();
        this.pnTerms.SuspendLayout();
        this.pnNotes.SuspendLayout();
        this.pnButtons.SuspendLayout();
        this.SuspendLayout();
        // 
        // panBusinessInfo
        // 
        this.panBusinessInfo.Controls.Add(this.tbxEmail);
        this.panBusinessInfo.Controls.Add(this.lblEmail);
        this.panBusinessInfo.Controls.Add(this.tbxPhone);
        this.panBusinessInfo.Controls.Add(this.lblPhone);
        this.panBusinessInfo.Controls.Add(this.tbxContactPerson);
        this.panBusinessInfo.Controls.Add(this.lblContactPerson);
        this.panBusinessInfo.Controls.Add(this.tbxWayBackMachURL);
        this.panBusinessInfo.Controls.Add(this.tbxBusinessURL);
        this.panBusinessInfo.Controls.Add(this.tbxZipCode);
        this.panBusinessInfo.Controls.Add(this.cbxState);
        this.panBusinessInfo.Controls.Add(this.tbxCity);
        this.panBusinessInfo.Controls.Add(this.tbxSuite);
        this.panBusinessInfo.Controls.Add(this.cbxIndustry);
        this.panBusinessInfo.Controls.Add(this.tbxAddress);
        this.panBusinessInfo.Controls.Add(this.tbxBusinessName);
        this.panBusinessInfo.Controls.Add(this.lblIndustry);
        this.panBusinessInfo.Controls.Add(this.lblWayBackMachURL);
        this.panBusinessInfo.Controls.Add(this.lblBusinessURL);
        this.panBusinessInfo.Controls.Add(this.lblZipCode);
        this.panBusinessInfo.Controls.Add(this.lblState);
        this.panBusinessInfo.Controls.Add(this.lblCity);
        this.panBusinessInfo.Controls.Add(this.lblSuite);
        this.panBusinessInfo.Controls.Add(this.lblAddress);
        this.panBusinessInfo.Controls.Add(this.lblBusinessName);
        this.panBusinessInfo.Location = new System.Drawing.Point(8, 8);
        this.panBusinessInfo.Name = "panBusinessInfo";
        this.panBusinessInfo.Size = new System.Drawing.Size(305, 286);
        this.panBusinessInfo.TabIndex = 0;
        // 
        // tbxEmail
        // 
        this.tbxEmail.Location = new System.Drawing.Point(150, 135);
        this.tbxEmail.Name = "tbxEmail";
        this.tbxEmail.Size = new System.Drawing.Size(148, 20);
        this.tbxEmail.TabIndex = 9;
        // 
        // lblEmail
        // 
        this.lblEmail.AutoSize = true;
        this.lblEmail.Location = new System.Drawing.Point(150, 119);
        this.lblEmail.Name = "lblEmail";
        this.lblEmail.Size = new System.Drawing.Size(35, 13);
        this.lblEmail.TabIndex = 999;
        this.lblEmail.Text = "Email:";
        // 
        // tbxPhone
        // 
        this.tbxPhone.Location = new System.Drawing.Point(6, 135);
        this.tbxPhone.Name = "tbxPhone";
        this.tbxPhone.Size = new System.Drawing.Size(138, 20);
        this.tbxPhone.TabIndex = 8;
        // 
        // lblPhone
        // 
        this.lblPhone.AutoSize = true;
        this.lblPhone.Location = new System.Drawing.Point(3, 119);
        this.lblPhone.Name = "lblPhone";
        this.lblPhone.Size = new System.Drawing.Size(41, 13);
        this.lblPhone.TabIndex = 999;
        this.lblPhone.Text = "Phone:";
        // 
        // tbxContactPerson
        // 
        this.tbxContactPerson.Location = new System.Drawing.Point(6, 250);
        this.tbxContactPerson.Name = "tbxContactPerson";
        this.tbxContactPerson.Size = new System.Drawing.Size(292, 20);
        this.tbxContactPerson.TabIndex = 12;
        // 
        // lblContactPerson
        // 
        this.lblContactPerson.AutoSize = true;
        this.lblContactPerson.Location = new System.Drawing.Point(3, 234);
        this.lblContactPerson.Name = "lblContactPerson";
        this.lblContactPerson.Size = new System.Drawing.Size(83, 13);
        this.lblContactPerson.TabIndex = 999;
        this.lblContactPerson.Text = "Contact Person:";
        // 
        // tbxWayBackMachURL
        // 
        this.tbxWayBackMachURL.Location = new System.Drawing.Point(6, 212);
        this.tbxWayBackMachURL.Name = "tbxWayBackMachURL";
        this.tbxWayBackMachURL.Size = new System.Drawing.Size(292, 20);
        this.tbxWayBackMachURL.TabIndex = 11;
        // 
        // tbxBusinessURL
        // 
        this.tbxBusinessURL.Location = new System.Drawing.Point(6, 173);
        this.tbxBusinessURL.Name = "tbxBusinessURL";
        this.tbxBusinessURL.Size = new System.Drawing.Size(292, 20);
        this.tbxBusinessURL.TabIndex = 10;
        // 
        // tbxZipCode
        // 
        this.tbxZipCode.Location = new System.Drawing.Point(214, 97);
        this.tbxZipCode.Name = "tbxZipCode";
        this.tbxZipCode.Size = new System.Drawing.Size(84, 20);
        this.tbxZipCode.TabIndex = 7;
        // 
        // cbxState
        // 
        this.cbxState.FormattingEnabled = true;
        this.cbxState.Items.AddRange(new object[] {
        "AL",
        "AK",
        "AZ",
        "AR",
        "CA",
        "CO",
        "CT",
        "DE",
        "FL",
        "GA",
        "HI",
        "ID",
        "IL",
        "IN",
        "IA",
        "KS",
        "KY",
        "LA",
        "ME",
        "MD",
        "MA",
        "MI",
        "MN",
        "MS",
        "MO",
        "MT",
        "NE",
        "NV",
        "NH",
        "NJ",
        "NM",
        "NY",
        "NC",
        "ND",
        "OH",
        "OK",
        "OR",
        "PA",
        "RI",
        "SC",
        "SD",
        "TN",
        "TX",
        "UT",
        "VT",
        "VA",
        "WA",
        "WV",
        "WI",
        "WY"});
        this.cbxState.Location = new System.Drawing.Point(150, 97);
        this.cbxState.Name = "cbxState";
        this.cbxState.Size = new System.Drawing.Size(57, 21);
        this.cbxState.TabIndex = 6;
        // 
        // tbxCity
        // 
        this.tbxCity.Location = new System.Drawing.Point(6, 97);
        this.tbxCity.Name = "tbxCity";
        this.tbxCity.Size = new System.Drawing.Size(138, 20);
        this.tbxCity.TabIndex = 5;
        // 
        // tbxSuite
        // 
        this.tbxSuite.Location = new System.Drawing.Point(150, 59);
        this.tbxSuite.Name = "tbxSuite";
        this.tbxSuite.Size = new System.Drawing.Size(100, 20);
        this.tbxSuite.TabIndex = 4;
        // 
        // cbxIndustry
        // 
        this.cbxIndustry.FormattingEnabled = true;
        this.cbxIndustry.Items.AddRange(new object[] {
        "Alteration Shop",
        "Art Gallery",
        "Retail-Clothes/Shoes",
        "Retail-Hair and Beauty",
        "Retail-Other",
        "Services-Dry Cleaning",
        "Services-Food",
        "Services-Other",
        "Other"});
        this.cbxIndustry.Location = new System.Drawing.Point(150, 19);
        this.cbxIndustry.Name = "cbxIndustry";
        this.cbxIndustry.Size = new System.Drawing.Size(121, 21);
        this.cbxIndustry.TabIndex = 2;
        // 
        // tbxAddress
        // 
        this.tbxAddress.Location = new System.Drawing.Point(6, 59);
        this.tbxAddress.Name = "tbxAddress";
        this.tbxAddress.Size = new System.Drawing.Size(138, 20);
        this.tbxAddress.TabIndex = 3;
        // 
        // tbxBusinessName
        // 
        this.tbxBusinessName.Location = new System.Drawing.Point(6, 19);
        this.tbxBusinessName.Name = "tbxBusinessName";
        this.tbxBusinessName.Size = new System.Drawing.Size(138, 20);
        this.tbxBusinessName.TabIndex = 1;
        // 
        // lblIndustry
        // 
        this.lblIndustry.AutoSize = true;
        this.lblIndustry.Location = new System.Drawing.Point(147, 3);
        this.lblIndustry.Name = "lblIndustry";
        this.lblIndustry.Size = new System.Drawing.Size(47, 13);
        this.lblIndustry.TabIndex = 999;
        this.lblIndustry.Text = "Industry:";
        // 
        // lblWayBackMachURL
        // 
        this.lblWayBackMachURL.AutoSize = true;
        this.lblWayBackMachURL.Location = new System.Drawing.Point(3, 195);
        this.lblWayBackMachURL.Name = "lblWayBackMachURL";
        this.lblWayBackMachURL.Size = new System.Drawing.Size(126, 13);
        this.lblWayBackMachURL.TabIndex = 999;
        this.lblWayBackMachURL.Text = "WayBack Machine URL:";
        // 
        // lblBusinessURL
        // 
        this.lblBusinessURL.AutoSize = true;
        this.lblBusinessURL.Location = new System.Drawing.Point(3, 157);
        this.lblBusinessURL.Name = "lblBusinessURL";
        this.lblBusinessURL.Size = new System.Drawing.Size(77, 13);
        this.lblBusinessURL.TabIndex = 999;
        this.lblBusinessURL.Text = "Business URL:";
        // 
        // lblZipCode
        // 
        this.lblZipCode.AutoSize = true;
        this.lblZipCode.Location = new System.Drawing.Point(211, 81);
        this.lblZipCode.Name = "lblZipCode";
        this.lblZipCode.Size = new System.Drawing.Size(53, 13);
        this.lblZipCode.TabIndex = 999;
        this.lblZipCode.Text = "Zip Code:";
        // 
        // lblState
        // 
        this.lblState.AutoSize = true;
        this.lblState.Location = new System.Drawing.Point(147, 81);
        this.lblState.Name = "lblState";
        this.lblState.Size = new System.Drawing.Size(35, 13);
        this.lblState.TabIndex = 999;
        this.lblState.Text = "State:";
        // 
        // lblCity
        // 
        this.lblCity.AutoSize = true;
        this.lblCity.Location = new System.Drawing.Point(3, 81);
        this.lblCity.Name = "lblCity";
        this.lblCity.Size = new System.Drawing.Size(27, 13);
        this.lblCity.TabIndex = 999;
        this.lblCity.Text = "City:";
        // 
        // lblSuite
        // 
        this.lblSuite.AutoSize = true;
        this.lblSuite.Location = new System.Drawing.Point(147, 42);
        this.lblSuite.Name = "lblSuite";
        this.lblSuite.Size = new System.Drawing.Size(34, 13);
        this.lblSuite.TabIndex = 999;
        this.lblSuite.Text = "Suite:";
        // 
        // lblAddress
        // 
        this.lblAddress.AutoSize = true;
        this.lblAddress.Location = new System.Drawing.Point(3, 42);
        this.lblAddress.Name = "lblAddress";
        this.lblAddress.Size = new System.Drawing.Size(48, 13);
        this.lblAddress.TabIndex = 999;
        this.lblAddress.Text = "Address:";
        // 
        // lblBusinessName
        // 
        this.lblBusinessName.AutoSize = true;
        this.lblBusinessName.Location = new System.Drawing.Point(3, 3);
        this.lblBusinessName.Name = "lblBusinessName";
        this.lblBusinessName.Size = new System.Drawing.Size(83, 13);
        this.lblBusinessName.TabIndex = 999;
        this.lblBusinessName.Text = "Business Name:";
        // 
        // pnDates
        // 
        this.pnDates.Controls.Add(this.dtpProjFinish);
        this.pnDates.Controls.Add(this.dtpProjStart);
        this.pnDates.Controls.Add(this.dtpDateDisc);
        this.pnDates.Controls.Add(this.lblProjFinishDate);
        this.pnDates.Controls.Add(this.lblProjStartDate);
        this.pnDates.Controls.Add(this.lblDateDiscovered);
        this.pnDates.Location = new System.Drawing.Point(319, 8);
        this.pnDates.Name = "pnDates";
        this.pnDates.Size = new System.Drawing.Size(232, 124);
        this.pnDates.TabIndex = 0;
        // 
        // dtpProjFinish
        // 
        this.dtpProjFinish.Location = new System.Drawing.Point(6, 97);
        this.dtpProjFinish.Name = "dtpProjFinish";
        this.dtpProjFinish.Size = new System.Drawing.Size(218, 20);
        this.dtpProjFinish.TabIndex = 15;
        // 
        // dtpProjStart
        // 
        this.dtpProjStart.Location = new System.Drawing.Point(6, 59);
        this.dtpProjStart.Name = "dtpProjStart";
        this.dtpProjStart.Size = new System.Drawing.Size(218, 20);
        this.dtpProjStart.TabIndex = 14;
        // 
        // dtpDateDisc
        // 
        this.dtpDateDisc.Location = new System.Drawing.Point(6, 19);
        this.dtpDateDisc.Name = "dtpDateDisc";
        this.dtpDateDisc.Size = new System.Drawing.Size(218, 20);
        this.dtpDateDisc.TabIndex = 13;
        // 
        // lblProjFinishDate
        // 
        this.lblProjFinishDate.AutoSize = true;
        this.lblProjFinishDate.Location = new System.Drawing.Point(3, 81);
        this.lblProjFinishDate.Name = "lblProjFinishDate";
        this.lblProjFinishDate.Size = new System.Drawing.Size(111, 13);
        this.lblProjFinishDate.TabIndex = 999;
        this.lblProjFinishDate.Text = "Projected Finish Date:";
        // 
        // lblProjStartDate
        // 
        this.lblProjStartDate.AutoSize = true;
        this.lblProjStartDate.Location = new System.Drawing.Point(3, 42);
        this.lblProjStartDate.Name = "lblProjStartDate";
        this.lblProjStartDate.Size = new System.Drawing.Size(106, 13);
        this.lblProjStartDate.TabIndex = 999;
        this.lblProjStartDate.Text = "Projected Start Date:";
        // 
        // lblDateDiscovered
        // 
        this.lblDateDiscovered.AutoSize = true;
        this.lblDateDiscovered.Location = new System.Drawing.Point(3, 3);
        this.lblDateDiscovered.Name = "lblDateDiscovered";
        this.lblDateDiscovered.Size = new System.Drawing.Size(90, 13);
        this.lblDateDiscovered.TabIndex = 999;
        this.lblDateDiscovered.Text = "Date Discovered:";

enter image description here

2 个答案:

答案 0 :(得分:2)

您可以简单地将此代码放在Load形式的事件中:

this.ActiveControl= this.tbxBusinessName;

this.tbxBusinessName.Select();

this.Visible = true;
this.tbxBusinessName.Focus();

以上所有代码都可以帮到你。

但为什么 this.tbxBusinessName.TabIndex = 0; 无效?

因为您有多个TabIndex = 0的控件,在这种情况下,控件的z顺序将会检查并且位于顶部的控件将获胜并首先获得焦点。
有关详细信息,请参阅How to: Set the Tab Order on Windows Forms

为什么只是 this.tbxBusinessName.Focus(); 没有用?

因为只有在表单的Control.Focus属性设置为true后,才能在表单的Load事件中使用Visible方法将焦点设置在控件上。您可以设置this.Visible = true;或致电this.Show();

答案 1 :(得分:0)

您可以通过为窗口创建OnLoad功能来手动设置焦点。或者像Lars建议的那样,在想要聚焦的对象上设置最低的tabIndex。