KeyPressed in textbox not working - c#-4.0

I tried to get the code below working, but to no avail.
I have to make a login window, and except from the Log in button I want by pressing the Enter key in password textbox the result to be the same. C# 2010.
private void button1_Click(object sender, EventArgs e)
{
int ok=0;
if (textBox1.Text == "administrator" && textBox2.Text == "administrator")
{
ok = 1;
this.Hide();
Admin admin = new Admin();
admin.ShowDialog();
}
if (textBox1.Text == "jucator" && textBox2.Text == "jucator")
{
ok = 1;
this.Hide();
}
if (ok == 0)
{
label2.Text = "nume user sau parola incorecta";
label2.Visible = true;
}
}
private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button1_Click(this, new EventArgs());
}
}

You should use the KeyUp event It's more reliable in you case.
Then check for the pressed key.
e.g.:
private void textBox2_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
//DoLogin... in your case it would be
button1_Click(null,null);
e.Handled = true;
}
else
{
e.Handled = false;
}
}
On a side note: I really discourage you to use that Log In method, comparing strings to a hardcoded strings is really a waste, there're better scenarios specially in an environment where you have multiple users Otherwise why would you require a login?

Related

Enable Custom Field on Project Quote Screen

I am using Acumatica 2020 R1. I have a custom text field on the PMQuote DAC. This field is in a custom tab on the project quotes screen. I want that field to always be editable. I put the following in my RowSelected event:
protected virtual void PMQuote_RowSelected(PXCache cache, PXRowSelectedEventArgs e, PXRowSelected del)
{
del?.Invoke(cache, e);
PMQuote quote = e.Row as PMQuote;
cache.AllowUpdate = true;
PXUIFieldAttribute.SetEnabled(cache, e.Row, true);
PXUIFieldAttribute.SetEnabled<PMQuoteExt.usrMyCustomField>(cache, e.Row, true);
}
This didn't work, so I also looked in the automation steps. I didn't see any automation steps available to modify.
I then looked at Workflow. I didn't see any workflows to edit either. I tried creating a new one based on the status field. I added the user field for each status and made sure disabled was unchecked. This didn't work either.
Any ideas on how I can get that field to be enabled regardless of the document status?
Thanks for your help!
AllowUpdate is called only on the cache object, add it for the data view, example:
Base.Quote.AllowUpdate = true
Also, try to extend PXQuoteMaintExt graph extension:
public class PMQuoteMaintExtExtension : PXGraphExtension<PMQuoteMaintExt, PMQuoteMaint>
{
protected virtual void PMQuote_RowSelected(PXCache sender, PXRowSelectedEventArgs e, PXRowSelected sel)
{
sel?.Invoke(sender, e);
}
}
It manages visibility with RowSelected event:
namespace PX.Objects.PM
{
public class PMQuoteMaintExt : PXGraphExtension<PMDiscount, PMQuoteMaint>
{
protected virtual void PMQuote_RowSelected(PXCache sender, PXRowSelectedEventArgs e, PXRowSelected sel)
{
sel?.Invoke(sender, e);
var row = e.Row as PMQuote;
if (row == null) return;
VisibilityHandler(sender, row);
}
private void VisibilityHandler(PXCache sender, PMQuote row)
{
CR.Standalone.CROpportunityRevision revisionInDb = PXSelectReadonly<CR.Standalone.CROpportunityRevision,
Where<CR.Standalone.CROpportunityRevision.noteID, Equal<Required<CR.Standalone.CROpportunityRevision.noteID>>>>.Select(Base, row.QuoteID).FirstOrDefault();
CR.Standalone.CROpportunity opportunityInDb = (revisionInDb == null) ? null : PXSelectReadonly<CR.Standalone.CROpportunity,
Where<CR.Standalone.CROpportunity.opportunityID, Equal<Required<CR.Standalone.CROpportunity.opportunityID>>>>.Select(Base, revisionInDb.OpportunityID).FirstOrDefault();
CR.Standalone.CROpportunity opportunity = PXSelect<CR.Standalone.CROpportunity,
Where<CR.Standalone.CROpportunity.opportunityID, Equal<Required<CR.Standalone.CROpportunity.opportunityID>>>>.Select(Base, row.OpportunityID).FirstOrDefault();
var opportunityIsClosed = opportunity?.IsActive == false;
bool allowUpdate = row.IsDisabled != true && !opportunityIsClosed && row.Status != PMQuoteStatusAttribute.Closed;
if (opportunityInDb?.OpportunityID == opportunity?.OpportunityID)
Base.Caches[typeof(PMQuote)].AllowUpdate = allowUpdate;
else
{
var quoteCache = Base.Caches[typeof(PMQuote)];
foreach (var field in quoteCache.Fields)
{
if (!quoteCache.Keys.Contains(field) &&
field != quoteCache.GetField(typeof(PMQuote.opportunityID)) &&
field != quoteCache.GetField(typeof(PMQuote.isPrimary)))
PXUIFieldAttribute.SetEnabled(sender, row, field, allowUpdate);
}
}
PXUIFieldAttribute.SetEnabled<PMQuote.bAccountID>(sender, row, row.OpportunityID == null);
Base.Caches[typeof(PMQuote)].AllowDelete = !opportunityIsClosed;
foreach (var type in new[]
{
typeof(CR.CROpportunityDiscountDetail),
typeof(CR.CROpportunityProducts),
typeof(CR.CRTaxTran),
typeof(CR.CRAddress),
typeof(CR.CRContact),
typeof(CR.CRPMTimeActivity),
typeof(PM.PMQuoteTask)
})
{
Base.Caches[type].AllowInsert = Base.Caches[type].AllowUpdate = Base.Caches[type].AllowDelete = allowUpdate;
}
Base.Caches[typeof(CopyQuoteFilter)].AllowUpdate = true;
Base.Caches[typeof(RecalcDiscountsParamFilter)].AllowUpdate = true;
Base.Actions[nameof(Base.Approval.Submit)]
.SetVisible(row.Status == PMQuoteStatusAttribute.Draft);
Base.actionsFolder
.SetVisible(nameof(Base.Approval.Approve), Base.Actions[nameof(Base.Approval.Approve)].GetVisible());
Base.actionsFolder
.SetVisible(nameof(Base.Approval.Reject), Base.Actions[nameof(Base.Approval.Reject)].GetVisible());
Base.Actions[nameof(EditQuote)]
.SetVisible(row.Status != PMQuoteStatusAttribute.Draft);
Base.Actions[nameof(Base.Approval.Submit)]
.SetEnabled(row.Status == PMQuoteStatusAttribute.Draft && !opportunityIsClosed);
Base.Actions[nameof(Base.Approval.Approve)]
.SetEnabled(row.Status == PMQuoteStatusAttribute.PendingApproval);
Base.Actions[nameof(Base.Approval.Reject)]
.SetEnabled(row.Status == PMQuoteStatusAttribute.PendingApproval);
Base.Actions[nameof(EditQuote)]
.SetEnabled(row.Status != PMQuoteStatusAttribute.Draft && row.Status != PMQuoteStatusAttribute.Closed);
Base.Actions[nameof(Base.CopyQuote)]
.SetEnabled(Base.Caches[typeof(PMQuote)].AllowInsert);
Base.Actions[nameof(PMDiscount.GraphRecalculateDiscountsAction)]
.SetEnabled((row.Status == PMQuoteStatusAttribute.Draft));
Base.Actions[nameof(Base.PrimaryQuote)].SetEnabled(!String.IsNullOrEmpty(row.OpportunityID) && row.IsPrimary == false && row.Status != PMQuoteStatusAttribute.Closed);
Base.Actions[nameof(Base.SendQuote)].SetEnabled(row.Status.IsIn<string>(PMQuoteStatusAttribute.Approved, PMQuoteStatusAttribute.Sent, PMQuoteStatusAttribute.Closed));
Base.Actions[nameof(Base.PrintQuote)].SetEnabled(true);
Base.convertToProject.SetEnabled( (row.OpportunityID == null || row.IsPrimary == true) && row.QuoteProjectID == null && row.Status.IsIn<string>(PMQuoteStatusAttribute.Approved, PMQuoteStatusAttribute.Sent));
PXUIFieldAttribute.SetEnabled<PMQuote.subject>(sender, row, true);
PXUIFieldAttribute.SetEnabled<PMQuote.status>(sender, row, false);
}
}
}

