Tooltip only shows up when I write something in the textbox - c#

This is my code for a button in C# windows forms. I want the Tooltip to show whenever I hover, but it only works after I insert some text in the textbox
this is the code
private void passwordTextBox_TextChanged(object sender, EventArgs e)
{
ToolTip toolTip3 = new ToolTip();
toolTip3.AutoPopDelay = 5000;
toolTip3.InitialDelay = 1000;
toolTip3.ReshowDelay = 500;
toolTip3.ShowAlways = true;
toolTip3.SetToolTip(this.passwordTextBox, "Not more than 50 characters, no special charachters!");
}

you have set tooltip on wrong event. Best way is to set tooltip on Form Load Event.
private void Form1_Load(object sender, EventArgs e)
{
this.setToolTip();
}
private void setToolTip()
{
ToolTip toolTip3 = new ToolTip();
toolTip3.AutoPopDelay = 5000;
toolTip3.InitialDelay = 1000;
toolTip3.ReshowDelay = 500;
toolTip3.ShowAlways = true;
toolTip3.SetToolTip(this.passwordTextBox, "Not more than 50 characters, no special charachters!");
//Tooltip for other controls
}
Let me know if you still face any issue.

Related

Dynamically Create,Move And Resize The Controls

I have a Code Which Create a TextBox In Run Time and Also Resize and Move the pre-created Controls.
The Problem i am facing is i cannot resize or Move the Control which I create During the Run time.
Here is the Code.
public System.Windows.Forms.TextBox AddNewTextBox()
{
System.Windows.Forms.TextBox txt = new System.Windows.Forms.TextBox();
this.Controls.Add(txt);
txt.Top = cLeft * 25;
txt.Left = 100;
txt.Text = "TextBox " + this.cLeft.ToString();
cLeft = cLeft + 1;
return txt;
}
private void button3_Click_1(object sender, EventArgs e)
{
AddNewTextBox();
}
private void button3_Click(object sender, EventArgs e)
{
ControlMoverOrResizer.Init(textBox1);
cboWorkType.SelectedIndex = 0;
}
The problem I am facing is I don't know how to refer the Newly created Textbox in
ControlMoverOrResizer.Init(textBox1);
I tried to Call
txt.Text
But it is throwing the error of
cannot convert String to Windows.form.controls.
Please Guide me where I am making mistake.
Thanks
You have to get a reference to the control. Either store it as a field in your form, Or give it a Name and find it later in the controls collection.
If you only add one text box then it's easy:
Method 1:
Add a field to your form, and assign it when you create the TextBox
private void button3_Click_1(object sender, EventArgs e)
{
this.myTextBox = AddNewTextBox();
}
Later:
ControlMoverOrResizer.Init(this.myTextBox);
Method 2: Give it name and find it dynamically later:
private void button3_Click_1(object sender, EventArgs e)
{
var txt = AddNewTextBox();
txt.Name = "MyTextBox";
}
Later:
ControlMoverOrResizer.Init(this.Controls["MyTextBox"]);
Note that if you add more than one TextBox you will have to adapt this code a bit
You need somewhere to reference from. Maybe add your textboxes to a dictionary.
private Dictionary<string, TextBox> dynamicTextBoxes = new Dictionary<string, TextBox>();
public System.Windows.Forms.TextBox AddNewTextBox()
{
System.Windows.Forms.TextBox txt = new System.Windows.Forms.TextBox();
this.Controls.Add(txt);
dynamicTextBoxes.Add($"tb{cLeft}", txt);
txt.Top = cLeft * 25;
txt.Left = 100;
txt.Text = "TextBox " + this.cLeft.ToString();
cLeft = cLeft + 1;
return txt;
}
private void button3_Click(object sender, EventArgs e)
{
ControlMoverOrResizer.Init(dynamicTextBoxes[$"tb{cLeft - 1}"]);
cboWorkType.SelectedIndex = 0;
}
Note: this will only ever move the last added textbox!

