Adding text to text box when event ''MouseHover'' is active - c#

I'm new at C#.
I want to add '#' to HALLO (in the textBox) each time when you hover your mouse over the button.
This is what i have:
public partial class Form1 : Form
{
string Q = "HALLO";
string hashtag = "#";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
tB1.Text = Q;
}
private void bT1_MouseHover(object sender, EventArgs e)
{
tB1.Text += hashtag;
if (Q.Length > 20)
{
tB1.Clear();
}
lBkarakters.Text = Convert.ToString(tB1.Text.Length);
}
}
}
It does add the '#', but HALLO is gone.

Initialize your textbox somewhere (I'd recommend on the Load event handler):
tB1.Text = "HALLO";
Register an event handler for the MouseHover event on the button:
this.yourButton.MouseHover += new System.EventHandler(this.yourButton_MouseHover);
// ...
private void yourButton_MouseHover(object sender, System.EventArgs e)
{
tB1.Text += "#";
}

public partial class Form1 : Form
{
string Q = "HALLO";
string hashtag = "#";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
tB1.Text = Q;
}
private void bT1_MouseHover(object sender, EventArgs e)
{
tB1.Text += hashtag;
}
}
or
public partial class Form1 : Form
{
string Q = "HALLO";
string hashtag = "#";
public Form1()
{
InitializeComponent();
tB1.Text = Q;
}
private void bT1_MouseHover(object sender, EventArgs e)
{
tB1.Text += hashtag;
}
}
Make sure you're event is registered:

Related

Calling Another user control from one user control

I have two user controls usercontrol_1 and usercontrol_2, using a click event i am bring in usercontrol_1 to a panel called panel_screen on my main form.
private void btn_Click(object sender, EventArgs e)
{
if (!panel_screen.Controls.Contains(usercontrol_1.Instance))
{
panel_screen.Controls.Add(usercontrol_1.Instance);
usercontrol_1.Instance.Dock = DockStyle.Fill;
usercontrol_1.Instance.BringToFront();
}
else
usercontrol_1.Instance.BringToFront();
}
Similarly i wanna bring usercontrol_2 to the same panel on the main form using a button (click event) on usercontrol_1.
How do i do this? any help would be appreciated.
I'm not sure where you are having problem, I think the way you are using usercontrol_1.Instance might cause the issue.
I have working example here, you can try it.
private void btn_Click(object sender, EventArgs e)
{
bool userControlIsAlreadyInPanel = false;
//assuming you are cheking if usercontrol_1 is already there on panel
// you don't want to create new usercontrol, but just bring existing control to the front
foreach(UserControl control in panel_screen.Controls)
{
if (control.GetType() == typeof(usercontrol_1))
{
userControlIsAlreadyInPanel = true;
control.BringToFront();
}
}
if(!userControlIsAlreadyInPanel)
{
usercontrol_1 instane = new usercontrol_1();
panel_screen.Controls.Add(instane);
instane.Dock = DockStyle.Fill;
instane.BringToFront();
}
}
output
namespace WindowsFormsApp1
{
public delegate void MyEventDelegate(object sender, string name);
public partial class Form1 : Form
{
usercontrol_1 _ctrl1 = null;
usercontrol_2 _ctrl2 = null;
public Form1()
{
InitializeComponent();
_ctrl1 = new usercontrol_1();
_ctrl1.Dock = DockStyle.Fill;
_ctrl1.userControlButtonClicked += userControlButtonClicked;
_ctrl2 = new usercontrol_2();
_ctrl2.Dock = DockStyle.Fill;
_ctrl2.userControlButtonClicked += userControlButtonClicked;
this.Load += Form1_Load;
}
private void Form1_Load(object sender, EventArgs e)
{
userControlButtonClicked(_ctrl1, "1");
}
private void userControlButtonClicked(object sender, string name)
{
panel1.Controls.Clear();
if (sender.Equals(_ctrl1))
{
panel1.Controls.Add(_ctrl2);
}
else if (sender.Equals(_ctrl2))
{
panel1.Controls.Add(_ctrl1);
}
}
}
}
namespace WindowsFormsApp1
{
public partial class usercontrol_1 : UserControl
{
public event MyEventDelegate userControlButtonClicked;
public usercontrol_1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyEventDelegate med = userControlButtonClicked;
if (med != null)
{
med(this, "1");
}
}
}
}
namespace WindowsFormsApp1
{
public partial class usercontrol_2 : UserControl
{
public event MyEventDelegate userControlButtonClicked;
public usercontrol_2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyEventDelegate med = userControlButtonClicked;
if (med != null)
{
med(this, "2");
}
}
}
}

how to pass string or value from usercontrol to main form in C#

