Populate textbox in a window using another window - c#

I have an application with two windows. One window contains textboxes while the other window is the keyboard. My problem now is that when I click on my Keyboard window I encounter error. I cannot type on the textbox I clicked on the other window. Here is my code.
When I click one textbox from window I, I trigger this Mouse event.
private void TextBoxPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
textBox = sender as TextBox;
textBox.Focus();
}
I used sender as TextBox because my textboxes are programmatically added to my window. I used it to get the name of the textbox.
And here is my code when I click a button from my Keyboard window.
Let used button 1:
private void button_numeric_1_Click(object sender, RoutedEventArgs e)
{
Screen1 screen1 = new Screen1();
screen1.textBox.Text += "1";
}
Screen1 is the window containing my textboxes.
How I possibly able to type text in my textbox using my created keyboard. I'm using C#. Anyone please help me out.

instead of using new Screen1(); you may need to use the actual instance of the screen, you may pass the same to the keyboard via constructor.
example
class Screen
{
Keyboard keyboard;
public Screen()
{
//pass the screen instance to the keyboard
keyboard = new Keyboard(this);
}
//...
private void TextBoxPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
textBox = sender as TextBox;
textBox.Focus();
//open keyboard etc
keyboard.Show();
}
}
class Keyboard
{
private Screen screenInstance;
public Keyboard(Screen instance)
{
//store the instance in a member variable
screenInstance = instance;
}
//...
private void button_numeric_1_Click(object sender, RoutedEventArgs e)
{
//use the stored screen instance
screenInstance.textBox.Text += "1";
}
public void Show()
{
//display logic etc
}
}
above is just an example based on some assumptions, you may adjust/merge with your code as needed.
you may adjust the same to pass TextBox instance if you have multiple TextBoxes to be used
example
class Screen
{
Keyboard keyboard = new Keyboard();
private void TextBoxPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
textBox = sender as TextBox;
textBox.Focus();
//open keyboard etc
keyboard.Show(textBox);
}
}
class Keyboard
{
private TextBox textBoxInstance;
private void button_numeric_1_Click(object sender, RoutedEventArgs e)
{
//use the stored TextBox instance
textBoxInstance.Text += "1";
}
public void Show(TextBox instance)
{
textBoxInstance = instance;
//display logic etc
}
}

Related

c# referencing a label from another method

I want if mouse leave the visible status change to false,but I get this error message:
Error CS7036 There is no argument given that corresponds to the required formal parameter 'e' of 'Form1.Repair_MouseLeave(object, EventArgs, Label)'
How shoudl I fix it?
private void Repair_MouseHover(object sender, EventArgs e)
{
Label RepairText = new Label();
RepairText = new Label();
RepairText.Location = new Point(161, 12);
RepairText.Text = "This what the program will do";
this.Controls.Add(RepairText);
RepairText.AutoSize = true;
Repair_MouseLeave(RepairText);
}
private void Repair_MouseLeave(object sender, EventArgs e,Label repairtext)
{
repairtext.Visible = false;
}
First of all, we need to set our event handlers for the MouseHover and MouseLeave methods for the Repair control. I am assuming you know how to do this. Still,
binding to the events of Repair control can be achieved using the Properties window of your Form in design mode. Set event handlers to both MouseHover and MouseLeave.
As far as I can see, you are trying to display a label with some text when the mouse is hovering over this Repair control and want to hide it when the mouse leaves it. But you are handling it incorrectly. First of all, calling MouseLeave from inside MouseHover would immediately hide your new label and it would not be displayed at all.
And your method signature for Repair_MouseLeave is also incorrect. A standard event handler takes two parameters: (object sender, EventArgs e)
Implement your event handlers like the following, having the new label repairText as an instance member of your class:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Label repairText;
private void Repair_MouseHover(object sender, EventArgs e)
{
if(repairText == null)
{
repairText = new Label();
repairText.Location = new Point(161, 12);
repairText.Text = "This what the program will do";
repairText.AutoSize = true;
this.Controls.Add(repairText);
}
repairText.Visible = true;
}
private void Repair_MouseLeave(object sender, EventArgs e)
{
if(repairText != null)
{
repairText.Visible = false;
}
}
}

What's the proper event to use for onFocus button after 2nd form is closed?