How to validate all the fields in c# in windows application?

I have a windows form it has 8 textboxes , 3 combobox and a radio button.
I am using this code but it does not work.
I put the beakpoint and checked the control does not go inside if(Cnt is TextBox) It is evaluating to false. I am keeping all the controls in the panel. May be because of this I am having a problem
private void button1_Click(object sender, EventArgs e)
{
foreach (Control cnt in this.Controls)
{
if (cnt is TextBox)
{
if (cnt.Text == "")
{
MessageBox.Show("TextBox is Blank.");
cnt.BackColor = Color.Pink;
}
}
else if (cnt is ComboBox)
{
ComboBox cmb = (ComboBox)cnt;
if (cmb.SelectedItem == null)
{
MessageBox.Show("Combox is not Selected.");
cnt.BackColor = Color.Plum;
}
}
}
}

Adding Duration in dateTimePicker

I have two DateTimePickers one as Start Time and other one as end time.
I also have a text box that has duration.
I would like to know how can I add the duration to start time datetimePicker to automatically get the end time in the endTime dateTimePicker?
Though I believe your Service Duration is a combobox, I've still used textbox as per your question.
Call textchanged. When text in textbox changes, the time is added to dateTimePicker2
private void textBox1_TextChanged(object sender, EventArgs e)
{
int mins = Convert.ToInt32(textBox1.Text);
dateTimePicker2.Value = dateTimePicker1.Value.AddMinutes(mins);
}
For combobox, call textchanged as well.
private void comboBox1_TextChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == -1 || comboBox1.Text == null)
{
dateTimePicker2.Value = dateTimePicker1.Value;
}
else if (comboBox1.Text == "15")
{
dateTimePicker2.Value = dateTimePicker1.Value.AddMinutes(15);
}
else if (comboBox1.Text == "30")
{
dateTimePicker2.Value = dateTimePicker1.Value.AddMinutes(30);
}
else if (comboBox1.Text == "60")
{
dateTimePicker2.Value = dateTimePicker1.Value.AddMinutes(60);
}
}

