c# TextBox Enter event fired without user action - c#

I use Enter event in textbox to manage my placeholder but the Enter event fire when my form is show without user action.
How can I fix it ?
Form loginWindow = new Form();
loginWindow.Show();
private void IdTextBox_Enter(object sender, EventArgs e)
{
if (IdTextBox.Text == "Identifiant")
{
IdTextBox.Text = "";
IdTextBox.ForeColor = Color.Black;
}
}

Related

User control click binding not working

I have a problem with my user control(SefAttributes.ascx). I have 2 textbox, 1 label and 1 button in my user control.In main page(SantiyeGiris2.aspx), I am data binding.I am adding this user control dynamically and I want to delete that row when user clicks delete button, but my click event not working.
My user control code behind like below:
public event EventHandler SefSilClicked;
protected void btnSefSil_Clicked(object sender, EventArgs e)
{
if (this.SefSilClicked != null)
{
this.SefSilClicked(this, e);
}
}
In SantiyeGiris2.aspx.cs I am binding like:
protected void rptSantifeSefDataBound(object sender, RepeaterItemEventArgs e)
{
SefAttributes sa = (SefAttributes)(e.Item.FindControl("sfDiv"));
SantiyePersonel sp = (SantiyePersonel)(e.Item.DataItem);
sa.txtSefBas.Text = sp.BaslangicFormatlanmis;
if (Convert.ToDateTime(sa.txtSefBas.Text) < System.DateTime.Now)
sa.txtSefBas.Enabled = false;
sa.txtSefBit.Text = sp.BitisFormatlanmis;
sa.lbl.Text = sp.PersonelTamAdi;
sa.SefSilClicked += new EventHandler(RptSefSilClicked);
}
and click function
protected void RptSefSilClicked(object sender, EventArgs e)
{
YP_SANTIYE.WindowScripts.Alert("click activated.");
}
I can't see the problem.I will grateful if anyone can help me.

Is it possible to refresh a separate WebBrowser control on text change without losing focus on textbox?

I'm designing an article editor for my company and I'd like to be able to show a live preview of the article in a separate WebBrowser window/control. The WebBrowser control needs to refresh the page every time the user changes anything in one of the fields for the article.
Previously, I had the WebBrowser control on the same form, but for space reasons, I had to break it out onto a separate form and access it using a button on the editor form. However, since I moved that control into a separate form, the WebBrowser gains focus on every refresh, meaning I can type one character and then I have to click back to the textbox I was typing in.
My question: Is there a way to refresh that preview page in the background without it stealing the focus so that I can update the preview to reflect what the user is typing without interrupting the user while typing?
Here are the methods for showing and refreshing the preview, respectively:
private void buttonShowPreview_Click(object sender, EventArgs e)
{
if (buttonShowPreview.Tag == null)
{
Form browserForm = new Form();
browserForm.FormClosing += new FormClosingEventHandler(delegate(Object form, FormClosingEventArgs args)
{
if (args.CloseReason == CloseReason.UserClosing)
{
args.Cancel = true;
browserForm.Hide();
previewShowing = false;
}
});
browserForm.Size = new System.Drawing.Size(1024, 768);
browserForm.DesktopLocation = new System.Drawing.Point(0, 0);
browserForm.Text = "Article Preview";
preview = new WebBrowser();
browserForm.Controls.Add(preview);
preview.Dock = DockStyle.Fill;
preview.Navigate("about:blank");
buttonShowPreview.Tag = browserForm;
}
Form previewForm = buttonShowPreview.Tag as Form;
previewForm.Show();
previewShowing = true;
RefreshPreview();
}
private void RefreshPreview(string jumpToAnchor)
{
if (preview != null)
{
preview.Document.OpenNew(true);
preview.Document.Write(structuredContent.GetStructuredContentHTML(content, jumpToAnchor, false));
preview.Refresh();
}
}
Based on the answer by Robberechts here, try disabling the parent Form, updating your WebBrowser, then re-enabling the parent Form again in the DocumentCompleted() event:
private void buttonShowPreview_Click(object sender, EventArgs e)
{
if (buttonShowPreview.Tag == null)
{
Form browserForm = new Form();
browserForm.FormClosing += new FormClosingEventHandler(delegate(Object form, FormClosingEventArgs args)
{
if (args.CloseReason == CloseReason.UserClosing)
{
args.Cancel = true;
browserForm.Hide();
}
});
preview = new WebBrowser();
preview.DocumentCompleted += preview_DocumentCompleted; // handle the DocumentCompleted() event
browserForm.Controls.Add(preview);
preview.Dock = DockStyle.Fill;
preview.Navigate("about:blank");
buttonShowPreview.Tag = browserForm;
}
Form previewForm = buttonShowPreview.Tag as Form;
previewForm.Size = new System.Drawing.Size(1024, 768);
previewForm.DesktopLocation = new System.Drawing.Point(0, 0);
previewForm.Text = "Article Preview";
RefreshPreview();
previewForm.Show();
}
private void RefreshPreview(string jumpToAnchor)
{
if (preview != null && preview.Parent != null)
{
preview.Parent.Enabled = false; // disable parent form
preview.Document.OpenNew(true);
preview.Document.Write(structuredContent.GetStructuredContentHTML(content, jumpToAnchor, false));
preview.Refresh();
}
}
private void preview_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser wb = sender as WebBrowser;
if (wb.Parent != null)
{
wb.Parent.Enabled = true; // re-enable parent form
}
}