Using: C# Forms VS2015
What I'm trying to do:
On form1, I have a textbox (tbJobTitle) and a button (bChooseJobTitle -> form2) for a "Job Title" of an employee.
The textbox(enabled=false) displays the chosen Job Title of an employee.
The button bChooseJobTitle opens another form (form2) that has a datagrid and 2 buttons (Choose & Cancel)
using System.Threading;
...
Thread the1;
...
private void bChooseJobTitle_Click(object sender, EventArgs e)
{
the1 = new Thread(OpenNew_tblJobTitle);
the1.SetApartmentState(ApartmentState.STA);
the1.Start();
}
private void OpenNew_tblJobTitle(object obj)
{
Application.Run(new form2());
}
...
I initially set a global string MyVar.Employee_Choose_idJobTitle (default "" ) to store the choosen index primary key if the user selected content and click the Choose button. If the Cancel button is click the MyVar.Employee_Choose_idJobTitle will remain = "".
//... at form2 "Choose" button
private void bChoose_Click(object sender, EventArgs e)
{
MyVar.idJobTitle = dataGridView1.CurrentRow.Cells[0].Value.ToString();
this.Close();
}
private void bCancel_Click(object sender, EventArgs e)
{
this.Close();
}
When form2 is closed either by "Choose" button or "Cancel" button, the focus goes back to form1's bChooseJobTitle button.
How do I trigger this event?
...so that can test if the content of MyVar.idJobTitle is not null and add the proper value to my textbox.
I was looking for the button events like onFocus or activate but could not find any. Do I use form events instead to do this?
Quite simply, use event Form Activate if you like.
private void form1_Activated(object sender, EventArgs e)
{
if (MyVar.idJobTitle != "")
{
tbJobTitle.Text = Choose_idJobTitle;
MyVar.idJobTitle = "";
}
}

custom tooltip in c# .net win forms

I am looking to simulate a custom tooltip the like of you see in websites using c# .NET 4.5 windows forms.This tooltip will basically show status of some Tasks like how many tasks are pending,tasks in process, completed etc.To do this i am using a borderless win form.This winform will have some texts, images etc.I want it to reveal itself on button's mouseHover event and disappear on MouseLeave event.My problem is that on Mousehover event numerous instances of that tooltip form is getting generated and on MouseLeave they are not getting closed.My code is
private void B_MouseHover(object sender, EventArgs e)
{
frmSecQStatToolTipDlg tooltip = new frmSecQStatToolTipDlg();
tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
frmSecQStatToolTipDlg tooltip = new frmSecQStatToolTipDlg();
tooltip.Close();
}
My code is not working, hence please tell me how to do this the correct way.Thanks
You're generating a new instance of the form class every time you get a hover event, and every time you get a leave event. If you want to continue to use this approach I would recommend you use a variable on your main form object to store the reference to your tooltip form. Secondly, you need to not generate a new instance whenever the event handler is called, but only when necessary. I would create your instance the first time your Hover event is called for a particular control, and then dispose of it when your Leave handler is called -- this is under the assumption that the tooltip dialog's constructor loads up different information for each control being hovered over. Like so:
frmSecQStatToolTipDlg f_tooltip;
private void B_MouseHover(object sender, EventArgs e)
{
if(frmSecQStatToolTipDlg == null)
{
f_tooltip = new frmSecQStatToolTipDlg();
}
tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
if(f_tooltip != null)
{
f_tooltip.Close();
f_tooltip = null;
}
}
You should keep a global field for this form, and should not dispose or close it. Just hide it on some events and show again.
Sample Code:
frmSecQStatToolTipDlg tooltip;
private void B_MouseHover(object sender, EventArgs e)
{
if(frmSecQStatToolTipDlg == null)
{
tooltip = new frmSecQStatToolTipDlg();
}
tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
if(frmSecQStatToolTipDlg != null)
{
tooltip.Hide();
}
}
With this logic you'll not have to create tooltip instance again and again and it will not take time to popup if you frequently do this activity.
Declare your tooltip once as readonly and use it without asking anytime if it is null or not.
If you need to Dispose it, implement the IDisposable pattern:
https://msdn.microsoft.com/en-us/library/b1yfkh5e(v=vs.110).aspx
private readonly frmSecQStatToolTipDlg _tooltip = new frmSecQStatToolTipDlg() ;
private void B_MouseHover(object sender, EventArgs e)
{
_tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
_tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
_tooltip.Hide();
}

On listbox double click open new form

