MouseHover event on custom usercontrol c# - c#

I created a usercontrol textbox named ucTextBox contains a label, a textbox and two buttons. I put it many in my form.
Now I want to attach a MouseHover event on each ucTextBox,TextBox and MaskedTextBox in my form.
I do this :
public void AttachHoverEvent(Control CTrl)
{
foreach (Control c in CTrl.Controls)
{
if (c is TextBox || c is MaskedTextBox)
{
c.MouseHover += new EventHandler(afficheDictionnaireChamp);
c.MouseLeave += new EventHandler(desafficheDictionnaireChamp);
continue;
}
if (c is ucTextBox)
{
c.MouseHover += new EventHandler(afficheDictionnaireChamp);
c.MouseLeave += new EventHandler(desafficheDictionnaireChamp);
continue;
}
if (c.HasChildren)
{
AttachHoverEvent(c);
}
}
}
Note : I put ucTextBox in other condition to put a breakpoint and the condition is true.
My code works correctly for TextBox and MaskedTextBox but don't work on my
ucTextBox (Nothing happen).
I try to add this in my ucTextBox class :
private void txbValeur_MouseHover(object sender, EventArgs e)
{
if (this.MouseHover != null)
this.MouseHover(this, e);
}
This my target events function :
public void afficheDictionnaireChamp(object sender, EventArgs e)
{
Dictionnaire dico = new Dictionnaire();
Control snd = (Control)sender;
string table = dico.getNomTable(this.Name, snd.Name);
string champ = dico.getNomChamp(this.Name, snd.Name);
if (table != "" && champ != "")
Globals.FormMain.tslTable.Text = table + " - " + champ;
else
Globals.FormMain.tslTable.Text = "";
}
public void desafficheDictionnaireChamp(object sender, EventArgs e)
{
Globals.FormMain.tslTable.Text = "";
}
If someone got an idea where the problem can from ?
Thanks in advance !
Thomas

Try to set your parent controls background to "Transparent"

Related

Textbox autocomplete - input from custom keyboard on screen

I'm trying to use autocomplete. The input text comes from a custom made keyboard, made from a form.
I tried autocomplete feature from a simple textbox and text input from my keyboard and works fine. But when I input text from the custom keyboard, it doesn't work. The custom keyboard adds the input from a key listener Key_Click.
I tried adding an extra 'a' and adding the text as txtInput.Text += 'o'; but it didn't work.
Any ideas?
keyboard code:
public partial class frmTextInput : Form
{
public string input_Text { get; set; }
public frmTextInput(string TEXT,bool CTRL)
{
InitializeComponent();
AlternarTeclas(chkShift.Checked);
AgregarListenerTeclas();
var source = new AutoCompleteStringCollection();
List<string> box = Data.Data.SourcePatente();
foreach (var item in box)
{
source.Add(item);
}
txtInput.AutoCompleteCustomSource = source;
txtInput.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
txtInput.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
private void btnSpace_Click(object sender, EventArgs e)
{
txtInput.Text = txtInput.Text + " ";
}
private void btnBorrar_Click(object sender, EventArgs e)
{
string str = txtInput.Text;
if (!string.IsNullOrEmpty(str))
{
txtInput.Text = str.TrimEnd(str[str.Length - 1]);
}
}
private void btnVolver_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnEnter_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
input_Text = txtInput.Text;
}
private void frmTextInput_Load(object sender, EventArgs e)
{
}
private void chkShift_CheckedChanged(object sender, EventArgs e)
{
AlternarTeclas(chkShift.Checked);
}
private void Key_Click(object sender, EventArgs e)
{
string key = sender.ToString();
if (chkShift.Checked)
{
key = key.ToUpper();
}
else
{
key = key.ToLower();
}
txtInput.Text = txtInput.Text + key.Substring(key.Length - 1);
}
private void AgregarListenerTeclas()
{
foreach (Control c in tabCaracteres.Controls)
{
if (c.GetType() == typeof(Button))
{
if (c.Text.Length == 1 && c.Text != "←")
{
c.Click += Key_Click;
}
}
}
foreach (Control c in tabSymbol.Controls)
{
if (c.GetType() == typeof(Button))
{
if (c.Text.Length == 1 && c.Text != "←")
{
c.Click += Key_Click;
}
}
}
}
private void AlternarTeclas(bool estaShiftApretado)
{
if (estaShiftApretado)
{
foreach (Control c in tabCaracteres.Controls)
{
if (c.GetType() == typeof(Button))
{
if (c.Text.Length < 2)
{
c.Text = c.Text.ToUpper();
}
}
}
}
else
{
foreach (Control c in tabCaracteres.Controls)
{
if (c.GetType() == typeof(Button))
{
if (c.Text.Length < 2)
{
c.Text = c.Text.ToLower();
}
}
}
}
}
private void btnSymbol_Click(object sender, EventArgs e)
{
tabTeclado.SelectTab(tabTeclado.SelectedIndex + 1);
}
private void btnTecAlfanumerico_Click(object sender, EventArgs e)
{
tabTeclado.SelectTab(tabTeclado.SelectedIndex - 1);
}
private void button1_Click(object sender, EventArgs e)
{
txtInput.Text += 'o';
}
}
txtInput.AutoCompleteCustomSource = source;
txtInput.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
txtInput.AutoCompleteSource = AutoCompleteSource.CustomSource;
This should be put in the designer file of your textbox, not here. Try that, and can you see your textbox text value change when you use the custom keyboard? What I understand is you catch click event for your custom keyboard and change txtInput.Text?
I made it work. First of all, it didn't work with textbox multiline.
Then, the correct way to input new chars was emulate the keyboard:
I was triying in function "Key_Click":
== txtInput.Text = txtInput.Text + key.Substring(key.Length - 1); ==> I don't work
Instead I used:
== txtInput.Focus(); // IMPORTANT
SendKeys.Send(key.Substring(key.Length - 1));