How to implement new tab in CefSharp with correct address and title change?

I am using CefSharp to create a browser. It is working, I can navigate to various websites by using new tab. But when I click on previous tabs, all the tab shows same URL in the address bar and all of them has exactly same title. Here is my code:
private void FormBrowser_Load(object sender, EventArgs e)
{
CefSettings settings = new CefSettings();
Cef.Initialize(settings);
ChromiumWebBrowser browser = new ChromiumWebBrowser(toolStripTextBoxAddress.Text);
browser.Parent = tabControl.SelectedTab;
browser.Dock = DockStyle.Fill;
browser.AddressChanged += Browser_AddressChanged;
browser.TitleChanged += Browser_TitleChanged;
}
// new tab function
public void addNewTab()
{
TabPage tpage = new TabPage();
tpage.Text = "New Tab";
tabControl.Controls.Add(tpage);
tabControl.SelectTab(tabControl.TabCount - 1);
toolStripTextBoxAddress.Text = "";
ChromiumWebBrowser browser = new ChromiumWebBrowser(toolStripTextBoxAddress.Text);
browser.Parent = tpage;
browser.Dock = DockStyle.Fill;
browser.AddressChanged += Browser_AddressChanged;
browser.TitleChanged += Browser_TitleChanged;
}
private void Browser_TitleChanged(object sender, TitleChangedEventArgs e)
{
this.Invoke(new MethodInvoker(() =>
{
tabControl.SelectedTab.Text = e.Title;
}));
}
private void Browser_AddressChanged(object sender, AddressChangedEventArgs e)
{
this.Invoke(new MethodInvoker(() =>
{
toolStripTextBoxAddress.Text = e.Address;
}));
}
// navigate method
private void toolStripTextBoxAddress_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (!string.IsNullOrEmpty(toolStripTextBoxAddress.Text))
{
if (!toolStripTextBoxAddress.Text.Contains("."))
{
getCurrentBrowser().Load("http://www.google.com/search?q=" + toolStripTextBoxAddress.Text);
}
else
{
getCurrentBrowser().Load(toolStripTextBoxAddress.Text);
}
}
}
}
// get current browser
private ChromiumWebBrowser getCurrentBrowser()
{
return (ChromiumWebBrowser)tabControl.SelectedTab.Controls[0];
}
// new tab button
private void toolStripButtonNewTab_Click(object sender, EventArgs e)
{
addNewTab();
}
Here is what I have tried:
private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
{
ChromiumWebBrowser currentBrowser = getCurrentBrowser();
toolStripTextBoxAddress.Text = currentBrowser.Address;
}
When i try to open a new tab it is giving me an error in this line return (ChromiumWebBrowser)tabControl.SelectedTab.Controls[0];
How can I solve this problem? Thanks in advance.
I wrote my multi-tab cefsharp code in a very similar fashion to yours, and encountered the same error.
It was cause by the default number of tab pages. (When you drag tabcontrol to your form, it by default comes with 2 tabpages to start with). From the property panel, I removed those two tabpages, so that the browser starts wit zero tabpage. Any tabpage is only added when you start browsing, by inputting url or clicking favorites.
If you do not set the initial number of tabpages to zero, those two "empty" tabpages have no browser attached to them. Therefore the getcurrentbrowser() function fails to find any browser on those empty tabpages and errors occur.

How to execute event on dynamic generated text box in c# imminently when text box created

