Adding different controls having same names in C# Windows Form - c#

I am working in C# windows forms application in which I am adding 3 different controls having same name (a button, a textBox & a Label) to my form.
Why there is error in button4_Click?
CODE:
private void button1_Click(object sender, EventArgs e)
{
TextBox myControl = new TextBox();
myControl.Name = "myControl";
this.Controls.Add(myControl);
}
private void button2_Click(object sender, EventArgs e)
{
Button myControl = new Button();
myControl.Name = "myControl";
this.Controls.Add(myControl);
}
private void button3_Click(object sender, EventArgs e)
{
Label myControl = new Label();
myControl.Name = "myControl";
this.Controls.Add(myControl);
}
private void button4_Click(object sender, EventArgs e)
{
((ComboBox)this.Controls["myControl"]).Text = "myCombo"; // works
((TextBox)this.Controls["myControl"]).Text = "myText"; // error
((Label)this.Controls["myControl"]).Text = "myLabel"; // error
}

The Controls[string] indexer returns the first control whose name matches the string. It will be hit and miss with your code but you probably have a ComboBox already added to the form with that same name. The next statements go kaboom because you cannot cast a ComboBox to a TextBox.
Of course, do try to do the sane thing, give these controls different names.

this.Controls["myControl"] returns the first control named myControl.
This is a TextBox, not a Label.
Instead of accessing them through the Controls collection, you should store your controls in fields in the form class (perhaps using List<T>s).

Here is one idea that might help you:
void SetControlText(Type controlType, string controlName, string text) {
foreach (var ctl in this.Controls.OfType<Control>()) {
if (ctl.GetType() == controlType && ctl.Name == controlName) {
ctl.Text = text;
break;
}
}
}
Or with LINQ only:
var item = this.Controls.OfType<Control>().Where(j => j.GetType() == controlType && j.Name == controlName).FirstOrDefault();
if (item != null)
item.Text = text;
Simply call the above function like so:
SetControlText(typeof(Button), "myButton", "Text was set!");
This function will iterate through all of the controls on the form, and when it finds the control type you specify with the name you specify, it will update the controls .Text field.

Related

Real Hint in TextBox Visual Studio C# [duplicate]