C# how to pass value from form to form through datagridview?

i have form, it goes like this
that nomor tabungan is auto generate
the problem is, i want to get nomor nasabah value from different form, through the datagridview. take a look at the following picture
there's a button ambil to retrieve the nomor nasabah value, and pass them to nomor nasabah textbox.text
i have successfully to get the nomor_nasabah value from datagridview. it is shown like the following picture
how do i pass the value to the tambah tabungan . nomor nasabah textbox.text ?
i have set the textbox modifier into public so when i click the ambil button, the textbox is filled with the retrieved value automatically.
how do i do that ?
i did the following code, and idk why it doesn't work
here is the ambil button code
private void button2_Click(object sender, EventArgs e)
{
Tabungan.Tambah tambahtabungan = new Tabungan.Tambah();
//nomornya is the retrieved value
tambahtabungan.textBox2.Text = nomornya;
}
here is the cari button code, to show getCustomer form
private void button2_Click(object sender, EventArgs e)
{
if (getcustomer == null || getcustomer.IsDisposed)
{
getcustomer = new getNasabah();
getcustomer.Show();
}
}
You should try this.
first make a public property on getCustomer form like
public string nomornyaValue { get; set;}
and modify your ambil button click event like, and set this property to your datagrid value.
private void button2_Click(object sender, EventArgs e)
{
nomornyaValue = nomornya;
this.DialogResult = DialogResult.OK;
}
and on tambah tabungan button Cari click call getCustomer form like
private void Cari_Click(object sender, EventArgs e)
{
/*getCustomer getCustomerForm = new getCustomer();
if(getCustomerForm.ShowDialog() == DialogResult.OK)
{
textBox2.Text = getCustomerForm.nomornyaValue;
}*/
if (getcustomer == null || getcustomer.IsDisposed)
{
getcustomer = new getNasabah();
}
if(getcustomer.ShowDialog() == DialogResult.OK)
{
textBox2.Text = getcustomer.nomornyaValue;
}
}

Log in/log out/restrictions