(J2ME) login page to authenticate username and password and display retrieved data

can someone please help me, i am relatively new in J2ME coding.... i have an API link which contains username and password (e.g. http://sunday-tech.com/chunghua/api/login.php?username=yasir&password=yasir )
i am trying to authenticate the user inputs and access the API and read the data. I am using httpconnection but i am not sure if its the right one to use.
so far the codes do access and display the contents by reading each character but how do i EXCLUDE commas and brackets as well display the data in an organized manner in the phone. Also, when the username and password inputs are wrong, it should alert Invalid Login......
I would appreciate it so much if someone can enlighten about this by providing some code snippets. here is wht i have done so far.
public class Login extends MIDlet implements CommandListener {
TextField UserName = null;
TextField Password = null;
Form authForm, mainscreen;
TextBox t = null;
StringBuffer b = new StringBuffer();
private Display myDisplay = null;
private Command okCommand = new Command("OK", Command.OK, 1);
private Command exitCommand = new Command("Exit", Command.EXIT, 2);
private Command backCommand = new Command("Back", Command.BACK, 2);
private Alert alert = null;
public Login() {
myDisplay = Display.getDisplay(this);
UserName = new TextField("Username", "", 10, TextField.ANY);
Password = new TextField("Password", "", 10, TextField.ANY);
authForm = new Form("Identification");
mainscreen = new Form("Logging IN");
mainscreen.append("Logging in....");
mainscreen.addCommand(backCommand);
authForm.append(UserName);
authForm.append(Password);
authForm.addCommand(okCommand);
authForm.addCommand(exitCommand);
authForm.setCommandListener(this);
myDisplay.setCurrent(authForm);
}
public void startApp() throws MIDletStateChangeException {
}
public void pauseApp() {
}
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException {
}
public void commandAction(Command c, Displayable d) {
if ((c == okCommand) && (d == authForm)) {
if (UserName.getString().equals("") || Password.getString().equals("")) {
alert = new Alert("Error", "You should enter Username and Password", null, AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
myDisplay.setCurrent(alert);
}
else {
//myDisplay.setCurrent(mainscreen);
login(UserName.getString(), Password.getString());
}
}
if ((c == backCommand) && (d == mainscreen)) {
myDisplay.setCurrent(authForm);
}
if ((c == exitCommand) && (d == authForm)) {
notifyDestroyed();
}
}
public void login(String UserName, String PassWord) {
HttpConnection connection = null;
DataInputStream in = null;
String base = "http://sunday-tech.com/chunghua/api/login.php";
String url = base + "?username=" + UserName + "&password=" + PassWord;
OutputStream out = null;
try {
connection = (HttpConnection) Connector.open(url);
connection.setRequestMethod(HttpConnection.POST);
connection.setRequestProperty("IF-Modified-Since", "2 Oct 2002 15:10:15 GMT");
connection.setRequestProperty("User-Agent", "Profile/MIDP-2.1 Configuration/CLDC-1.0");
connection.setRequestProperty("Content-Language", "en-CA");
connection.setRequestProperty("Content-Length", "" + (UserName.length() + PassWord.length()));
connection.setRequestProperty("UserName", UserName);
connection.setRequestProperty("PassWord", PassWord);
out = connection.openDataOutputStream();
out.flush();
in = connection.openDataInputStream();
int ch;
while ((ch = in.read()) != -1) {
b.append((char) ch);
//System.out.println((char)ch);
}
//t = new TextBox("Reply",b.toString(),1024,0);
mainscreen.append(b.toString());
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
if (connection != null) {
connection.close();
}
} catch (IOException x) {
}
myDisplay.setCurrent(mainscreen);
}
}

change the Text of Button

I have a Button in datagridview and its text is Start so when I Click on it the text now should be Stop and when I click again the text should be Start.
So i have written code but its didnt work for me
private void dgvCampaign_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is Button)
{
Button btn = e.Control as Button;
if(btn.Text=="Start")
btn.Text = "Stop";
else
btn.Text = "Start";
}
}
try to use CellContentClick event
private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 2)
{
if (dataGridView2[e.ColumnIndex, e.RowIndex].EditedFormattedValue.ToString().Length > 0)
{
if (dataGridView2[e.ColumnIndex, e.RowIndex].EditedFormattedValue.ToString() == "Start")
{
dataGridView2[e.ColumnIndex, e.RowIndex].Value = "Stop";
}
else
{
dataGridView2[e.ColumnIndex, e.RowIndex].Value = "Start";
}
}
else
dataGridView2[e.ColumnIndex, e.RowIndex].Value = "Start";
}
}

Resources