I'm trying to do something that i would think to be rather simple. I have 2 toolstrip items.
test1: ToolStripMenuItem which should show panel -> test_panel_1
test2: ToolStripMenuItem which should show panel -> panel1
test_panel_1 contains button -> panel_1_button1 which should hide test_panel_1 and show test_panel_2
panel1 contains button -> button1 which should hide test_panel2 and then show panel2
However, when I run the code and click on test1ToolStripMenuItem it shows test_panel_1 like it's supposed to, then when i click on panel_1_button_1 it just clears test_panel_1 and doesn't show test_panel_2. And regardless of what I click first, test2ToolStripMenuItem doesn't show panel1 at all.
Here's my code...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace panel_test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void panel_1_button1_Click(object sender, EventArgs e)
{
test_panel2.Visible = true;
test_panel_1.Visible = false;
}
private void test1ToolStripMenuItem_Click(object sender, EventArgs e)
{
test_panel_1.Visible = true;
panel1.Visible = false;
}
private void button1_click(object sender, EventArgs e)
{
test_panel_1.Visible = false;
test_panel2.Visible = true;
}
private void test2ToolStripMenuItem_Click(object sender, EventArgs e)
{
test_panel2.Visible = false;
panel1.Visible = true;
}
private void button1_Click_1(object sender, EventArgs e)
{
this.panel1.Visible = false;
this.panel2.Visible = true;
}
}
}
and, not sure if this helps, but...
namespace panel_test
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.testToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.test1ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.test2ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.test3ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.test_panel_1 = new System.Windows.Forms.Panel();
this.panel1 = new System.Windows.Forms.Panel();
this.button1 = new System.Windows.Forms.Button();
this.panel2 = new System.Windows.Forms.Panel();
this.label3 = new System.Windows.Forms.Label();
this.test_panel2 = new System.Windows.Forms.Panel();
this.label2 = new System.Windows.Forms.Label();
this.panel_1_button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.menuStrip1.SuspendLayout();
this.test_panel_1.SuspendLayout();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
this.test_panel2.SuspendLayout();
this.SuspendLayout();
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.testToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(467, 24);
this.menuStrip1.TabIndex = 0;
this.menuStrip1.Text = "menuStrip1";
//
// testToolStripMenuItem
//
this.testToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.test1ToolStripMenuItem,
this.test2ToolStripMenuItem,
this.test3ToolStripMenuItem});
this.testToolStripMenuItem.Name = "testToolStripMenuItem";
this.testToolStripMenuItem.Size = new System.Drawing.Size(38, 20);
this.testToolStripMenuItem.Text = "test";
//
// test1ToolStripMenuItem
//
this.test1ToolStripMenuItem.Name = "test1ToolStripMenuItem";
this.test1ToolStripMenuItem.Size = new System.Drawing.Size(99, 22);
this.test1ToolStripMenuItem.Text = "test1";
this.test1ToolStripMenuItem.Click += new System.EventHandler(this.test1ToolStripMenuItem_Click);
//
// test2ToolStripMenuItem
//
this.test2ToolStripMenuItem.Name = "test2ToolStripMenuItem";
this.test2ToolStripMenuItem.Size = new System.Drawing.Size(99, 22);
this.test2ToolStripMenuItem.Text = "test2";
this.test2ToolStripMenuItem.Click += new System.EventHandler(this.test2ToolStripMenuItem_Click);
//
// test3ToolStripMenuItem
//
this.test3ToolStripMenuItem.Name = "test3ToolStripMenuItem";
this.test3ToolStripMenuItem.Size = new System.Drawing.Size(99, 22);
this.test3ToolStripMenuItem.Text = "test3";
//
// test_panel_1
//
this.test_panel_1.Controls.Add(this.test_panel2);
this.test_panel_1.Controls.Add(this.panel_1_button1);
this.test_panel_1.Controls.Add(this.label1);
this.test_panel_1.Location = new System.Drawing.Point(44, 28);
this.test_panel_1.Name = "test_panel_1";
this.test_panel_1.Size = new System.Drawing.Size(442, 317);
this.test_panel_1.TabIndex = 1;
this.test_panel_1.Visible = false;
//
// panel1
//
this.panel1.Controls.Add(this.button1);
this.panel1.Location = new System.Drawing.Point(218, 187);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(328, 318);
this.panel1.TabIndex = 1;
this.panel1.Visible = false;
//
// button1
//
this.button1.Location = new System.Drawing.Point(86, 155);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "panel 3 to 4";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click_1);
//
// panel2
//
this.panel2.Controls.Add(this.panel1);
this.panel2.Controls.Add(this.label3);
this.panel2.Location = new System.Drawing.Point(95, 194);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(398, 260);
this.panel2.TabIndex = 1;
this.panel2.Visible = false;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(86, 118);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(71, 13);
this.label3.TabIndex = 0;
this.label3.Text = "this is panel 4";
//
// test_panel2
//
this.test_panel2.Controls.Add(this.panel2);
this.test_panel2.Controls.Add(this.label2);
this.test_panel2.Location = new System.Drawing.Point(154, 101);
this.test_panel2.Name = "test_panel2";
this.test_panel2.Size = new System.Drawing.Size(358, 237);
this.test_panel2.TabIndex = 3;
this.test_panel2.Visible = false;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(70, 118);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(71, 13);
this.label2.TabIndex = 0;
this.label2.Text = "this is panel 2";
//
// panel_1_button1
//
this.panel_1_button1.Location = new System.Drawing.Point(86, 155);
this.panel_1_button1.Name = "panel_1_button1";
this.panel_1_button1.Size = new System.Drawing.Size(75, 23);
this.panel_1_button1.TabIndex = 2;
this.panel_1_button1.Text = "button1";
this.panel_1_button1.UseVisualStyleBackColor = true;
this.panel_1_button1.Click += new System.EventHandler(this.panel_1_button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(28, 21);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(42, 13);
this.label1.TabIndex = 0;
this.label1.Text = "panel 1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(467, 357);
this.Controls.Add(this.test_panel_1);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "Form1";
this.Text = "Form1";
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.test_panel_1.ResumeLayout(false);
this.test_panel_1.PerformLayout();
this.panel1.ResumeLayout(false);
this.panel2.ResumeLayout(false);
this.panel2.PerformLayout();
this.test_panel2.ResumeLayout(false);
this.test_panel2.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem testToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem test1ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem test2ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem test3ToolStripMenuItem;
private System.Windows.Forms.Panel test_panel_1;
private System.Windows.Forms.Panel test_panel2;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button panel_1_button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button button1;
}
}
I've toyed with Usercontrols a little, and i like the way i can edit and view them much better than panels; however, i don't know how to control hiding and showing them.
Thanks for the help. I know this must be coding 101, but it's something i haven't quite fully figured out yet.
It looks like your test_panel_2 is a child of test_panel_1. panel1 is a child of panel2. This is likely not what you intended. What's happening is that, because test_panel_2 is inside test_panel_1, hiding test_panel_1 also hides test_panel_2. There's a hierarchy there.
There's one or two spots where you set the visibility to false again - I'm not sure if those are correct.
Related
When I try to execute my windows form application, all I see is a completely white blank form. When I look in my form1.cs[Design] everything looks normal, but not when I run it? I can't see the labels, or the buttons or anything. I've never had this issue before and my InitializeComponent I think is where it should be? Here is my Form1.Designer.Cs, or maybe there's an issue in my form1.cs?
partial class Identity
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.label6 = new System.Windows.Forms.Label();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(278, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(116, 87);
this.label1.TabIndex = 0;
this.label1.Text = "VÄLKOMMEN!";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label2
//
this.label2.Location = new System.Drawing.Point(67, 87);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(222, 23);
this.label2.TabIndex = 1;
this.label2.Text = "Skriv in ditt namn och personnummer nedan.:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(67, 181);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(51, 13);
this.label3.TabIndex = 2;
this.label3.Text = "Förnamn:";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(63, 217);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(58, 13);
this.label4.TabIndex = 3;
this.label4.Text = "Efternamn:";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(30, 269);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(161, 13);
this.label5.TabIndex = 4;
this.label5.Text = "Personummer:(ååååmmdd - xxxx)";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(127, 174);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 5;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(127, 210);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 20);
this.textBox2.TabIndex = 6;
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(197, 266);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(164, 20);
this.textBox3.TabIndex = 7;
//
// button1
//
this.button1.Location = new System.Drawing.Point(307, 329);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(87, 51);
this.button1.TabIndex = 8;
this.button1.Text = "Kör!";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(337, 395);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(35, 13);
this.label6.TabIndex = 9;
this.label6.Text = "label6";
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(33, 423);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(492, 296);
this.richTextBox1.TabIndex = 10;
this.richTextBox1.Text = "\"\"";
//
// button2
//
this.button2.Location = new System.Drawing.Point(662, 650);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(116, 69);
this.button2.TabIndex = 11;
this.button2.Text = "Avsluta:";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Identity
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1207, 755);
this.Controls.Add(this.button2);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.label6);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Identity";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button button2;
private EventHandler label1_Click;
}
}
Here I am calling the IntializeComponent in my Form1.cs, I haven't moved it since I started this new program at all.
public partial class Identity : Form
{
public Identity()
{
InitializeComponent();
}
public struct Person
{
public double idok;
public string fname;
public string lname;
public string id;
public string sex;
public string year;
public string fyear;
}
Per the comment trail, you made a new solution, copied some select lines over from the problematic form's designer code, commented out some event handlers that were missing in the designer code, and then were able to load the designer and add the missing event handlers and their body code. The resulting form worked OK
i'm new at c# and excel coding. i want to choose a excel file and show it after that show random row or rows (need to user enter how many row user want) after user pressing a random button and it can be pop up the screen like another form or change in data grid view it doesnt matter. User can choose excel file from computer and i can show it in data grid view but stuck at random row part and cant find anything useful in internet. This is may code(i tried too much thing so maybe some not useful code lines be in the codes):
using ExcelDataReader;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
namespace Excel_D
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataTableCollection tableCollection;
public void button1_Click(object sender, EventArgs e)
{
using(OpenFileDialog openFileDialog=new OpenFileDialog() { Filter="Excel Workbook|*xlsx|Excel 97-2003 Workbook|*xls"})
{
if(openFileDialog.ShowDialog() == DialogResult.OK)
{
txtFileName.Text = openFileDialog.FileName;
using(var stream = File.Open(openFileDialog.FileName, FileMode.Open, FileAccess.Read))
{
using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream))
{
DataSet result = reader.AsDataSet(new ExcelDataSetConfiguration()
{
ConfigureDataTable = (_) => new ExcelDataTableConfiguration() { UseHeaderRow = true }
});
tableCollection = result.Tables;
cBoxSheet.Items.Clear();
foreach (System.Data.DataTable table in tableCollection)
cBoxSheet.Items.Add(table.TableName);
}
}
}
}
}
Random rnd = new Random();
public void label1_Click(object sender, EventArgs e)
{
}
public void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
System.Data.DataTable dt = tableCollection[cBoxSheet.SelectedItem.ToString()];
GridView.DataSource = dt;
}
public void label2_Click(object sender, EventArgs e)
{
}
public void textBox1_TextChanged(object sender, EventArgs e)
{
}
public void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
public void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
}
public void GridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
public void button1_Click_1(object sender, EventArgs e)
{
}
}
}
i dont know this is important or not but it is the designer part of the code:
namespace Excel_D
{
partial class Form1
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.BtnBrowse = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.cBoxSheet = new System.Windows.Forms.ComboBox();
this.txtFileName = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.GridView = new System.Windows.Forms.DataGridView();
this.button2 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.GridView)).BeginInit();
this.SuspendLayout();
//
// BtnBrowse
//
this.BtnBrowse.Location = new System.Drawing.Point(682, 279);
this.BtnBrowse.Name = "BtnBrowse";
this.BtnBrowse.Size = new System.Drawing.Size(75, 20);
this.BtnBrowse.TabIndex = 0;
this.BtnBrowse.Text = "=>";
this.BtnBrowse.UseVisualStyleBackColor = true;
this.BtnBrowse.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 279);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(58, 13);
this.label1.TabIndex = 2;
this.label1.Text = "Dosya Adı:";
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// cBoxSheet
//
this.cBoxSheet.FormattingEnabled = true;
this.cBoxSheet.Location = new System.Drawing.Point(86, 306);
this.cBoxSheet.Name = "cBoxSheet";
this.cBoxSheet.Size = new System.Drawing.Size(121, 21);
this.cBoxSheet.TabIndex = 3;
this.cBoxSheet.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// txtFileName
//
this.txtFileName.Location = new System.Drawing.Point(86, 279);
this.txtFileName.Name = "txtFileName";
this.txtFileName.ReadOnly = true;
this.txtFileName.Size = new System.Drawing.Size(590, 20);
this.txtFileName.TabIndex = 4;
this.txtFileName.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 306);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(37, 13);
this.label2.TabIndex = 5;
this.label2.Text = "Sayfa:";
this.label2.Click += new System.EventHandler(this.label2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(12, 333);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(723, 27);
this.button1.TabIndex = 6;
this.button1.Text = "Rastgele örneklem seçmek için tıklayın.";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click_1);
//
// GridView
//
this.GridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.GridView.Location = new System.Drawing.Point(12, 12);
this.GridView.Name = "GridView";
this.GridView.Size = new System.Drawing.Size(745, 254);
this.GridView.TabIndex = 1;
this.GridView.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.GridView_CellContentClick);
//
// button2
//
this.button2.Location = new System.Drawing.Point(12, 366);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(723, 23);
this.button2.TabIndex = 7;
this.button2.Text = "Rastgele örneklem görmek için tıklayın.";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoScroll = true;
this.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.ClientSize = new System.Drawing.Size(766, 451);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label2);
this.Controls.Add(this.txtFileName);
this.Controls.Add(this.cBoxSheet);
this.Controls.Add(this.label1);
this.Controls.Add(this.GridView);
this.Controls.Add(this.BtnBrowse);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "ExcelÖrneklem";
((System.ComponentModel.ISupportInitialize)(this.GridView)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button BtnBrowse;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ComboBox cBoxSheet;
private System.Windows.Forms.TextBox txtFileName;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DataGridView GridView;
private System.Windows.Forms.Button button2;
}
}
I find the Visual Studio's anchor property control very user friendly.
If I don't have this control, I need to create 4 check-boxes (or a CheckedlistBox):
Top, Bottom, Left, Right.
So this anchor will make the GUI of the software looks neat. How can I create the control like this Visual Studio's anchor property control?
Create your own using a Panel, 1 Button and 4 Checkbox, set chkTop.Appearance = System.Windows.Forms.Appearance.Button
Normal style
Checked style
Code
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.chkTop = new System.Windows.Forms.CheckBox();
this.button1 = new System.Windows.Forms.Button();
this.chkBottom = new System.Windows.Forms.CheckBox();
this.chkRight = new System.Windows.Forms.CheckBox();
this.chkLeft = new System.Windows.Forms.CheckBox();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.BackColor = System.Drawing.Color.White;
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel1.Controls.Add(this.chkLeft);
this.panel1.Controls.Add(this.chkRight);
this.panel1.Controls.Add(this.chkBottom);
this.panel1.Controls.Add(this.chkTop);
this.panel1.Controls.Add(this.button1);
this.panel1.Location = new System.Drawing.Point(43, 49);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(207, 148);
this.panel1.TabIndex = 0;
//
// chkTop
//
this.chkTop.Appearance = System.Windows.Forms.Appearance.Button;
this.chkTop.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
this.chkTop.FlatAppearance.CheckedBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
this.chkTop.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.chkTop.Location = new System.Drawing.Point(95, 2);
this.chkTop.Name = "chkTop";
this.chkTop.Size = new System.Drawing.Size(17, 58);
this.chkTop.TabIndex = 1;
this.chkTop.UseVisualStyleBackColor = false;
//
// button1
//
this.button1.BackColor = System.Drawing.Color.Gray;
this.button1.Enabled = false;
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Location = new System.Drawing.Point(73, 61);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(58, 23);
this.button1.TabIndex = 0;
this.button1.UseVisualStyleBackColor = false;
//
// chkBottom
//
this.chkBottom.Appearance = System.Windows.Forms.Appearance.Button;
this.chkBottom.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
this.chkBottom.FlatAppearance.CheckedBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
this.chkBottom.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.chkBottom.Location = new System.Drawing.Point(95, 85);
this.chkBottom.Name = "chkBottom";
this.chkBottom.Size = new System.Drawing.Size(17, 59);
this.chkBottom.TabIndex = 2;
this.chkBottom.UseVisualStyleBackColor = false;
//
// chkRight
//
this.chkRight.Appearance = System.Windows.Forms.Appearance.Button;
this.chkRight.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
this.chkRight.FlatAppearance.CheckedBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
this.chkRight.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.chkRight.Location = new System.Drawing.Point(132, 64);
this.chkRight.Name = "chkRight";
this.chkRight.Size = new System.Drawing.Size(71, 17);
this.chkRight.TabIndex = 3;
this.chkRight.UseVisualStyleBackColor = false;
//
// chkLeft
//
this.chkLeft.Appearance = System.Windows.Forms.Appearance.Button;
this.chkLeft.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
this.chkLeft.FlatAppearance.CheckedBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
this.chkLeft.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.chkLeft.Location = new System.Drawing.Point(1, 63);
this.chkLeft.Name = "chkLeft";
this.chkLeft.Size = new System.Drawing.Size(71, 17);
this.chkLeft.TabIndex = 4;
this.chkLeft.UseVisualStyleBackColor = false;
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.panel1);
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.CheckBox chkTop;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.CheckBox chkLeft;
private System.Windows.Forms.CheckBox chkRight;
private System.Windows.Forms.CheckBox chkBottom;
}
The AnchorStyles enumeration type has an attribute that determines how this value type configured in the winforms designer.
[Editor("System.Windows.Forms.Design.AnchorEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
So in ordner to do that with your own type, you need to build your type specific UITypeEditor class and tag your type with the Editor attribute.
Get ILSpy or any other .NET disassembler and see what the the AnchorEditor class does and how its structured.
I am extremely new to coding but I have interest so decided to take a class at my community college. I am completing a basic assignment creating a language translator for three words of my choosing. I have followed my book but still getting the errors. I have copied my code and errors below and appreciate any guidance.
-- using System;
namespace Language_Translator_Latin
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(21, 112);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(85, 21);
this.button1.TabIndex = 0;
this.button1.Text = "Hello";
this.button1.UseVisualStyleBackColor = true;
//
// button2
//
this.button2.Location = new System.Drawing.Point(149, 112);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(85, 21);
this.button2.TabIndex = 1;
this.button2.Text = "Happy";
this.button2.UseVisualStyleBackColor = true;
//
// button3
//
this.button3.Location = new System.Drawing.Point(288, 112);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(85, 21);
this.button3.TabIndex = 2;
this.button3.Text = "Good night";
this.button3.UseVisualStyleBackColor = true;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(81, 41);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(201, 13);
this.label1.TabIndex = 3;
this.label1.Text = "Select a word and I will convert it to Latin";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(198, 76);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(35, 13);
this.label2.TabIndex = 4;
this.label2.Text = "label2";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(431, 164);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
}
}
private void HelloButton_Click(object sender, EventArgs e)
{
translationLabel.Text = "Salve";
}
private void HappyButton_Click(object sender, EventArgs e)
{
translationLabel.Text = "Laeta";
}
private void GoodnightButton_Click(object sender, EventArgs e)
{
translationLabel.Text = "Bonum nox"
Errors:
Not too sure what errors you are getting, that part of your question is blank right now.
I do see some issues with the placement of the "private void HelloButton" and downward, it appears to be outside the class itself.
You have two lines which are
}
}
right above the section I mentioned, and it should only be one } followed by your private void HelloButton. Then at the end you are missing one }, plus the closing }.
Here is something closer to what you want. I don't have all of your code so only cleaned up the end part. You need to be more careful about matching up the { and }. Another thing is the placement of #endregion, which should stay up with the automated code which it refers to.
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(431, 164);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private void HelloButton_Click(object sender, EventArgs e)
{
translationLabel.Text = "Salve";
}
private void HappyButton_Click(object sender, EventArgs e)
{
translationLabel.Text = "Laeta";
}
private void GoodnightButton_Click(object sender, EventArgs e)
{
translationLabel.Text = "Bonum nox";
}
}
}
I am adding value to a textbox on webpage.
Here is the Inspect Element code:
<input type="text" name="DERIVED_LAM_GRADE_1$0" id="DERIVED_LAM_GRADE_1$0"
tabindex="90" value="0.00" class="PSEDITBOX" style="width:44px;
text-align:right; " maxlength="6" onchange="addchg_win0(this);oChange_win0=this;">
and here is my C# code:
Which is giving me Exception "Object reference not set to an instance of an object."
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string username = textBox1.Text;
HtmlDocument doc = this.webBrowser1.Document;
doc.GetElementById("DERIVED_LAM_GRADE_1$0").SetAttribute("value", username);
}
}
}
i Dont know whats wrong. i am giving a perfect id and still its throwing exception.
Designer CODE:
namespace WindowsFormsApplication2
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.button1 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
this.SuspendLayout();
//
// webBrowser1
//
this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
this.webBrowser1.Location = new System.Drawing.Point(0, 0);
this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowser1.Name = "webBrowser1";
this.webBrowser1.Size = new System.Drawing.Size(527, 309);
this.webBrowser1.TabIndex = 0;
this.webBrowser1.Url = new System.Uri("http://cms.nust.edu.pk/psp/ps/?cmd=login", System.UriKind.Absolute);
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.Location = new System.Drawing.Point(0, 0);
this.splitContainer1.Name = "splitContainer1";
this.splitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal;
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.button1);
this.splitContainer1.Panel1.Controls.Add(this.label2);
this.splitContainer1.Panel1.Controls.Add(this.label1);
this.splitContainer1.Panel1.Controls.Add(this.textBox2);
this.splitContainer1.Panel1.Controls.Add(this.textBox1);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.webBrowser1);
this.splitContainer1.Size = new System.Drawing.Size(527, 398);
this.splitContainer1.SplitterDistance = 85;
this.splitContainer1.TabIndex = 1;
//
// button1
//
this.button1.Location = new System.Drawing.Point(330, 31);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 4;
this.button1.Text = "Insert";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(149, 61);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(53, 13);
this.label2.TabIndex = 3;
this.label2.Text = "Password";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(149, 15);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(55, 13);
this.label1.TabIndex = 2;
this.label1.Text = "Username";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(208, 58);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 20);
this.textBox2.TabIndex = 1;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(208, 12);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(527, 398);
this.Controls.Add(this.splitContainer1);
this.Name = "Form1";
this.Text = "Form1";
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel1.PerformLayout();
this.splitContainer1.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.WebBrowser webBrowser1;
private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox1;
}
}