Tooltip not showing for nested control - c#

I have the following piece of code that works great in all but one instance.
private void tbxLastName_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
{
GetRemainingChars(sender);
}
public void GetRemainingChars(object sender)
{
var control = sender as TextEdit;
var maxChars = control.Properties.MaxLength;
tipCharacterCounter.Show(control.Text.Length + "/" + maxChars, this, control.Location.X, control.Location.Y - control.Height);
}
I just repeat this process from any textbox. Unfortunately, I have one control that is more complicated and I cannot get this to work. The Event portion looks like this -->
private void memDirectionsToAddress_Popup(object sender, EventArgs e)
{
MemoExPopupForm popupForm = (sender as DevExpress.Utils.Win.IPopupControl).PopupWindow as MemoExPopupForm;
MemoEdit meDirections = popupForm.Controls[2] as MemoEdit;
meDirections.EditValueChanging += new DevExpress.XtraEditors.Controls.ChangingEventHandler(meDirections_EditValueChanging);
}
void meDirections_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
{
GetRemainingChars(sender);
}
What I don't understand is if I replace the tipCharacterCounter portion with, say updating a label, it works fine. It's like the ToolTip is hidden or something but I have tried feeding Show() different points to no avail.
Ideas?

Which version of DXPerience are you using? I've tried the following code using DXperience 10.1.5 and it works fine here:
private void memoExEdit1_Popup(object sender, EventArgs e) {
MemoExPopupForm popupForm = (sender as DevExpress.Utils.Win.IPopupControl).PopupWindow as MemoExPopupForm;
MemoEdit meDirections = popupForm.Controls[2] as MemoEdit;
meDirections.EditValueChanging += new DevExpress.XtraEditors.Controls.ChangingEventHandler(meDirections_EditValueChanging);
}
void meDirections_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e) {
GetRemainingChars(sender);
}
public void GetRemainingChars(object sender) {
TextEdit control = sender as TextEdit;
int maxChars = control.Properties.MaxLength;
tipCharacterCounter.ShowHint(control.Text.Length + "/" + maxChars, control, ToolTipLocation.RightBottom);
}

Related

How to standardize many button events C#