I created a usercontrol that contains many buttons and in the main form I have a textbox.
I add the usercontrol to the main form and I want to click any button on the usercontrol and have the textbox in the main form shows the button text.
The question is how to pass the string of the button in usercontrol to the textbox in the main form? This is what I'm trying to do
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public string a ;
private void button1_Click(object sender, EventArgs e)
{
a = button1.Text;
}
private void button2_Click(object sender, EventArgs e)
{
a = button2.Text;
}
private void button3_Click(object sender, EventArgs e)
{
a = button3.Text;
}
and the main form code is :
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Text = usrCtrl.a;
// usrCtrl come from : Usercontrol1 usrCtrl = new Usercontrol1();
}
and it shows nothing in the textbox.
refer to this answer, you need to create a property changed event.
UserControl.cs class;
public partial class UserControl1 : UserControl
{
public event PropertyChangedEventHandler PropertyChanged;
public UserControl1()
{
InitializeComponent();
}
private string stringA;
public string a
{
get { return stringA; }
set
{
if (value != stringA)
{
stringA = value;
if (PropertyChanged!= null)
{
PropertyChanged(this, new PropertyChangedEventArgs(a));
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
a = button1.Text;
}
private void button2_Click(object sender, EventArgs e)
{
a = button2.Text;
}
private void button3_Click(object sender, EventArgs e)
{
a = button3.Text;
}
private void button4_Click(object sender, EventArgs e)
{
a = button4.Text;
}
}
On Form's Load we need to define the event,
private void Form1_Load(object sender, EventArgs e)
{
cntr.PropertyChanged += Cntr_PropertyChanged; // press tab + tab after += and it will generate the following method automatically.
}
Here is Event;
private void Cntr_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
textBox1.Text = cntr.a.ToString(); //cntr is the instance of UserControl1
}
Hope helps,
Your code to change the textBox1.Text value is in the wrong event handler.
The textBox1_TextChanged event handler only fires when text in that field changes.
What you need to do is put the line:
textBox1.Text = a;
in the click event handlers.

My label disappears when my WebBrowser.Document.Title updates. (C#)

I want to display the current page-name that's on my WebBrowser control on my Label, but when the Document.Title updates, my label disappears instead of showing the current website title. How can i fix this?
public partial class StreamViewer : Form
{
public StreamViewer()
{
InitializeComponent();
webBrowserViewer.DocumentTitleChanged += new EventHandler(webBrowserViewer_DocumentTitleChanged);
}
private void webBrowserViewer_DocumentTitleChanged(object sender, EventArgs e)
{
label2.Text = webBrowserViewer.DocumentTitle;
}
Use the DocumentTitleChanged event
public Form1()
{
InitializeComponent();
webBrowserViewer.DocumentTitleChanged += new EventHandler(webBrowserViewer_DocumentTitleChanged);
}
private void webBrowserViewer_DocumentTitleChanged(object sender, EventArgs e)
{
label2.Text = webBrowser1.DocumentTitle;
}
https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.documenttitlechanged(v=vs.110).aspx

Passing a dynamic String from one form to another in c#

I have 2 forms Form1 and Form2.
In Form1 i have a backgroundWorker.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
backgroundWorker1.RunWorkerAsync();
Form2 frm2 = new Form2();
frm2.Show();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int k = 0;bool b=true;
while (b==true)
{
Thread.Sleep(100);
k++;
backgroundWorker1.ReportProgress(0, "data");
if (k >= 100)
b = false;
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
string str = "";
str+=e.UserState.ToString();
label1.Text += str;
}
}
In Form2 I have one label label2.
How can i display the same content of label1 in form1 on label2 in form 2 dynamically.
Please Help with an example.
You can have an Action that you call when you update the label on Form1
public Action<string> UpdateFormAction {get; set;}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
string str = "";
str+=e.UserState.ToString();
label1.Text += str;
UpdateForm2(label1.Text);
}
private void UpdateForm2(string text)
{
Action<string> handler = UpdateFormAction;
if (handler != null)
handler(text);
}
Then depending on where you instantiate your forms, you can hook up the Action

Adding data to a gridview in c# from multiple forms

How would I go about inserting the text that I have entered in to the textbox in NewActivity into the first column in the datagridview on form1?
Here is the coding I have thus far.
Form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.IsMdiContainer = true;
}
private void viewToolStripMenuItem1_Click(object sender, EventArgs e)
{
}
private void newActivityToolStripMenuItem_Click(object sender, EventArgs e)
{
NewActivity NewAc = new NewActivity();
NewAc.MdiParent = this;
NewAc.Show();
}
private void deleteActivityToolStripMenuItem_Click(object sender, EventArgs e)
{
}
}
}
NewActivity
public partial class NewActivity : Form
{
public string activityName;
public NewActivity()
{
InitializeComponent();
}
private void btnCancel_Click(object sender, EventArgs e)
{
activityName = "";
this.Close();
}
private void btnAddActivity_Click(object sender, EventArgs e)
{
activityName = txtActivityName.Text;
this.Close();
}
}
}
you can insert it in your event click
private void btnAddActivity_Click(object sender, EventArgs e)
{
activityName = txtActivityName.Text;
int index = yourDataGridView.Rows.Add();
DataGridViewRow row = yourDataGridView.Rows[index];
row.Cells[0].Value = activityName ;
this.Close();
}
Here is an example of how to bind data from a textbox control to a DataGrid
// create new row
DataGridViewRow row = new DataGridViewRow();
// create cells
row.CreateCells(this.dataGridView1, textBox1.Text, textBox2.Text, textBox3.Text);
// add to data grid view
this.dataGridView1.Rows.Add(row);
---------------Below is how you would use it in your case--------------------
private void btnAddActivity_Click(object sender, EventArgs e)
{
activityName = txtActivityName.Text;
int index = dgvActivityList .Rows.Add();
DataGridViewRow row = dgvActivityList .Rows[index];
row.Cells[0].Value = activityName;
this.Close();
}

Categories

Resources