This question already has answers here:
Watermark TextBox in WinForms
(11 answers)
Closed 4 years ago.
I'm currently making a Windows Forms Application on Visual Studio in C# and I'm trying to find a way to have a real hint.
I've found a lot of answers online on how to have some text preset there, Some examples even show how to grey out the text to look like a placeholder, but that's not what I'm looking for.
I want a grayed out text that you don't have to backspace to type something there. So I want it to behave like an HTML placeholder like the "Search Q&A" search bar on stack Overflow.
Is there an easy way to do this, like configuring a property of the textbox in the designer on Visual Studio?
This might be the ugliest code but I think you can improve it.
This following class is merely an extension of the standard TextBox
class PHTextBox : System.Windows.Forms.TextBox
{
System.Drawing.Color DefaultColor;
public string PlaceHolderText {get;set;}
public PHTextBox(string placeholdertext)
{
// get default color of text
DefaultColor = this.ForeColor;
// Add event handler for when the control gets focus
this.GotFocus += (object sender, EventArgs e) =>
{
this.Text = String.Empty;
this.ForeColor = DefaultColor;
};
// add event handling when focus is lost
this.LostFocus += (Object sender, EventArgs e) => {
if (String.IsNullOrEmpty(this.Text) || this.Text == PlaceHolderText)
{
this.ForeColor = System.Drawing.Color.Gray;
this.Text = PlaceHolderText;
}
else
{
this.ForeColor = DefaultColor;
}
};
if (!string.IsNullOrEmpty(placeholdertext))
{
// change style
this.ForeColor = System.Drawing.Color.Gray;
// Add text
PlaceHolderText = placeholdertext;
this.Text = placeholdertext;
}
}
}
Copy/paste to new cs file entitled PHTextBox.cs.
Go to your graphic designer and add a TextBox.
Go to the designer and change the instiantion line for the textbox as follow:
Now compile but before you do, just make sure the textbox is not the first element to get the focus. Add button for that matter.
Have you tried overlapping a label on the textbox?
On the textbox keypress event you can check the length of the textbox.text and set the label.
On the keypress event..
MyLabel.Visible = String.IsNullOrEmpty(MyTextBox.Text);
Of course you might want to set the default text of the label as well as grey it out too.
Issue with this is if your form is re sizable.
What you want to achieve is not native to windows forms.
I know that this is an old question; However I was searching for a way and I found my answer to be best answer to display a prompt text in a TextBox:
1) Create a class .cs file called for example MyExtensions.cs having a namespace called for example 'Extensions'.
2) Create a method in the TextBox called Init(string prompt) that takes the prompt text you want to display inside the TextBox.
3) Let me stop talking and give you the rest of the code for MyExtensions.cs (The entire code):
MyExtensions.cs
using System.Drawing;
using System.Windows.Forms;
namespace Extensions
{
public static class MyExtensions
{
public static void Init(this TextBox textBox, string prompt)
{
textBox.Text = prompt;
bool wma = true;
textBox.ForeColor = Color.Gray;
textBox.GotFocus += (source, ex) =>
{
if (((TextBox)source).ForeColor == Color.Black)
return;
if (wma)
{
wma = false;
textBox.Text = "";
textBox.ForeColor = Color.Black;
}
};
textBox.LostFocus += (source, ex) =>
{
TextBox t = ((TextBox)source);
if (t.Text.Length == 0)
{
t.Text = prompt;
t.ForeColor = Color.Gray;
return;
}
if (!wma && string.IsNullOrEmpty(textBox.Text))
{
wma = true;
textBox.Text = prompt;
textBox.ForeColor = Color.Gray;
}
};
textBox.TextChanged += (source, ex) =>
{
if (((TextBox)source).Text.Length > 0)
{
textBox.ForeColor = Color.Black;
}
};
}
}
}
Now Assume that you have three TextBox's : tbUsername, tbPassword, tbConfirm:
In your Form_Load(object sender, EventArgs e) method initialize your three TextBox's to have their appropriate Prompt Text Messages:
using Extensions;
namespace MyApp{
public partial class Form1 : Form{
private void Form1_Load(object sender,
EventArgs e){
tbUsername.Init("Type a username");
tbPassword.Init("Type a password");
tbConfirm.Init("Confirm your password");
}
}
}
Enjoy! :)
What about this
private bool hasTextBeenTyped;
private void Form1_Load(object sender, EventArgs e)
{
this.ActiveControl = label1;
textBox1.ForeColor = Color.LightGray;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
hasTextBeenTyped = !String.IsNullOrEmpty(textBox1.Text);
if (hasTextBeenTyped)
{
textBox1.ForeColor = Color.Black;
}
}
private void textBox1_Click(object sender, EventArgs e)
{
if (!hasTextBeenTyped)
{
textBox1.Text = "";
}
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
hasTextBeenTyped = true;
}
the this.ActiveControl = label1; is just to take focus away from the text box initially. If something else already does, don't worry about that line.
Please, take a look at my ControlHintManager class, ControlHintInfo type and ControlHintType enumeration. They are written in Vb.Net, but you can get the idea analyzing the source-code, or compile the library to use it in your C# project without any more effort.
My solution has even better behavior than the StackOverflows hint that you mentioned, taking into account that when the control leave focus, the hint-text should be restored if the string remains empty.
Usage is so friendlly:
ControlHintInfo hint1 =
new ControlHintInfo("I'm a hint text.", font (or nul), Color.Gray,
ControlHintType.Persistent);
ControlHintManager.SetHint(TextBox1, hint1);
To acchieve this by your own, one way is calling the Win32 SendMessage function with the EM_SETCUEBANNER message, however, that will produce a too basic hint with poor behavior, not recommendable,
so a proper way to acchieve this is by taking control of the edit-control text by yourself, handling the Control.HandleCreated, Control.Enter, Control.Leave, Control.MouseDown, Control.KeyDown and Control.Disposed events (as you can see in my linked source-code).
Just use a object to keep track of the control's state (forecolor, text, and optionally the font), then use properlly the event-handlers mentioned to set or restore the text and color.
This is a online C# translation of the most-important code of the linked urls if this help you understand it better:
private static void Control_HandleCreated(object sender, EventArgs e) {
InstanceControlHintFields();
Control ctrl = (Control)sender;
ControlHintInfo hintInfo = controlHintsB(ctrl);
SetProperties(ctrl, hintInfo);
}
private static void Control_Enter(object sender, EventArgs e) {
InstanceControlHintFields();
Control ctrl = (Control)sender;
string ctrlText = ctrl.Text;
ControlHintInfo ctrlDefaults = controlHintsDefaults(ctrl);
ControlHintInfo hintInfo = controlHintsB(ctrl);
switch (hintInfo.HintType) {
case ControlHintType.Normal:
if ((ctrlText.Equals(hintInfo.Text, StringComparison.OrdinalIgnoreCase))) {
RestoreProperties(ctrl, ctrlDefaults);
}
break;
}
}
private static void Control_Leave(object sender, EventArgs e) {
InstanceControlHintFields();
Control ctrl = (Control)sender;
string ctrlText = ctrl.Text;
ControlHintInfo ctrlDefaults = controlHintsDefaults(ctrl);
ControlHintInfo hintInfo = controlHintsB(ctrl);
switch (hintInfo.HintType) {
case ControlHintType.Normal:
if ((ctrlText.Equals(hintInfo.Text, StringComparison.OrdinalIgnoreCase))) {
RestoreProperties(ctrl, ctrlDefaults);
} else if (string.IsNullOrEmpty(ctrlText)) {
SetProperties(ctrl, hintInfo);
}
break;
case ControlHintType.Persistent:
if (string.IsNullOrEmpty(ctrlText)) {
SetProperties(ctrl, hintInfo);
}
break;
}
}
private static void Control_MouseDown(object sender, MouseEventArgs e) {
InstanceControlHintFields();
Control ctrl = (Control)sender;
string ctrlText = ctrl.Text;
ControlHintInfo hintInfo = controlHintsB(ctrl);
switch (hintInfo.HintType) {
case ControlHintType.Persistent:
if ((ctrlText.Equals(hintInfo.Text, StringComparison.OrdinalIgnoreCase))) {
// Get the 'Select' control's method (if exist).
MethodInfo method = sender.GetType.GetMethod("Select", BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, {
typeof(int),
typeof(int)
}, null);
if ((method != null)) {
// Select the zero length.
method.Invoke(ctrl, new object[] {
0,
0
});
}
}
break;
}
}
private static void Control_KeyDown(object sender, KeyEventArgs e) {
Control ctrl = (Control)sender;
string ctrlText = ctrl.Text;
ControlHintInfo ctrlDefaults = controlHintsDefaults(ctrl);
ControlHintInfo hintInfo = controlHintsB(ctrl);
switch (hintInfo.HintType) {
case ControlHintType.Persistent:
if ((ctrlText.Equals(hintInfo.Text, StringComparison.OrdinalIgnoreCase))) {
RestoreProperties(ctrl, ctrlDefaults);
} else if (string.IsNullOrEmpty(ctrlText)) {
RestoreProperties(ctrl, ctrlDefaults, skipProperties: { "Text" });
}
break;
case ControlHintType.Normal:
if (string.IsNullOrEmpty(ctrlText)) {
RestoreProperties(ctrl, ctrlDefaults);
}
break;
}
}
private static void Control_Disposed(object sender, EventArgs e) {
RemoveHint((Control)sender);
}
PS: It is Reflection based to support more variety of controls.