Could you tell me how to put together the many button event.
Writing all the many button event is bad Maintainability.
So I want to turn many button event into one method.
Like this...
Before
private void button1_Click(object sender, EventArgs e)
{
//button1 event
}
private void button2_Click(object sender, EventArgs e)
{
//button2event
}
private void buttonN_Click(object sender, EventArgs e)
{
//buttonNevent
}
After
private void buttonClickEvent(object sender, EventArgs e)
{
Button btn = (Button)sender;
int index = int.Parse(btn.Name.Replace("button", ""));
if(index==1)
{
//button1 event
}
if(index==2)
{
//button2 event
}
}
In ASP.NET Web Forms I've solved this situation like this.
Define a general hidden button (this will be the trigger for all). You should define it "hidden" using the styles.
<asp:Button ID="btnPrintPdf" runat="server" Style="display: none" OnClick="btnPrint_Click" />
For the all other buttons "redirect" the click on the client side to the general one like this:
btnPrintPlan.OnClientClick = ClientScript.GetPostBackClientHyperlink(btnPrintPdf, itemData.ClientIw.ID.ToString() + "|" + ((int)PrintDocs.NextStepsPlan).ToString()) + ";return false;";
btnPrintNetWorth.OnClientClick = ClientScript.GetPostBackClientHyperlink(btnPrintPdf, itemData.ClientIw.ID.ToString() + "|" + ((int)PrintDocs.NetWorth).ToString()) + ";return false;";
As you can see, I use a Enum to define what I want to print by clicking different buttons.
The last part is to define the "general" button logic:
protected void btnPrint_Click(object sender, EventArgs e)
{
string sVal = Request.Params["__EVENTARGUMENT"];
if (string.IsNullOrEmpty(sVal))
return;
string[] tks = sVal.Split('|');
if (tks.Length != 2)
return;
string sOrderId = tks[0];
string sPrintType = tks[1];
int orderId = 0;
int iPrintType = 0;
if (!int.TryParse(sOrderId, out orderId) || !int.TryParse(sPrintType, out iPrintType))
return;
string sPdf = null;
if (iPrintType == (int)PrintDocs.NextStepsPlan)
{
....
}//endif
if (iPrintType == (int)PrintDocs.NetWorth)
{
....
}//endif
You could try something like this. Dictionary would be better than if's if you have hundreds of buttons.
private Dictionary<string, Action<object, EventArgs>> buttonEventMap = new Dictionary<string, Action<object, EventArgs>>();
private void setup()
{
buttonEventMap["button1"] = (object sender, EventArgs e)=>{Console.WriteLine("Button 1 Clicked");};
// etc....
}
private void buttonClickEvent(object sender, EventArgs e)
{
Button btn = (Button)sender;
if( buttonEventMap.ContainsKey(btn.Name))
buttonEventMap[btn.Name](sender, e);
}
Although this still is't much different from just implementing each individual ButtonClickEvent.

How to copy, paste, cut in FastColoredTextBox?

I need three functions: Copy, Paste, Cut ,
For a FastColoredTextBox.. so far with my methods, the job is done but afterwards,
the cursor's position get changed and I got no clue on how to keep it where it
was before.
Here's my methods:
private void OnMouseMenuCut(object sender, EventArgs e)
{
var sPoint = rtbScript.SelectionStart;
var ePoint = rtbScript.SelectionLength;
var text = rtbScript.SelectedText;
rtbScript.Text = rtbScript.Text.Remove(sPoint, ePoint);
Clipboard.SetText(text.Replace("\n", "\r\n"));
rtbScript.Text = rtbScript.Text.Insert(sPoint, text);
}
private void OnMouseMenuCopy(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(rtbScript.SelectedText)) return;
Clipboard.SetText(rtbScript.SelectedText.Replace("\n", "\r\n"));
}
private void OnMouseMenuPaste(object sender, EventArgs e)
{
if (!Clipboard.ContainsText()) return;
var index = rtbScript.SelectionStart;
rtbScript.Text = rtbScript.Text.Insert(index, Clipboard.GetText());
}
Also, If there's a better way to do those functions, please post..
Thanks!
For a RichTextBox your code has more issues than loosing the Cursor position, It also looses all formatting! Here are versions that should work better:
private void OnMouseMenuCut(object sender, EventArgs e)
{
var sPoint = rtbScript.SelectionStart;
var text = rtbScript.SelectedText;
rtbScript.Cut();
Clipboard.SetText(text.Replace("\n", "\r\n"));
rtbScript.SelectionStart = sPoint;
}
private void OnMouseMenuCopy(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(rtbScript.SelectedText)) return;
Clipboard.SetText(rtbScript.SelectedText.Replace("\n", "\r\n"));
}
private void OnMouseMenuPaste(object sender, EventArgs e)
{
if (!Clipboard.ContainsText()) return;
var index = rtbScript.SelectionStart;
rtbScript.SelectedText = Clipboard.GetText();
rtbScript.SelectionStart = index + Clipboard.GetText().Length;
}
Note: You must never change the Text property of a RTB or else you will mess up the formating!
Since you wrote that this also works with your FastColoredTextBox I have undeleted the solution..
In the current version of FCTB, these methods already exist inside the FCTB.cs file. They just need to be linked up.

How to handle a group of textbox/label in an array