Im using windowsForms c# have a listbox which is bound with data items ...So depending on double click of the listbox item i want the corressponding form open...thnx in adv
ListBox has an DoubleClick event. You can access it if you select object, open Events tab in a Properties window.
Double click it and Visual Studio will create an event handler for you like this:
public void ListBox1_OnDoubleClick(object sender, EventArgs e)
{
// here is your code
}
Now, you just need to enter your code.
If you want to open a form with a corresponding item then it will be something like:
public void ListBox1_OnDoubleClick(object sender, EventArgs e)
{
string text = listBox1.Text; // Don't forget to manipulate with it
Form1 form = new Form1();
form.Show();
}
Subscribe to DoubleClick event on ListBox
listBox1.DoubleClick += new System.EventHandler(this.listBox1_DoubleClick);
private void listBox1_DoubleClick(object sender, EventArgs e)
{
// logic here
}
or
listBox1.DoubleClick += (s,e) => { /*logic here */};
I would use the MouseDoubleClick Event, it provides the cursor position in MouseEventArgs so you can easily detect which item got doubleclicked.
void Listbox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
int index = Listbox1.IndexFromPoint(e.Location);
if (index != ListBox.NoMatches)
{
object clickedItem = Listbox1.Items[index];
// open your form here
}
}

Using a generic clear textbox method on event

I've got a program with a lot of text boxes that I've got text in that I want to be cleared on _click and then reset to default if nothing is entered and the user clicks away.
The way I was going to do it is clearly inefficient, having to name the text box each time and I'd like to know how I could go about streamlining it.
this is what I've got at the minute, and I'd have to change the txtUserName for the text box field name each time
private void txtUserName_Click(object sender, EventArgs e)
{
txtUserName.Text = ""
txtUserName.ForeColor = Color.Black;
}
is there a way I can do essentially
private void txtAnyTextBox_Click(object sender, EventArgs e)
{
string caller = //Get this textbox name
this.ClearBoxes(caller)
}
void ClearBoxes(string Caller)
{
Caller.txt.Text = "";
//..... and so on
}
Yes, you can try this (though it's not generic but there is no need for generics in this case):
private void txtAnyTextBox_Click(object sender, EventArgs e)
{
TextBox tb = sender as TextBox;
if(tb != null) tb.Text = "";
}
And you can attach this method to all your textBoxes Click event.
textBox1.Click += txtAnyTextBox_Click;
textBox2.Click += txtAnyTextBox_Click;
I don't think this is gonna work:
void ClearBoxes(string Caller)
{
Caller.txt.Text = "";
//..... and so on
}
If you want to use ClearBoxes method you should pass it your TextBox element.But there is no need for this,you can directly clear your textBox as shown above code.
Also if you want to clear all TextBoxes in the same time,for example one button click you can use this:
private void button1_Click(object sender, EventArgs e)
{
foreach (var tBox in this.Controls.OfType<TextBox>())
{
tBox.Text = "";
}
}
You can use the sender argument for that.
private void txtAnyTextBox_Click(object sender, EventArgs e)
{
var textbox = sender as TextBox;
this.ClearTextbox(textbox)
}
private void ClearTextbox(TextBox textbox)
{
textbox.Text = "";
//...
}
You can get name of textbox from sender of event:
private void txtAnyTextBox_Click(object sender, EventArgs e)
{
TextBox textBox = (TextBox)sender;
string caller = textBox.Name;
this.ClearBoxes(caller); // call your custom method
}
If you want to simply clear textbox text, then you don't need to get its name - you can use it's Clear() method:
private void txtAnyTextBox_Click(object sender, EventArgs e)
{
TextBox textBox = (TextBox)sender;
textBox.Clear();
}
Also you can consider creation of custom textbox, which will have some default value and will resent itself to default when clicked:
public class CustomTextBox : TextBox
{
public string DefaultText { get; set; }
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
Text = DefaultText;
}
}
Use custom textboxes instead of default textboxes, and provide DefaultText value for each custom textbox which should reset itself to something more meaningful than empty string (you can use Properties window for that).
This would be quite nasty - as you'd cause a page reload every time someone clicked in the text box.
A far simpler way would be to do it in javascript.
just add a function to clear the text box, and then maybe use a css selector to enable the function for every text box you want to use it in.
e.g.
<input type="text" class="clearme" />
$(".clearme").click(function() {
$(this).val('');
});
this will do it all client side without causing any post backs.

Categories

Resources