Why does my dynamic textbox not remove on first click?

I started this question and was able to get an answer to my original question. Now the textbox gets removed but only the second time I click the remove button. Here is what I have tried
protected void btnRemoveTextBox_Click(object sender, EventArgs e)
{
foreach (Control control in PlaceHolder1.Controls)
{
var tb = new TextBox();
tb.ID = "Textbox" + counter;
if ((control.ID == tb.ID.ToString()) && (control.ID != null))
{
controlIdList.Remove(tb.ID);
ViewState["controlIdList"] = controlIdList;
}
}
}
When I step through using breakpoints and error debugging the code runs through twice without error however on the second time through it removes the button.
Because you created and added textboxes in LoadViewState method (earlier in the the page's life cycle), and here only remove an id from controlIdList but not from the control tree. Note: you do not need to create new TextBox instances in btnRemoveTextBox_Click method.
protected void btnRemoveTextBox_Click(object sender, EventArgs e)
{
foreach (Control control in PlaceHolder1.Controls)
{
string id = "Textbox" + counter;
if (control.ID == id)
{
controlIdList.Remove(id);
PlaceHolder1.Controls.Remove(control);
break;
}
}
}

Canceling textboxes if I type in other textbox

I got few textboxes in my form. I would like to type in one textbox and the other textboxes will be automatically canceled (Canceled so the user wont be able to type anymore in the other textboxes).
If the user delete the word he typed in the textbox (or in other words: if the textbox is empty) all other textboxes will be enabled again.
Here is what I got so far:
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text.Trim().Length > 0)
{
// Disable other textboxes (I have no idea how can I do that).
}
else if (textBox1.Text.Trim().Length == 0)
{
// Enable other textboxes
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (textBox1.Text.Trim().Length > 0)
{
// Keep this comboBox enabled while typing in textBox1.
}
}
How can I disable other textboxes while typing in priticillar textbox? (also keeping comboBox enabled).
DO NOTICE: I would like to do the same on textBox2 (when I type in textBox2, textbox 1 and 3 will be disabled) and textBox3.
You don't need a condition on your 'else if', just 'else' will do, as if the length is more than 0, the only other possibility is 0. Also you can use the sender instead of hardcoding the control name.
Then set the Enabled property for the textbox's you want disabled. You can loop through all the textbox's on the form, excluding the one you are typing into, or just manually list them. SImpler is putting the textbox's in a groupbox, then if you disable the groupbox it will disable the controls with in it.
private void textBox1_TextChanged(object sender, EventArgs e)
{
var senderTextBox = (TextBox)sender;
var textBoxesEnabled = senderTextBox.Text.Trim().Length == 0;
textBox2.Enabled = textBoxesEnabled;
textBox3.Enabled = textBoxesEnabled;
// OR
groupBox1.Enabled = textBoxesEnabled;
}
REPLY EDIT: You can chain of textbox's, say 4 of them, disable the last 3, then:
void TextBox1TextChanged(object sender, System.EventArgs e)
{
var isTextEmpty = ((TextBox)sender).Text.Trim() == "";
textBox2.Enabled = !isTextEmpty;
}
void TextBox2TextChanged(object sender, System.EventArgs e)
{
var isTextEmpty = ((TextBox)sender).Text.Trim() == "";
textBox1.Enabled = isTextEmpty;
textBox3.Enabled = !isTextEmpty;
}
void TextBox3TextChanged(object sender, System.EventArgs e)
{
var isTextEmpty = ((TextBox)sender).Text.Trim() == "";
textBox2.Enabled = isTextEmpty;
textBox4.Enabled = !isTextEmpty;
}
void TextBox4TextChanged(object sender, System.EventArgs e)
{
var isTextEmpty = ((TextBox)sender).Text.Trim() == "";
textBox3.Enabled = isTextEmpty;
}
But for a large amount of textbox's, another alternative is having multiple textbox share the same TextChanged event. You need to click on each TextBox control, go into the Events list and manually select the method for TextChanged. Here is the method:
private void TextBoxGroup_TextChanged(object sender, EventArgs e)
{
var groupOrder = new List<TextBox>() { textBox1, textBox2, textBox3, textBox4 };
var senderTextBox = (TextBox)sender;
var senderIndex = groupOrder.IndexOf(senderTextBox);
var isTextEmpty = senderTextBox.Text.Trim() == "";
if (senderIndex != 0) groupOrder[senderIndex - 1].Enabled = isTextEmpty;
if (senderIndex != groupOrder.Count - 1) groupOrder[senderIndex + 1].Enabled = !isTextEmpty;
}
Use Controls collection and some LINQ:
if (textBox1.Text.Trim().Length > 0)
{
var textboxes = this.Controls.OfType<TextBox>().Where(x => x.Name != "textBox1");
foreach(var tBox in textboxes)
tBox.Enabled = false;
}