I created Dynamic Text Box using List in C# and i want to execute event immediately after text box is created
Following is my code.
private List<TextBox> txtTotalCost = new List<TextBox>();
private void btnMaterialAdd_Click(object sender, EventArgs e)
{
TextBox tbTotalCost = new TextBox();
tbTotalCost.Location = new Point(652, RowCount * 22);
tbTotalCost.Width = 60;
txtTotalCost.Add(tbTotalCost);
tbTotalCost.MouseClick += tbTotalCost_TextChanged;
panel1.Controls.Add(tbTotalCost);
}
void tbTotalCost_TextChanged(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;
TotalCost = CanadianCost * Qut;
tb.Text = TotalCost.ToString();
return;
}
I want to generate or execute event as text box created i do now want to mouse click or text_change.
And want to display multiplication of two integer inside
how can i do that??
Just do this:
private void btnMaterialAdd_Click(object sender, EventArgs e)
{
TextBox tbTotalCost = new TextBox();
tbTotalCost.Location = new Point(652, RowCount * 22);
tbTotalCost.Width = 60;
txtTotalCost.Add(tbTotalCost);
tbTotalCost.MouseClick += tbTotalCost_TextChanged;
panel1.Controls.Add(tbTotalCost);
tbTotalCost_TextChanged(tbTotalCost, null); // <-- this line.
}

How to make a MessageBoxButton move?

I've made a TextBox that retains what you type, and when you click the button associated it gives you a messagebox. When people want to click no, I want the button to change location so people cannot click it so they are forced to click yes. Can you help me? Here is the code:
{
MsgBox = new CustomMsgBox();
MsgBox.label1.Text = Text;
MsgBox.button1.Text = btnOK;
MsgBox.button2.Text = btnCancel;
MsgBox.Text = Caption;
result = DialogResult.No;
MsgBox.ShowDialog();
return result;
}
private void button2_Click(object sender, EventArgs e)
{
button2.Location = new Point(25, 25);
}
private void button2_MouseHover(object sender, EventArgs e)
{
button2.Location = new Point(+50, +50);
}
private void button2_MouseLeave(object sender, EventArgs e)
{
button2.Location = new Point(+100, +100);
}
You will need to create your own form and make it act like a messagebox. Instead of creating a MessageBox, you will instantiate your own form and so that you can handle the buttons on it.

Button with image and not visible text properties

Can I create a button with both .Image and .Text properties simultaniously, in such way, that text is not visible on form, and is created just for identifying what button should do at the moment?
Using TextAlign and TextImageRelation properties doesn't help. Text is always visible, just a position changes.
private System.Windows.Forms.Button bRenameCourse;
this.bRenameCourse.BackColor = System.Drawing.SystemColors.ButtonFace;
this.bRenameCourse.Image = ((System.Drawing.Image)(resources.GetObject("bRenameCourse.Image")));
this.bRenameCourse.Location = new System.Drawing.Point(966, 6);
this.bRenameCourse.Name = "bRenameCourse";
I want this text "Rename" to be not visible on button
this.bRenameCourse.Text = "Rename";
this.bRenameCourse.Size = new System.Drawing.Size(64, 60);
this.bRenameCourse.TabIndex = 10;
this.bRenameCourse.UseVisualStyleBackColor = false;
this.bRenameCourse.Click += new System.EventHandler(this.bRenameCourse_Click);
Here is why do I want it works :
private void bRenameCourse_Click(object sender, EventArgs e)
{
if (bRenameCourse.Text.Equals("Rename"))
{
//DO SMTHNG
bRenameCourse.Text = "OK";
}
else if (bRenameCourse.Text.Equals("OK"))
{
//DO SMTHNG
bRenameCourse.Text = "Rename";
}
}
I can avoid this using some flags, but I'd like to know if it's possible in general.
Don't use the .Text property of the button to store information.You can use the .Tag property
ie
this.bRenameCourse.Tag = "Rename";
And in the Event
private void bRenameCourse_Click(object sender, EventArgs e)
{
if (bRenameCourse.Tag.Equals("Rename"))
{
//DO SMTHNG
bRenameCourse.Tag = "OK";
}
else if (bRenameCourse.Tag.Equals("OK"))
{
//DO SMTHNG
bRenameCourse.Tag = "Rename";
}
}
Just set the .Text property to ""(blank or empty)

Categories

Resources