My english isn't very good.
Hi, I have class where I inherit from GroupBox and I want to use polymorphism and I see in debugger that all is corrent but after compilation I see nothing...
Here is screenshot like it is and how it should be.
grid = new Grid.KierownikGrid();
SetGrid();
private void SetGrid()
{
grid.Location = new System.Drawing.Point(1, 0);
grid.Size = new System.Drawing.Size(10,10);
grid.TabIndex = 10;
grid.TabStop = false;
grid.Text = "";
}
public class KierownikGrid : GroupBox
{
RadioButton addUsers;
RadioButton deleteUsers;
RadioButton troubles;
public KierownikGrid()
:base()
{
Inicjacja();
}
protected void Inicjacja()
{
this.Controls.Add(addUsers = new RadioButton());
this.Controls.Add(deleteUsers = new RadioButton());
this.Controls.Add(troubles = new RadioButton());
this.addUsers.AutoSize = true;
this.addUsers.Checked = true;
this.addUsers.Location = new System.Drawing.Point(3, 10);
this.addUsers.TabIndex = 0;
this.addUsers.TabStop = true;
this.addUsers.Text = "Dodaj użytkownika";
this.addUsers.UseVisualStyleBackColor = true;
this.deleteUsers.AutoSize = true;
this.deleteUsers.Location = new System.Drawing.Point(125, 10);
this.deleteUsers.TabIndex = 1;
this.deleteUsers.Text = "Usuń użytkownika";
this.deleteUsers.UseVisualStyleBackColor = true;
this.troubles.AutoSize = true;
this.troubles.Location = new System.Drawing.Point(250, 10);
this.troubles.TabIndex = 2;
this.troubles.Text = "Problemy";
this.troubles.UseVisualStyleBackColor = true;
}
}
https://i.stack.imgur.com/DFu4t.png
https://i.stack.imgur.com/Dqeim.png
As #BugFinder already mentioned you have to add your control to a form.
There is a tutorial for that.
public class Form1 : System.Windows.Forms.Form
{
//Controls.
private TextBox txtBox = new TextBox();
private Button btnAdd = new Button();
private ListBox lstBox = new ListBox();
private CheckBox chkBox = new CheckBox();
private Label lblCount = new Label();
private void Form1_Load(object sender, EventArgs e)
{
//Add controls to the form.
this.Controls.Add(btnAdd);
this.Controls.Add(txtBox);
this.Controls.Add(lstBox);
this.Controls.Add(chkBox);
this.Controls.Add(lblCount);
}
}
Related
I´m stuck on a problem using controls.find and I can´t find the error.
I´m creating a loginform using different panels.
Panels are created as follows:
public Panel CreatePanel()
{
Panel login = new Panel();
TextBox Login_UsernameTB = new TextBox();
TextBox Login_PasswordTB = new TextBox();
Label label1 = new Label();
Label label2 = new Label();
Label loginstatus = new Label();
Button Login_Loginbtn = new Button();
Button Login_Registerbtn = new Button();
PictureBox Login_Logo = new PictureBox();
PictureBox Login_ECard = new PictureBox();
//creatingPanel
login.Location = new Point(0, 0);
login.Name = "Login";
login.Size = new Size(1000, 400);
//locations
Login_ECard.Location = new Point(500, 250);
Login_ECard.SizeMode = PictureBoxSizeMode.StretchImage;
Login_ECard.Image = PatientSimulator.Properties.Resources.Ecard;
Login_ECard.Name = "Login_Ecard";
Login_Logo.Location = new Point(148, 12);
Login_Logo.SizeMode = PictureBoxSizeMode.StretchImage;
Login_Logo.Image = PatientSimulator.Properties.Resources.Logo;
Login_Logo.Name = "Login_Logo";
label1.Location = new Point(31, 200);
label1.Size = new Size(58, 13);
label1.Text = "Username:";
label1.Name = "label1";
label2.Location = new Point(31, 244);
label2.Size = new Size(56, 13);
label2.Text = "Password:";
label2.Name = "label2";
Login_UsernameTB.Location = new Point(148, 197);
Login_UsernameTB.Size = new Size(359, 20);
Login_UsernameTB.Text = "Testfirma1";
Login_UsernameTB.Name = "Login_UsernameTB";
Login_PasswordTB.Location = new Point(148, 241);
Login_PasswordTB.Size = new Size(359, 20);
Login_PasswordTB.Text = "Hallo1234#";
Login_PasswordTB.PasswordChar = '*';
Login_PasswordTB.Name = "Login_PasswordTB";
Login_Loginbtn.Location = new Point(432, 334);
Login_Loginbtn.Size = new Size(75, 23);
Login_Loginbtn.Text = "Login";
Login_Loginbtn.Click += Login_Loginbtn_Click;
loginstatus.Location = new Point(323, 360);
loginstatus.Size = new Size(300, 20);
loginstatus.Text = "";
loginstatus.Name = "loginstatus";
Login_Registerbtn.Location = new Point(323, 334);
Login_Registerbtn.Size = new Size(75, 23);
Login_Registerbtn.Text = "Register";
Login_Registerbtn.Click += Login_Registerbtn_Click;
login.Controls.Add(Login_ECard);
login.Controls.Add(Login_Logo);
login.Controls.Add(label1);
login.Controls.Add(label2);
login.Controls.Add(Login_UsernameTB);
login.Controls.Add(Login_PasswordTB);
login.Controls.Add(Login_Loginbtn);
login.Controls.Add(Login_Registerbtn);
login.Controls.Add(loginstatus);
return login;
}
In Form_load:
Panel login = CreatePanel();
login.Visible = true;
Controls.Add(login);
When I start the application, everything is displayed. if I type some text into systemstatus.text isn't gets displayed.
When doing a check if the entered password & username are correct, the strange stuff happens.
private void Login_Loginbtn_Click(object sender, EventArgs e)
{
Patient.DBAccess db = new Patient.DBAccess();
Sha256 sha = new Sha256();
string username = login.Controls.Find("Login_UsernameTB", true)[0].Text;
string userpw = login.Controls.Find("Login_PasswordTB", true)[0].Text;
Patient.DatabaseRequestInterface.UserInterface user = new Patient.DatabaseRequestInterface.UserInterface();
user.Username = username;
if (db.IsUserExisting(user)){
user = db.ReadUserPassword(user);
string salt = Encoding.ASCII.GetString(user.PasswordSalt);
byte[] pw = sha.GetSha256(userpw, salt);
if (pw.SequenceEqual(user.PasswordHash))
{
//Programm starten
//token generieren
Patient.DatabaseRequestInterface.UserInterface useri = db.ReadFullUser(user);
UserAuthentificationToken = new UserInfo(useri.Username, useri.Uuid, DateTime.Now);
LoginSuccessEvent?.Invoke(this, new EventArgs());
login.Controls.Find("loginstatus", true)[0].Text = "Login successfull";
this.Close();
}
else
{
MessageBox.Show("Login failed, Username and/or password incorrect");
}
}
else
{
MessageBox.Show("Falscher User oder Passwort");
//Controls.Find("Login_StatusL", true)[0].Text = "Login failed, Username and/or password incorrect!";
}
}
The first 2 times I use controls.find it works, I get the strings entered by the users at the corresponding TextBoxes. When I try to change loginstatus, I get a System.IndexOutOfRangeException. My interpretation of the exception is, that there was no loginstatus found. And I don´t understand why. (the other 2 elements can´t be found within the if as well)
Can anyone help?
the controls.find("string",true)[0].Text = "XXX" works, I use it a lot at another part of the application
Any ideas?
Thanks in advance,
David
You miss the Name property in your code and Controls.Find strictly work on Name
Login_UsernameTB.Name = "Login_UsernameTB";
So your Code should be something like this:
public Panel CreatePanel()
{
Panel login = new Panel();
login.Size = new Size(900, 900);
TextBox Login_UsernameTB = new TextBox();
Login_UsernameTB.Name = "Login_UsernameTB";
Login_UsernameTB.Location = new Point(50, 50);
Login_UsernameTB.Text = "Username here";
TextBox Login_PasswordTB = new TextBox();
Login_PasswordTB.Name = "Login_PasswordTB";
Login_PasswordTB.Location = new Point(150, 50);
Login_PasswordTB.Text = "password here";
//stuff is asigned to TBs...
Label loginstatus = new Label();
loginstatus.Location = new Point(150, 150);
loginstatus.Size = new Size(300, 20);
loginstatus.Text = "";
loginstatus.Name = "loginstatus";
login.Controls.Add(Login_UsernameTB);
login.Controls.Add(Login_PasswordTB);
login.Controls.Add(loginstatus);
return login;
}
Also, the login should be class level but not defined inside the Form_Load
like this :
public partial class Form1 : Form
{
Panel login;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
login = CreatePanel();
login.Visible = true;
Controls.Add(login);
}
.....
I have tested it and works fine.
My test code is
using System;
using System.Drawing;
using System.Windows.Forms;
namespace SackOverflow
{
public partial class Form1 : Form
{
Panel login;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
login = CreatePanel();
login.Visible = true;
Controls.Add(login);
}
public Panel CreatePanel()
{
Panel login = new Panel();
login.Size = new Size(900, 900);
TextBox Login_UsernameTB = new TextBox();
Login_UsernameTB.Name = "Login_UsernameTB";
Login_UsernameTB.Location = new Point(50, 50);
Login_UsernameTB.Text = "Username";
TextBox Login_PasswordTB = new TextBox();
Login_PasswordTB.Name = "Login_PasswordTB";
Login_PasswordTB.Location = new Point(150, 50);
Login_PasswordTB.Text = "password";
//stuff is asigned to TBs...
Label loginstatus = new Label();
loginstatus.Location = new Point(150, 150);
loginstatus.Size = new Size(300, 20);
loginstatus.Text = "";
loginstatus.Name = "loginstatus";
login.Controls.Add(Login_UsernameTB);
login.Controls.Add(Login_PasswordTB);
login.Controls.Add(loginstatus);
return login;
}
private void button1_Click(object sender, EventArgs e)
{
if (true)
{
string username = login.Controls.Find("Login_UsernameTB", true)[0].Text;
string userpw = login.Controls.Find("Login_PasswordTB", true)[0].Text;
//UserInterface user = //some database stuff to get the password saved by the user
if ("123" == userpw)
{
login.Controls.Find("loginstatus", true)[0].Text = "Logging in";
Login();
}
else
{
login.Controls.Find("loginstatus", true)[0].Text = "Error";
}
}
}
private void Login()
{
MessageBox.Show("Login");
}
}
}
Result is:
I receive the message box MessageBox.Show("Login"); shown and Loginstatus is shown too.
Looking for help.
I have a form which generates fields into a Class extends groupbox.
I am creating a button to clear and then generate a new GUID into a text box but I can seem to access my textbox which has been creating in the Initialize method.
I created a list to store input.
private List<InputSetItem> _inputSetItems = new List<InputSetItem>();
This is where the textbox is created:
public void initialize()
{
//balanceIdentifier
var balanceIdentifierSet = InputGenerator.GenerateInputControl(this, typeof(Guid), "BalanceIdentifier");
Controls.Add(balanceIdentifierSet.Label);
Controls.Add(balanceIdentifierSet.Input);
balanceIdentifierSet.Label.Left = 820;
balanceIdentifierSet.Input.Left = 1050;
balanceIdentifierSet.Input.Width = 400;
balanceIdentifierSet.Label.Top = 20;
balanceIdentifierSet.Input.Top = 20;
_inputSetItems.Add(balanceIdentifierSet);
// btn_Guid
this.btn_Guid.BackColor = System.Drawing.Color.Blue;
this.btn_Guid.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point);
this.btn_Guid.ForeColor = System.Drawing.Color.White;
this.btn_Guid.Location = new System.Drawing.Point(1455, 19);
this.btn_Guid.Name = "btn_Guid";
this.btn_Guid.Size = new System.Drawing.Size(75, 26);
this.btn_Guid.TabIndex = 3;
this.btn_Guid.Text = "GENERATE";
this.btn_Guid.UseVisualStyleBackColor = false;
this.btn_Guid.ImageAlign = ContentAlignment.MiddleRight;
this.btn_Guid.TextAlign = ContentAlignment.MiddleLeft;
// Give the button a flat appearance.
this.btn_Guid.FlatStyle = FlatStyle.Flat;
this.Controls.Add(btn_Guid);
this.btn_Guid.Click += new System.EventHandler(this.generateRandomGuid);
}
This is the UI
This is my Generate Method, I cant wrap my head around accesses and updating this textBox
private void generateRandomGuid(object sender, EventArgs e)
{
var delBal = new InputSetItem();
Guid randomGuid = Guid.NewGuid();
//delBal.btn_Guid.Input = randomGuid.ToString();
//delBal.Input.Text;
}
So figured it out.
I had to create a Control instances
private Control _idemp;
Add the textbox assigning it
var IdempotencyRef = InputGenerator.GenerateInputControl(this, typeof(Guid), "IdempotencyReference");
_idemp = IdempotencyRef.Input;
Controls.Add(IdempotencyRef.Label);
Controls.Add(_idemp);
And then updating the method
private void generateRandomGuid(object sender, EventArgs e)
{
Guid randomGuid = Guid.NewGuid();
_idemp.Text = randomGuid.ToString();
}
I need your help on how to bind List to the DataGridView. I tried to use the BindingList<T> but still it does not display the records in my gridview. I tried using the List<T>, but it still does not work.
Below is the code which I used:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ToolClientController ctrl = new ToolClientController();
IpAddressTextbox.Text = ctrl.GetIPv4Config();
PortNumberTextbox.Text = ctrl.GetPortNumber();
}
private void BrowseButton_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
ToolMsgFileTextbox.Text = openFileDialog1.FileName;
}
}
private void UploadButton_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(ToolMsgFileTextbox.Text))
{
FileTypeError.SetError(UploadButton, "Please enter filename.");
return;
}
if (!openFileDialog1.FileName.Contains(".txt"))
{
FileTypeError.SetError(UploadButton, "File should be in .txt");
return;
}
ToolClientController ctrl = new ToolClientController();
List<ToolMessages> test = new List<ToolMessages>();
ToolMessages tool = new ToolMessages();
tool.IsPass = true;
tool.ToolMessageReply = string.Empty;
tool.ToolMessageRequest = "x";
test.Add(tool);
MessageGridViews.AutoGenerateColumns = false;
DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn();
col1.DataPropertyName = "ToolMessageRequest";
col1.HeaderText = "Request";
MessageGridViews.Columns.Add(col1);
BindingList<ToolMessages> bind = new BindingList<ToolMessages>(test);
MessageGridViews.VirtualMode = true;
MessageGridViews.DataSource = bind;
}
}
Based on your code I prepared simple WinForms app:
public Form1()
{
InitializeComponent();
List<ToolMessages> test = new List<ToolMessages>();
ToolMessages tool = new ToolMessages();
tool.IsPass = true;
tool.ToolMessageReply = string.Empty;
tool.ToolMessageRequest = "x";
test.Add(tool);
dataGridView1.AutoGenerateColumns = false;
DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn();
col1.DataPropertyName = "ToolMessageRequest";
col1.HeaderText = "Request";
dataGridView1.Columns.Add(col1);
BindingList<ToolMessages> bind = new BindingList<ToolMessages>(test);
dataGridView1.DataSource = bind;
dataGridView1.Show();
}
I added dataGridView1 into form directly via WinForms designer, so InitializeComponent method looks like:
private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(560, 57);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(240, 150);
this.dataGridView1.TabIndex = 1;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(967, 399);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.gridControl1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.gridView1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
And everything seems to work just fine... Data are bound to grid and ouput looks like:
which is exactly what I would expect to see, so I don't see any real problem with you solution, unless you post bigger piece of your code on which I can reproduce the issue you are experiencing.
It says createPassword and repeatPassword are not in the current context. Why and what happens ?
Code;
public MainPage()
{
InitializeComponent();
TextBox createPassword = new TextBox();
createPassword.Width = 400;
TextBox repeatPassword = new TextBox();
repeatPassword.Width = 400;
Button createButton = new Button();
createButton.Content = "Create New Password";
createButton.Click += new RoutedEventHandler(savePassword);
StackPanel content = new StackPanel();
content.HorizontalAlignment = HorizontalAlignment.Center;
content.Children.Add(createPassword);
content.Children.Add(repeatPassword);
content.Children.Add(createButton);
LayoutRoot.Children.Add(content);
}
void savePassword(object sender, RoutedEventArgs e)
{
string password1 = createPassword.Text;
string password2 = repeatPassword.Text;
}
createPassword and repeatPassword must be class members to use them in different class methods:
TextBox createPassword;
TextBox repeatPassword;
public MainPage()
{
InitializeComponent();
createPassword = new TextBox();
createPassword.Width = 400;
repeatPassword = new TextBox();
repeatPassword.Width = 400;
Button createButton = new Button();
createButton.Content = "Create New Password";
createButton.Click += new RoutedEventHandler(savePassword);
StackPanel content = new StackPanel();
content.HorizontalAlignment = HorizontalAlignment.Center;
content.Children.Add(createPassword);
content.Children.Add(repeatPassword);
content.Children.Add(createButton);
LayoutRoot.Children.Add(content);
}
void savePassword(object sender, RoutedEventArgs e)
{
string password1 = createPassword.Text;
string password2 = repeatPassword.Text;
}
I am trying to create a windows application where I want to display a group of controls (Combo Box, Text Box and a button) on button click inside a panel.
I have created a code to create controls once but I want to create them again and again on button click one below another.
The code I am using is
public partial class Employee_PayHeads_add : Form
{
private TextBox txtBox = new TextBox();
private Button btnAdd = new Button();
private ComboBox combohead = new ComboBox();
public Employee_PayHeads_add()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.btnAdd.BackColor = Color.Gray;
this.btnAdd.Text = "Remove";
this.btnAdd.Location = new System.Drawing.Point(240, 25);
this.btnAdd.Size = new System.Drawing.Size(70, 25);
this.txtBox.Text = "";
this.txtBox.Location = new System.Drawing.Point(150, 25);
this.txtBox.Size = new System.Drawing.Size(70, 40);
this.combohead.Location = new System.Drawing.Point(10, 25);
panel1.Controls.Add(btnAdd);
panel1.Controls.Add(txtBox);
panel1.Controls.Add(combohead);
}
Also I want a vertical scroller in the panel if number controls overlap the space.
Thanks in advance
Inside the button click event create new objects, instead of using the one you declared before.
Try something like that:
public partial class Employee_PayHeads_add : Form
{
private TextBox txtBox = new TextBox();
private Button btnAdd = new Button();
private ComboBox combohead = new ComboBox();
private int txtBoxStartPosition = 150;
private int btnAddStartPosition = 240;
private int comboheadStartPosition = 10;
}
public Employee_PayHeads_add()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
TextBox newTxtBox = new TextBox();
Button newBtnAdd = new Button();
ComboBox newCombohead = new ComboBox();
newBtnAdd.BackColor = Color.Gray;
newBtnAdd.Text = "Remove";
newBtnAdd.Location = new System.Drawing.Point(btnAddStartPosition, 25);
newBtnAdd.Size = new System.Drawing.Size(70, 25);
newTxtBox.Text = "";
newTxtBox.Location = new System.Drawing.Point(txtBoxStartPosition, 25);
newTxtBox.Size = new System.Drawing.Size(70, 40);
newCombohead.Location = new System.Drawing.Point(comboheadStartPosition, 25);
panel1.Controls.Add(newBtnAdd);
panel1.Controls.Add(newTxtBox);
panel1.Controls.Add(newCombohead);
txtBoxStartPosition += 50;
btnAddStartPosition += 50;
comboheadStartPosition += 50;
}
I havent tried your code yet, but it shows that it is always creating the new controls on every click event, but as youo have specified the hardcoaded location for buttons, so it must be creating new controls overlapping each other. so you can change the location dynamically and hopefully it will work
If you want to add the controls again and again you have to create new ones. So rather than defining them in your form like that you have to:
private void button1_Click(object sender, EventArgs e)
{
Button btnAdd = new Button();
btnAdd.BackColor = Color.Gray;
btnAdd.Text = "Remove";
btnAdd.Location = new System.Drawing.Point(240, 25);
btnAdd.Size = new System.Drawing.Size(70, 25);
TextBox txtBox = new TextBox();
txtBox.Text = "";
txtBox.Location = new System.Drawing.Point(150, 25);
txtBox.Size = new System.Drawing.Size(70, 40);
ComboBox combohead = new ComboBox();
combohead.Location = new System.Drawing.Point(10, 25);
panel1.Controls.Add(btnAdd);
panel1.Controls.Add(txtBox);
panel1.Controls.Add(combohead);
}
Now you can remove those private declarations on top of your class.