How to make visible another combo box on selection of a value in current combo box?

I have a combobox(CB1) and it contains items like 1,2,3 and i want to make another combobox (CB2) visible when i select the value 3 from CB1. Which property should i user. I am working on a windows based application and I am using C# as the code behind language. An example would be great to solve the problem.
The combo box CBFormat consists of a list of items as follows:
var allWiegandFormat = WiegandConfigManager.RetrieveAllWiegandFormats();
var allWiegandList = new List<IWiegand>(allWiegandFormat);
CBFormat.Items.Add(allWiegandList[0].Id);
CBFormat.Items.Add(allWiegandList[3].Id);
CBFormat.Items.Add(allWiegandList[4].Id);
CBFormat.Items.Add(allWiegandList[5].Id);
CBProxCardMode.Items.Add(ProxCardMode.Three);
CBProxCardMode.Items.Add(ProxCardMode.Five);
Now I want to show the Combo box of CBPorxCardMode when i select the second item from CBFormat combo box.
Try this
Private void CB1_SelectedIndexChanged(object sender, System.EventArgs e)
{
Combobox CB = (ComboBox) sender;
if(CB.SelectedIndex != -1)
{
int x = Convert.ToInt32(CB.Text)
if(x == 3)
{
CB2.Visible = True;
}
}
}
Use SelectionChangeCommitted event and subscribe your CB1 to it:
// In form load or form initialization
cb1.SelectionChangeCommitted += ComboBoxSelectionChangeCommitted;
// Event
private void ComboBoxSelectionChangeCommitted(object sender, EventArgs e)
{
cb2.Visible = cb1.SelectedItem != null && cb1.Text == "3";
}
If it is Winforms You can use Something Like this
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add(1);
comboBox1.Items.Add(2);
comboBox1.Items.Add(3);
comboBox2.Visible = false;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem.ToString() == "3")
{
comboBox2.Visible = true;
}
else
{
comboBox2.Visible = false;
}
}
Hope this helps.,
Start with the CB2 Visible property set to False and add a event handler code for the SelectedIndexChanged on the CB1 through the WinForms designer
private void ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
ComboBox comboBox = (ComboBox) sender;
if(comboBox.SelectedItem != null)
{
int id = Convert.ToInt32(comboBox.SelectedItem)
cbo2.Visible = (id == 3)
}
}
This is supposing the ID, that you are adding at the first combo, is an Integer value as it seems.
Also rembember that SelectedIndexChanged event will be called even if you change the SelectedItem programmatically and not just when the user changes the value. Also, if the user change again the selection moving away from the ID==3 the method will set again the Cbo2 not visible.