I have a serie of textboxes and labels form textbox 1-9 and label 1 to 9. With a click on a any label I clear the correspondant textbox.
I created a methode but it's like a baby toy comparison to my procedures in TP or VB. There must be a shortest well structered way. Any idea wiil be very much appreciated?
What I did :)))
private void label1_Click(object sender, EventArgs e)
{
textBox1.Text = "" ;
}
private void label2_Click(object sender, EventArgs e)
{
textBox2.Text = "" ;
}
private void label3_Click(object sender, EventArgs e)
{
textBox3.Text = "" ;
}
private void label4_Click(object sender, EventArgs e)
{
textBox4.Text = "" ;
}
private void label5_Click(object sender, EventArgs e)
{
textBox5.Text = "" ;
}
private void label6_Click(object sender, EventArgs e)
{
textBox6.Text = "" ;
}
private void label7_Click(object sender, EventArgs e)
{
textBox7.Text = "" ;
}
private void label8_Click(object sender, EventArgs e)
{
textBox8.Text = "" ;
}
private void label9_Click(object sender, EventArgs e)
{
textBox9.Text = "" ;
}
You can utilize Tag property to mark controls. Then you can iterate through them (preferably starting from most parent control - form and with the use of recursion! or, if you are sure, from the container, which holds the group of controls).
// assign tag "1" to "9" to labels and texboxes
// subscribe all labels to same event label_Click
private void label_Click(object sender, EventArgs e)
{
string id = (sender as Control).Tag.ToString();
// iterate or recurse
FindTextboxWithId(id).Clear();
}
// it shouldn't be hard to write FindTextboxWithId
Other possibility is to create private arrays of controls, in the form constructor, just to ease referencing them.
public TextBox[] _textBox;
public Form1()
{
InitializeComponent();
_textBox = new TextBox[] {textBox1, texBox2, ..., textBox9};
}
// assign tag "0" to "8" to labels and texboxes
// subscribe all labels to same event label_Click
private void label_Click(object sender, EventArgs e)
{
int index = int.Parse((sender as Label).Tag);
_textBox[index].Clear();
}
Third possibility is to utilize containers, to example, TableLayoutPanel. You can create 2 column container where first column is Label's and second is TextBox'es. Then just fill 9 rows and have fun in OnClick (to find sender position, to find texbox position, to find textbox and to finally clear it).
Perhaps one handler for all and using Controls.Find:
private void label_Click(object sender, EventArgs e)
{
var label = (Label)sender;
string lastDigits = new string(label.Name.SkipWhile(c => !Char.IsDigit(c)).ToArray());
var textBox = Controls.Find("textBox" + lastDigits, true).FirstOrDefault() as TextBox;
if(textBox != null)
textBox.Text = "" ;
}
Although relying on those meaningless variable names is not best practise.
To make your code less redundant, you can loop over the controls in your application:
Control Class, so when clicking on a label you will have to search for the textBox's Tag
that you will set for each textBox.
foreach (Control C in this.Controls)
{
//Code Here...
}
Quick solution:
Rename your labels like: label_1, label_2, ... label_22, then you can use the following common event-handler for all clicks.
An improvement on this would be to just pass labelNr to a separate number, which would then use that to find the textbox by name, instead of using a swith to check all of them. I don't have time to try that now, but I'm sure you can figure it out somehow.. ;)
private void label1_Click(object sender, EventArgs e)
{
var labelNr = ((Label) sender).Name.Split('_').Last();
switch (labelNr)
{
case "1":
textBox_1.Clear();
break;
case "22":
textBox_22.Clear();
break;
}
}
Update: Seems Tim Schmelter had the answer here. To steal a small detail from him: Use Controls.Find("textBox" + labelNr, true) as he shows above instead of the switch here, and you should be set.
And a javascript solution:
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
<asp:Label ID="lbl1" runat="server" AssociatedControlID="txt1" onclick="clearTextBox(this)">Clear</asp:Label>
function clearTextBox(sender){
var assocControlId = sender.htmlFor;
var el = document.getElementById(assocControlId);
if (el)
el.value = '';
}
I would suggest you create a UserControl
Arrange a Lable and a TextBox
handle the label_click event
and uses that UserControl on your form instead.
something like this:
public class LableAndTextBox : UserControl
{
public LableAndTextBox()
{
InitializeComponents();
}
public void label_Click(object sender, EventArgs e)
{
textBox.Text = string.Empty;
}
}
Edit - make sure you create the userControl, in a seperate assembly - for compile reasons..
With two solutions of #sinatr I've created one other method because both are given an error message.
private void label_Click (object sender , EventArgs e)
{
string id = (sender as Control).Tag.ToString();
int newidx = Convert.ToInt32(id);
_textBox[newidx].Clear();
}
THIS WORKS!
Sure! I've added juste here this
namespace WindowsFormsApplication1
{
public partial class
DefBiscuit : Form
{
public TextBox[] _textBox;
And
In form_load this
_textBox = new TextBox[] { textBox1, textBox2, textBox3, textBox4, textBox5, textBox6, textBox7, textBox8, textBox9 };
If you don't like to write code much, i have a program can write it fast.
For example, if you input "lable1.Text = textbox1.Text;" and "15" the program will output into a textbox:
lable1.Text = textbox1.Text;
lable2.Text = textbox2.Text;
lable3.Text = textbox3.Text;
lable4.Text = textbox4.Text;
lable5.Text = textbox5.Text;
lable6.Text = textbox6.Text;
...
lable15.Text = textbox15.Text;
Go here to know more and download: Download Counter Replacer

C# checkedlistbox mouse enter/leave error

I want my checkedlistbox to expand to a certain size when the mouse enters and then go back to a its original size after mouse leaves. Below is the code is have. However, I receive an error when i have another program selected and my mouse goes over the checkedlistbox while the application is not active.
Any suggestions on how to fix?
private void checkedListBox1_MouseEnter(object sender, EventArgs e)
{
Search.ActiveForm.Height = 552;
checkedListBox1.Height = 130;
}
private void checkedListBox1_MouseLeave(object sender, EventArgs e)
{
Search.ActiveForm.Height = 452;
checkedListBox1.Height = 34;}
Error Code - Object Reference not set to an instance of an object.
Try this
private void checkedListBox1_MouseEnter(object sender, EventArgs e)
{
checkedListBox1.Size = new Size(Width,Height);
}
This of course would work so that no exception is thrown, but I hope it's also what you want:
private void checkedListBox1_MouseEnter(object sender, EventArgs e)
{
if(Search.ActiveForm == null) return;
Search.ActiveForm.Height = 552;
checkedListBox1.Height = 130;
}
private void checkedListBox1_MouseLeave(object sender, EventArgs e)
{
if(Search.ActiveForm == null) return;
Search.ActiveForm.Height = 452;
checkedListBox1.Height = 34;
}

Changing the BackColor of a textbox upon entry

I have the following code for my form:
private void txt1_Enter(object sender, EventArgs e)
{
txt1.SelectAll();
txt1.BackColor = Color.LightBlue;
}
private void txt2_Enter(object sender, EventArgs e)
{
txt2.SelectAll();
txt2.BackColor = Color.LightBlue;
}
private void txt1_Leave(object sender, EventArgs e)
{
txtThermalConductivity.BackColor = Color.White;
}
private void txt2_Leave(object sender, EventArgs e)
{
txtThermalConductivity.BackColor = Color.White;
}
There are another 20 textboxes on my form that I would like to do the same for. Is it possible to combine all of the enter events and all of the leave events so I have two events in total rather than 44 individual events?
In your Designer view, select each textbox and set the Enter and Leave events to point to a single implementation of each.
Then you can do this:
private void txt_enter(object sender, EventArgs e) {
((TextBox)sender).BackColor = Color.LightBlue;
}
private void txt_leave(object sender, EventArgs e) {
((TextBox)sender).BackColor = Color.White;
}
Also, SelectAll isn't required because you're setting the entire textbox's background color.. not the SelectionColor of a RichTextBox.
You could either add manually or iterate over all textboxes in form (extension method found here GetChildControls.
foreach (TextBox textBox in this.GetChildControls<TextBox>())
{
textBox.Enter += new EventHandler(TextBox_Enter);
textBox.Leave += new EventHandler(TextBox_Leave);
}
The above can be called from the Form's Load event.
The event listener now can look like the following by casting the sender to TextBox.
private void TextBox_Enter(object sender, EventArgs e)
{
TextBox txtBox = (TextBox)sender;
txtBox .SelectAll();
txtBox .BackColor = Color.LightBlue;
}
private void TextBox_Leave(object sender, EventArgs e)
{
TextBox txtBox = (TextBox)sender;
txtBox.BackColor = Color.White;
}
It is, just use something like the following:
private void tbLeave(object sender, EventArgs e) {
((TextBox) sender).BackColor = Color.White;
}
The set the controls event declaration to point to this function.
You can also do the same for the Leave() event.
(Just a little note to say, I much prefer to handle this kind of thing client side where possible.)

Categories

Resources