I have built application in multiple winforms. First form is a login form. When user logs in, it opens him another form, let say form 2, and then from there I have menu strip which takes user further to form 3,4,5 and so on.
What I want is to put a button on a upper right corner and call it LOG OUT.This button will appear on all forms(only not on first one of course), so when user logs in, do what he needs to do, and then to have ability to log out,regardless on what form he is on. When he logs out the first form will pop out again! How can this be done? Is it possible to close form 1 (log in form, parent) and not shut down the whole application (children forms) after log in?
Next thing I need is to put restrictions... What I mean by that is that there will be different type of users, regular ones and admins, which will have more options available. I have done the login part, checked if there is user name and password from database that match e.g. textbox1 and textbox2 but I need some advice to implement what I just described above.
Thanks, Bane
private void button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection con1 = getConnection();
con1.Open();
SqlCommand com1 = new SqlCommand();
com1.Connection = con1;
com1.CommandType = CommandType.Text;
com1.CommandText = "select * from radnik WHERE username = '" + textBox2.Text + "' AND password = '" + textBox3.Text + "'";
SqlDataReader reader = com1.ExecuteReader();
if (reader.Read())
{
MessageBox.Show("Uspesno ste se ulogovali!");
Form2 form2 = new Form2();
form2.Show();
}
else { MessageBox.Show("Doslo je do greske!"); }
}
catch (Exception ee)
{
MessageBox.Show(ee.StackTrace);
}
Refresh();
}
this checks info for log in
private void stoloviToolStripMenuItem1_Click(object sender, EventArgs e)
{
var form3 = new Form3();
form3.Show();
}
private void stoloviToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void sifrarnikToolStripMenuItem_Click(object sender, EventArgs e)
{
var form4 = new Form4();
form4.Show();
}
private void rezervacijeToolStripMenuItem_Click(object sender, EventArgs e)
{
var form6 = new Form6();
form6.Show();
}
private void porudzbineToolStripMenuItem_Click(object sender, EventArgs e)
{
var form7 = new Form7();
form7.Show();
}
private void magacinToolStripMenuItem_Click(object sender, EventArgs e)
{
var form9 = new Form9();
form9.Show();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 fr1 = new Form1();
fr1.Show();
}
this just lead user to different winforms
That's a typical MDI application. The Main-Form (the parent of the MDI Child) will have a button in the left upper corner which is activated when the user logs in. The login prompt's form will be a modal form. When the user clicks the logout button you should close all the children forms and then presents again the modal login form. Regarding the user's privileges is not difficult to do, just have a field in the database with that particular piece of information. It could be a byte an integer or whatever you like. Let's say 1=Normal user, 2=Admin user, etc.
I believe you would need 3 classes:
a login form, where the user will authenticate by inserting the credentials and clicking the loginButton
a main form, to hold the menu strip, where the other forms could be shown by pressing the menu buttons
the child form, which will have a logoutButton and other custom controls (so basically you can derive other child forms from it)
In the login form, handle the login button click like this:
private void loginButton_Click(object sender, EventArgs e)
{
if (Authenticated(username.Text, password.Text))
{
var mainForm = new MainForm();
this.Visible = false;
mainForm.Show(this);
}
}
The main form, add a handler for the form closing event and some handlers for showing the children:
public MainForm()
{
InitializeComponent();
FormClosing += MainForm_FormClosing;
}
void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
this.Owner.Visible = true;
}
private void form1ToolStripMenuItem_Click(object sender, EventArgs e)
{
var childForm = new ChildForm();
childForm.Show(this);
}
In the child form, when clicking on the logoutButton, get a reference to the owner of the child form (which will be the main form), close it's owned forms (all it's children) and close the main form itself:
private void logoutButton_Click(object sender, EventArgs e)
{
var owner = ((MainForm)this.Owner);
foreach (var childForm in owner.OwnedForms)
{
childForm.Close();
}
owner.Close();
}

keyDown event is not working on textbox

I a have simple login form and I have set it's accept button property to "OK" button,
and I have a textbox "username" which I set it's KeyDown event to do some processing.
the Ok has enabled set to false.
btnOk.Enbled = false;
this.AcceptButton = btnOk;
txtUsername.KeyDown += new KeyEventHandler(KeyDownHandle);
when I hit enter in the username textbox I do some processing and then set accept button enabled to true.
private void KeyDownHandle(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// some processing
btnOk.Enabled = true;
txtPassword.Focus();
}
}
then I write password in password textbox and hit enter so "OK.Click" is triggered.
but the problem is the keyDown is not working because "accept button".
what can I do to solve this?
Edit: Just want to say the problem is resolved if I set acceptButton to "none" but that's not what I'm looking for.
Why don't you remove this.AcceptButton = btnOk from the constrctor (I suppose) and put it in the KeyDownHandler, that way btnOk will accept Enter key only after it is enabled and after the username has been inserted. so the code should be like this:
btnOk.Enabled = false;
txtUsername.KeyDown += new KeyEventHandler(textBox1_KeyDown);
btnOk.Click += new EventHandler(button1_Click);
private void KeyDownHandle(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// some processing
btnOk.Enabled = true;
this.AcceptButton = btnOk;
txtPassword.Focus();
}
}

Categories