Get name of control when mouse hovers over it C#

I am adding ComboBoxes dynamically at runtime as shown below.
The problem that I am having is that i do not know which of the comboboxes the user is using.
For eg. The user decides to add 5 comboBoxes to the form, and then goes to the first comboBox,and selects a value, I need to retrieve the value of that comboBox.
What the below code is doing - My approach
I am adding a comboBox to a FlowlayoutPanel and the retrieve its name based on the mouse co-ordinates.... this by the way is not working... and I have no idea what to do.
Any help is greatly appreciated.
public partial class Form1 : Form
{
int count = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
count += 1;
ComboBox cb = new ComboBox();
cb.Name = count.ToString();
cb.MouseHover += new EventHandler(doStuff);
Label lb = new Label();
lb.Text = count.ToString();
flowLayoutPanel1.Controls.Add(cb);
flowLayoutPanel1.Controls.Add(lb);
}
public void doStuff(object sender, EventArgs e)
{
label1.Text = flowLayoutPanel1.GetChildAtPoint(Cursor.Position).Name;
}
}
}
You could try:
cb.SelectionChangeCommitted += selectionChangedHandler
...
void selectionChangedHandler(object sender, EventArgs e) {
ComboBox cb = (ComboBox)sender;
label1.Text = cb.Name;
// Do whatever else is needed with the combo box
}
The SelectionChangeCommitted event is "raised only when the user changes the combo box selection", which sounds like what you're after.
The combobox that raised the event in your doStuff-eventhandler is in the sender-parameter. Try casting it to a checkbox likte this:
ComboBox boxThatRaisedTheEvent = (ComboBox)sender;
string text = ((ComboBox)this.GetChildAtPoint(pt)).Text;
public void DoStuff(object sender, EventArgs e)
{
var comboBox = sender as ComboBox;
var name = (comboBox != null ? comboBox.Name : null);
}
this code casts the 'sender' parameter to a ComboBox object and if the cast is done correctly assings the ComboBox name to the string 'name', otherwise 'name' is null.
Tip: The C# coding style suggests that method names should start with capitalized letter.
You can try something like:
flowLayoutPanel1.Controls.OfType<ComboBox>().FirstOrDefault(cb => cb.Name.Equals(NAME_OF_COMBOBOX))
Or better:
ComboBox box = (ComboBox)sender;

Categories