How to set Textbox.Enabled from false to true on TextChange?

I programmatically create a Form with two textboxes. My goal is to disable one textbox if I type something in the second one and contrariwise. I managed to disable second textbox on first textbox textchange,but can't figure out how enable it when the first textbox.Text is empty.
Here is the code :
private void metaName_TextChanged(object sender,EventArgs e)
{
var ctrl = (Control)sender;
var frm = ctrl.FindForm();
TextBox metaTxt = null;
foreach (var ctr in frm.Controls)
{
if (ctr is TextBox)
{
metaTxt = (TextBox)ctr;
if (metaTxt.Name == "metaHTTPEquiv")
{
metaTxt.Enabled = false;
}
else
if (?)
{
}
}
}
}
I want to make something like this :
if(textBox3.Text == String.Empty)
{
textBox4.Enabled = true;
}
else
if(textBox3.Text != String.Empty)
{
textBox4.Enabled = false;
}
You can check only the textchanged event for each one like the following:
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Enabled = !(textBox1.Text.Length >= 1);
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
textBox1.Enabled = !(textBox2.Text.Length >= 1);
}
The self textbox has some values, then the enabled will be false for the other one
First set a flag to enable or disable the second control based on the content of the metaName textbox that raises the event, then search for the second textbox using a bit of Linq.
private void metaName_TextChanged(object sender,EventArgs e)
{
TextBox ctrl = sender as TextBox;
if(ctrl != null)
{
bool enable = !string.IsNullOrEmpty(ctrl.Text);
TextBox secondOne = this.Controls
.OfType<TextBox>()
.FirstOrDefault(x => x.Name == "metaHTTPEquiv");
if(secondOne != null)
secondOne.Enabled = enable;
}
}
The same code, reversing the textboxes roles, could be used as the event handler of the second textbox.
Forget about control events and use data binding.
Take the following helper method
static void Bind(Control target, string targetProperty, object source, string sourceProperty, Func<object, object> expression)
{
var binding = new Binding(targetProperty, source, sourceProperty, true, DataSourceUpdateMode.Never);
binding.Format += (sender, e) => e.Value = expression(e.Value);
target.DataBindings.Add(binding);
}
and just add something like this in your form load event
Bind(textBox2, "Enabled", textBox1, "Text", value => string.IsNullOrEmpty((string)value));

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;
}
}
}

Calling Dynamic controls from a seperate class

I need to know if it is possible to call a control and also attach it's events from a class. I have been researching the internet for a some valuable information but to no avail. Below is a simple illustration of what I intend achieving.
PAGE
protected void Page_Load(object sender, EventArgs e)
{
DynamicControls(IsPostBack);
}
public void DynamicControls(bool posting_back)
{
ControlHandler ch = new ControlHandler();
CreateTextbox(item.id, item.value, item.textMode, item.mandatoryInput, item.maxLength,int.Parse(item.rowNumber), int.Parse(item.colNumber), item.visible, item.autoPostBack,item.enable, table);
}
CLASS
public void CreateTextbox(String id, String value, String textMode, bool mandatoryInput
, String maxLength, int rowNumber, int colNumber, bool visible, bool autopostBack, bool enable, Table table)
{
TextBox tb = new TextBox();
tb.ID = id;
tb.Text = value == null ? "" : value;
tb.TextMode = textMode == null ? TextBoxMode.SingleLine : textMode.ToLower() == "multiline" ? TextBoxMode.MultiLine : TextBoxMode.SingleLine;
tb.MaxLength = maxLength == null ? 32500 : int.Parse(maxLength);
tb.Visible = visible;
tb.Style.Add("width", "80%");
tb.Enabled = enable;
tb.AutoPostBack = autopostBack;
tb.Font.Bold = true;
tb.ForeColor = System.Drawing.Color.Chocolate;
tb.TextChanged += new EventHandler(tb_TextChanged);
}
protected void tb_TextChanged(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;
tb.Text = //some values or display in another control in the form
}
Thanks
It is possible to listen to events yes. Though dynamic controls should always be created during page preinit or page init, not load.

Adding different controls having same names in C# Windows Form

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.

Categories