i currently have a form that has several check boxes, once all the boxes are checked or left empty, it sends it to the database the state of the check boxes
i want to add, if a box is not checked, when you check it, it should ask for confirmation window "you sure you wanna check this"
any help please?
this is the current code
public IMeditor(IMui IMui, IMuser U, string User)
{
InitializeComponent();
this.IMui = IMui;
imu = U;
if (imu.UID == 0)
{
Add.Text = "Add";
imu.name = user;
}
else
Add.Text = "Update";
AuthChat.Checked = imu.AuthChat == 1;
AuthTac1.Checked = imu.AuthTac1 == 1;
AuthTac2.Checked = imu.AuthTac2 == 1;
AuthTac3.Checked = imu.AuthTac3 == 1;
AuthTac4.Checked = imu.AuthTac4 == 1;
AuthTac5.Checked = imu.AuthTac5 == 1;
AuthTac6.Checked = imu.AuthTac6 == 1;
AuthTac7.Checked = imu.AuthTac7 == 1;
AuthTac8.Checked = imu.AuthTac8 == 1;
AuthTac9.Checked = imu.AuthTac9 == 1;
AuthTac10.Checked = imu.AuthTac10 == 1;
switch (imu.Transport.ToLower()) {
case "aim": Transport.SelectedIndex = 0; break;
case "gtalk": Transport.SelectedIndex = 1; break;
case "msn": Transport.SelectedIndex = 2; break;
case "yahoo": Transport.SelectedIndex = 3; break;
}
}
I agree with Izzy. Add something like
private void PrivateInitialize()
{
CheckBox chkBox = new CheckBox();
chkBox.Text = "Click me";
chkBox.CheckedChanged += new EventHandler(chkBox_CheckedChanged);
}
private void chkBox_CheckedChanged(object sender, EventArgs e)
{
MessageBox.Show("Nag, nag, nag! You take out the trash yet?");
}
You need to handle the event CheckedChangedEvent for the checkbox. You can find the sample code on MSDN. Also
this link for the complete understanding of checkbox.
Or else you can derive your own class from CheckBox control of windows forms then override the required function and implement your own logic and create all the checkboxes in your application from this derived class
public IMeditor(IMui IMui, IMuser U, string User)
{
InitializeComponent();
this.IMui = IMui;
imu = U;
if (imu.UID == 0)
{
Add.Text = "Add";
imu.name = user;
}
else
Add.Text = "Update";
AuthChat.Checked = imu.AuthChat == 1;
AuthTac1.Checked = imu.AuthTac1 == 1;
AuthTac2.Checked = imu.AuthTac2 == 1;
AuthTac3.Checked = imu.AuthTac3 == 1;
AuthTac4.Checked = imu.AuthTac4 == 1;
AuthTac5.Checked = imu.AuthTac5 == 1;
AuthTac6.Checked = imu.AuthTac6 == 1;
AuthTac7.Checked = imu.AuthTac7 == 1;
AuthTac8.Checked = imu.AuthTac8 == 1;
AuthTac9.Checked = imu.AuthTac9 == 1;
AuthTac10.Checked = imu.AuthTac10 == 1;
switch (imu.Transport.ToLower()) {
case "aim": Transport.SelectedIndex = 0; break;
case "gtalk": Transport.SelectedIndex = 1; break;
case "msn": Transport.SelectedIndex = 2; break;
case "yahoo": Transport.SelectedIndex = 3; break;
}
AuthChat.Click += new EventHandler(ClickHandler);
}
void ClickHandler(Object obj, EventArgs args)
{
if (obj == AuthChat && AuthChat.CheckState == CheckState.Checked)
{
DialogResult result = MessageBox.Show("Do you want to change?", "are you sure?", MessageBoxButtons.YesNo);
if (result == DialogResult.No)
{
AuthChat.Checked = false;
}
}
}
public IMeditor(IMui IMui, IMuser U, string User)
{
InitializeComponent();
this.IMui = IMui;
imu = U;
if (imu.UID == 0)
{
Add.Text = "Add";
imu.name = user;
}
else
Add.Text = "Update";
AuthChat.Checked = imu.AuthChat == 1;
AuthTac1.Checked = imu.AuthTac1 == 1;
AuthTac2.Checked = imu.AuthTac2 == 1;
AuthTac3.Checked = imu.AuthTac3 == 1;
AuthTac4.Checked = imu.AuthTac4 == 1;
AuthTac5.Checked = imu.AuthTac5 == 1;
AuthTac6.Checked = imu.AuthTac6 == 1;
AuthTac7.Checked = imu.AuthTac7 == 1;
AuthTac8.Checked = imu.AuthTac8 == 1;
AuthTac9.Checked = imu.AuthTac9 == 1;
AuthTac10.Checked = imu.AuthTac10 == 1;
switch (imu.Transport.ToLower()) {
case "aim": Transport.SelectedIndex = 0; break;
case "gtalk": Transport.SelectedIndex = 1; break;
case "msn": Transport.SelectedIndex = 2; break;
case "yahoo": Transport.SelectedIndex = 3; break;
}
AuthChat.CheckedChanged += new EventHandler(CheckChangedHandler);
AuthChat1.CheckedChanged += new EventHandler(CheckChangedHandler);
}
void CheckChangedHandler(Object obj, EventArgs args)
{
if (obj == AuthChat)
{
MessageBox.Show("Checked changed for AuthChat");
}
else if (obj == AuthChat1)
{
MessageBox.Show("Checked changed for AuthChat1");
}
}
do it with javascript? add OnClientClick="return confirmCheck(this);"
<script language="javascript">
function confirmCheck(e){
return confirm("are you sure?");
// do other stuff
}
</script>
this should be better than redirecting people
Related
I have added the ICSharpCode.TextEditor.dll component to my Visual Studio 2017 reference in my C# project. Next, i want to find out a way to Auto-complete a tag. I have provided a GIF image below on what i mean.
I have provided a code that i tried to debug with the component itself, but it doesn't work.
if (textEditorControl1.Text.Contains("<html>"))
{
textEditorControl1.ActiveTextAreaControl.SelectionManager.SelectedText("</html>");
}
The ICsharpCode.TextEditor component does not have the same property as a normal Textbox.
if (textBox2.Text.Contains("<html>"))
{
textBox2.SelectedText = "</html>";
}
If i try debugging the TextBox version, i get the tag countless times until i get an unhandled System.StackOverflowException.
I found exactly what you're looking for here. The only thing is, that this is a working code for a normal TextBox.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace AutoCompleteHTMLTags_CSharp
{
public partial class Simple_Form : Form
{
public Simple_Form()
{
InitializeComponent();
}
public static String EnteredString = "";
public static Boolean Is_LessThanKeyPressed = false;
public static Boolean Is_GreaterThanKeyPressed = false;
public static Boolean Is_AutoCompleteCharacterPressed = false;
public Boolean Is_SpaceBarKeyPressed = false;
public Boolean Is_TagClosedKeyPressed = false;
public String[] tagslist ={
"html",
"head",
"title",
"body",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"b",
"u",
"i",
"sub",
"sup",
"center",
"strike",
"font",
"p",
"style",
"pre",
"marquee",
"ul",
"ol",
"a",
"img",
"table",
"tr",
"th",
"td",
"frameset",
"iframe",
"form",
"input",
"button",
"textarea",
"select",
"div",
"fieldset",
"span",
"strong",
"em",
"big",
"small"
};
public void ProcessAutoCompleteBrackets(String s)
{
int sel = richTextBox1.SelectionStart;
switch (s)
{
case "(":
richTextBox1.Text = richTextBox1.Text.Insert(sel, ")");
richTextBox1.SelectionStart = sel;
Is_AutoCompleteCharacterPressed = true;
break;
case "[":
richTextBox1.Text = richTextBox1.Text.Insert(sel, "]");
richTextBox1.SelectionStart = sel;
Is_AutoCompleteCharacterPressed = true;
break;
case "\"":
Is_AutoCompleteCharacterPressed = true;
richTextBox1.Text = richTextBox1.Text.Insert(sel, "\"");
richTextBox1.SelectionStart = sel;
break;
case "'":
richTextBox1.Text = richTextBox1.Text.Insert(sel, "'");
richTextBox1.SelectionStart = sel;
Is_AutoCompleteCharacterPressed = true;
break;
}
}
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
String ch = e.KeyChar.ToString();
this.ProcessAutoCompleteBrackets(ch);
if (ch == "<")
{
Is_LessThanKeyPressed = true;
Is_SpaceBarKeyPressed = false;
EnteredString = "";
}
else if (ch == ">")
{
if (!Is_TagClosedKeyPressed)
{
Is_GreaterThanKeyPressed = true;
Is_SpaceBarKeyPressed = false;
int oldsel = richTextBox1.SelectionStart;
for (int i = 0; i < tagslist.Length; i++)
{
if (EnteredString == tagslist[i])
{
richTextBox1.Text = richTextBox1.Text.Insert(oldsel, "</" + tagslist[i] + ">");
richTextBox1.SelectionStart = richTextBox1.SelectionStart + oldsel;
EnteredString = "";
}
}
Is_LessThanKeyPressed = false;
}
else
{
Is_TagClosedKeyPressed = false;
}
}
else
{
if (Is_LessThanKeyPressed)
{
for (char a = 'a'; a <= 'z'; a++)
{
if (a.ToString() == ch)
{
EnteredString += ch;
}
else if (a.ToString().ToUpper() == ch)
{
EnteredString += ch;
}
}
for (int a = 0; a <= 9; a++)
{
if (a.ToString() == ch)
{
EnteredString += ch;
}
}
}
}
// if user itself closes the tag
if (Is_LessThanKeyPressed)
{
if (ch == "/")
{
Is_TagClosedKeyPressed = true;
Is_SpaceBarKeyPressed = true;
EnteredString = "";
}
}
}
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Space:
Is_SpaceBarKeyPressed = true;
if (Is_GreaterThanKeyPressed)
{
Is_GreaterThanKeyPressed = false;
}
Is_LessThanKeyPressed = false;
for (int i = 0; i < tagslist.Length; i++)
{
if(EnteredString==tagslist[i])
{
EnteredString = tagslist[i];
}
}
break;
case Keys.Up:
if (Is_AutoCompleteCharacterPressed == false)
{
EnteredString = "";
Is_AutoCompleteCharacterPressed = false;
}
Is_SpaceBarKeyPressed = false;
break;
case Keys.Down:
if (Is_AutoCompleteCharacterPressed == false)
{
EnteredString = "";
Is_AutoCompleteCharacterPressed = false;
}
Is_SpaceBarKeyPressed = false;
break;
case Keys.Left:
if (Is_AutoCompleteCharacterPressed == false)
{
EnteredString = "";
Is_AutoCompleteCharacterPressed = false;
}
Is_SpaceBarKeyPressed = false;
break;
case Keys.Right:
if (Is_AutoCompleteCharacterPressed == false)
{
EnteredString = "";
Is_AutoCompleteCharacterPressed = false;
}
Is_SpaceBarKeyPressed = false;
break;
case Keys.Enter: EnteredString = "";
Is_SpaceBarKeyPressed = false;
break;
case Keys.Back:
int sel = richTextBox1.SelectionStart;
Point pt = richTextBox1.GetPositionFromCharIndex(sel);
char ch = richTextBox1.GetCharFromPosition(pt);
if (EnteredString.Length > 0)
{
if (ch != '>')
{
EnteredString = EnteredString.Remove(EnteredString.Length - 1);
Is_LessThanKeyPressed = true;
}
}
if (ch == '<')
{
EnteredString = "";
}
break;
}
}
}
}
I've a model with a lot of strings named "hours1, hours2, hours3... " which one is used to assign to the index of the foreach.
How can I simplify this code?
if (colNames.IndexOf(item2) == 0)
{
if (model.Hours == null)
{
item.Hours = 0;
}
else
{
item.Hours = (decimal)model.Hours;
}
}
if (colNames.IndexOf(item2) == 1)
{
if (model.Hours1 == null)
{
item.Hours = 0;
}
else
{
item.Hours = (decimal)model.Hours1;
}
}
if (colNames.IndexOf(item2) == 2)
{
if (model.Hours2 == null)
{
item.Hours = 0;
}
else
{
item.Hours = (decimal)model.Hours2;
}
}
This isn't very pretty, but perhaps:
decimal? hours = null;
switch(colNames.IndexOf(item2))
{
case 0: hours = model.Hours; break;
case 1: hours = model.Hours1; break;
case 2: hours = model.Hours2; break;
}
item.Hours = hours ?? 0M;
I have C#/.NET program which uses a COM port and SQL CE Database with a WCF service. Sometimes the program becomes unresponsive (the window freezes) and even in debug mode I cannot the reason.
I can restart the program after restarting my PC or the COM port. The problem isn't with my COM port problem because I have tried it on many PCs and all have the same problem.
How could I resolve this?
private void timer_Tick(object sender, EventArgs e)
{
try
{
labeltimer.Text = DateTime.Now.ToString();
if (timerstarted == true)
{
return;
}
timerstarted = true;
_CARDCODE = 0;
string error = "";
bool hasbassed = false;
if (this._Close == true)
{
this.Close();
}
if (serialPort1.IsOpen)
{
if (i < 8)
{
i++;
}
else
{
i = 0;
}
to[0] = (byte)(128 + i);
try
{
serialPort1.Write(to, 0, 1);
}
catch (System.Exception ex)
{
ListViewItem Lvi = new ListViewItem((++rowcount).ToString());
Lvi.SubItems.Add("");
Lvi.SubItems.Add("");
Lvi.SubItems.Add("Disconnected");
Lvi.SubItems.Add("");
listView.Items.Add(Lvi);
StartPort();
}
if (progressBar.Value >= 1500)
progressBar.Value = 0;
else
progressBar.Value += 10;
if (serialPort1.BytesToRead != 0)
{
byte[] data = new byte[serialPort1.BytesToRead];
from = new byte[3];
try
{
serialPort1.Read(data, 0, data.Length);
}
catch (System.Exception ex)
{
ListViewItem LVi = new ListViewItem((++rowcount).ToString());
LVi.SubItems.Add("");
LVi.SubItems.Add("");
LVi.SubItems.Add("Disconnected");
LVi.SubItems.Add("");
listView.Items.Add(LVi);
StartPort();
}
// data.Length == 4 by Button or HassPassed
if (data.Length == 4)
{
if (data[1] > 2)
DBS.AddButtonPass(data[0], data[1] / 4);
else
{
DBS.EditPass((int)data[0]);
timerstarted = false;
return;
}
}
else if ((data[data.Length - 1] != 255) || (data.Length < 4))
{
from[0] = data[0];
from[1] = 0;
from[2] = 0;
serialPort1.Write(from, 0, 3);
timerstarted = false;
return;
}
//RegistoringSubscriber_ID Add Subscriber Card
else if ((RegistoringSubscriber_ID > 0) && (RegisterCheckpoint != 0) && (data[0] == RegisterCheckpoint))
{
Registoring = false;
if (RegistorNewCard(Convert.ToInt32(data[2].ToString() + data[3].ToString() + data[4].ToString())) > 0)
FormBringPC.AnswerType = 1;
else
FormBringPC.AnswerType = 2;
RegistoringSubscriber_ID = 0;
error = "register card";
}
// RegistoringSubscriber_ID < 0 Add Bonus Card
else if ((RegistoringSubscriber_ID < 0) && (RegisterCheckpoint != 0) && (data[0] == RegisterCheckpoint))
{
Registoring = false;
if (RegistorBonusCard(Convert.ToInt32(data[2].ToString() + data[3].ToString() + data[4].ToString())) > 0)
FormBringPC.AnswerType = 1;
else
FormBringPC.AnswerType = 2;
RegistoringSubscriber_ID = 0;
error = "register Bonus card";
}
else if ((List_Checkpoint_ID.Contains(data[0]) && ((data[1] == 130) || (data[1] == 129))))
{
// DBS.EditPass((int)data[0]);
}
else if ((List_Checkpoint_ID.Contains(data[0]) && (data.Length == 7)))
{
_CARDCODE = Convert.ToInt32(data[2].ToString() + data[3].ToString() + data[4].ToString());
GlobalTypes.InvalidPass pass = DBS.CheckPassStatus(_CARDCODE, (int)data[0], (int)data[1]);
if (pass == GlobalTypes.InvalidPass.Valid)
{
from[0] = data[0];
from[1] = 1;
from[2] = 1;
serialPort1.Write(from, 0, 3);
LBTitle.Text = DateTime.Now.ToLongTimeString();
hasbassed = true;
}
else
{
from[0] = data[0];
from[1] = 0;
from[2] = 0;
serialPort1.Write(from, 0, 3);
switch (pass)
{
case GlobalTypes.InvalidPass.InvalidCard:
{
error = "Չգրանցված քարտ";
break;
}
case GlobalTypes.InvalidPass.InvalidCount:
{
error = "Այց. քանակ";
break;
}
case GlobalTypes.InvalidPass.InvalidGraphic:
{
error = "Ժամանակ hh:mm:ss";
break;
}
case GlobalTypes.InvalidPass.InvalidPeriod:
{
error = "Ժամանակահատված xxxx.dd.yyy";
break;
}
case GlobalTypes.InvalidPass.InvalidStatus:
{
error = "Հառացված աբոնենտ";
break;
}
case GlobalTypes.InvalidPass.InvalidSuscribe:
{
error = "Անհայտ բաժանորդ";
break;
}
default:
{
error = "esim inch";
break;
}
}
}
}
else if ((List_Checkpoint_ID.Contains(data[0]) && (data.Length > 0)))
{
try
{
byte[] ticket = new byte[data.Length - 6];
for (int ii = 0; ii < data.Length - 6; ii++)
{
ticket[ii] = data[ii + 2];
}
_CARDCODE = Convert.ToInt32(Encoding.ASCII.GetString(ticket.ToArray()));
GlobalTypes.HasPass pass = DBS.CheckTicketPassStatus(_CARDCODE, (int)data[0], (int)data[1]);
if (pass == GlobalTypes.HasPass.TruePass)
{
from[0] = data[0];
from[1] = 1;
from[2] = 1;
serialPort1.Write(from, 0, 3);
LBTitle.Text = DateTime.Now.ToLongTimeString();
hasbassed = true;
}
else
{
from[0] = data[0];
from[1] = 0;
from[2] = 0;
serialPort1.Write(from, 0, 3);
switch (pass)
{
case GlobalTypes.HasPass.CardNotFound:
{
error = "Not registored ticket";
break;
}
case GlobalTypes.HasPass.CardRepeat:
{
error = "ReEntring";
break;
}
case GlobalTypes.HasPass.ValidationPeriodError:
{
error = "Ժամանակ hh:mm:ss";
break;
}
case GlobalTypes.HasPass.SeasonError:
{
error = "Ժամանակահատված xxxx.dd.yyy";
break;
}
case GlobalTypes.HasPass.WeekOfDayError:
{
error = "Day of Weak";
break;
}
default:
{
error = "esim inch";
break;
}
}
}
}
catch { timerstarted = false; return; }
}
if (rowcount >= 10)
{
rowcount = 0;
listView.Items.Clear();
}
ListViewItem lvi = new ListViewItem((++rowcount).ToString());
lvi.SubItems.Add(data[0].ToString());
if (data.Length == 4)
hasbassed = true;
if (((data[1] == 2) || (data[1] == 8)) && ((data.Length == 4) || (Dictionary_Chekpoint[data[0]].CheckPointType == 2)))
{
lvi.SubItems.Add("Ելք");
}
else
{
lvi.SubItems.Add("Մուտք");
}
if (data.Length != 4)
lvi.SubItems.Add(_CARDCODE.ToString());
else
lvi.SubItems.Add("Հերթապահ");
lvi.SubItems.Add(error);
if (hasbassed == true)
{
lvi.BackColor = Color.LightGreen;
}
else
{
lvi.BackColor = Color.LightPink;
}
listView.Items.Add(lvi);
}
}
else MessageBox.Show("Serial port is closed!", "RS232 tester", MessageBoxButtons.OK, MessageBoxIcon.Error);
timerstarted = false;
}
catch
{
timerstarted = false;
return;
}
}
C# programs show as NOT RESPONDING whenever there is processing happening in the main UI thread that is preventing UI processing. The general answer here is to push that work into a different thread, so that UI events can continue to process (see references below for more information). In the case of serial ports, the easiest thing to do is hook the SerialPort.DataReceived event instead of polling it: https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived%28v=vs.110%29.aspx
Ref:
GUI not responding while fetching data
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/dd744765%28v=vs.85%29.aspx
http://www.dreamincode.net/forums/topic/246911-c%23-multi-threading-in-a-gui-environment/
i check whether admin have licence or not so i check monthwise to login now i am using system date for check but admin change system date so i want date BIOS date
string crt_val = "",crt_mont = "",crt_year = "";
dt_v = objdata.select_upload();
if (dt_v.Rows.Count > 0)
{
crt_val = dt_v.Rows[0]["val"].ToString();
crt_mont = dt_v.Rows[0]["mon"].ToString();
crt_year = dt_v.Rows[0]["yer"].ToString();
//yy = Convert.ToInt32(crt_year);
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Window", "alert('Please Update your Licence Key');", true);
return;
}
if (Verification.Trim() != "")
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Window", "alert('Please Update your Licence Key');", true);
return;
}
string fin="";
string oyear = "";
oyear = DateTime.Now.Year.ToString();
int nyear = DateTime.Now.Year + 1;
int smonth = Convert.ToInt32(DateTime.Now.ToString("MM"));
fin = oyear + "-" + nyear.ToString();
int num;
switch (crt_mont)
{
case ("January"):
num = 1;
break;
case ("February"):
num = 2;
break;
case ("March"):
num = 3;
break;
case ("April"):
num = 4;
break;
case ("May"):
num = 5;
break;
case ("June"):
num = 6;
break;
case ("July"):
num = 7;
break;
case ("August"):
num = 8;
break;
case ("September"):
num = 9;
break;
case ("October"):
num = 10;
break;
case ("November"):
num = 11;
break;
default:
num = 12;
break;
}
if ((num < smonth) || (num == smonth))
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Window", "alert('Please Update your Licence Key');", true);
}
else
{
if (crt_val == "0")
{
Session["UserId"] = txtusername.Text.Trim();
objuser.UserCode = txtusername.Text.Trim();
objuser.Password = s_hex_md5(txtpassword.Text.Trim());
string pwd = s_hex_md5(txtpassword.Text);
objuser.Ccode = ddlCode.SelectedValue;
objuser.Lcode = ddlLocation.SelectedValue;
DataTable dt = new DataTable();
dt = objdata.UserLogin(objuser);
if (dt.Rows.Count > 0)
{
if ((dt.Rows[0]["UserCode"].ToString().Trim() == txtusername.Text.Trim()) && (dt.Rows[0]["Password"].ToString().Trim() == pwd) && (dt.Rows[0]["CompCode"].ToString().Trim() == ddlCode.SelectedValue) && (dt.Rows[0]["LocationCode"].ToString().Trim() == ddlLocation.SelectedValue))
{
Session["Isadmin"] = dt.Rows[0]["IsAdmin"].ToString().Trim();
Session["Usernmdisplay"] = txtusername.Text.Trim();
Session["Ccode"] = dt.Rows[0]["CompCode"].ToString().Trim();
Session["Lcode"] = dt.Rows[0]["LocationCode"].ToString().Trim();
if (Session["Isadmin"].ToString() == "1")
{
Session["RoleCode"] = "1";
}
else
{
Session["RoleCode"] = "2";
}
Response.Redirect("Dashboard.aspx");
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Window", "alert('User Name and Password Wrong');", true);
}
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Window", "alert('Please Update your Licence Key');", true);
}
}
else
{ }
}
Getting BIOS date won't change anything, because if someone changes the system date, the hardware real-time clock gets changed too.
Better option is to check date outside of client computer, so for example check some URL.
I have a javascript function called MakeInput.
It has got approximately 150 lines.
And I have to call that function about 300 times in my code.
My C# code called that function through webbrowser control.
It has to call that function 300 times, until DataTable in C# finishes looping.
But maybe javascript in webbrowser thinks that I am calling an infinite recursive function, so when it comes to about 60 times, it always throws an error called stack overflow at line 38.
How can I prevent this error?
How can I do a long time function in javascript?
EDIT
OK.
I am posting my code.
Here it goes.
C#
ieBrowser.Document.InvokeScript("setPageIndex", new object[] { currentPage });
ieBrowser.Document.InvokeScript("fnSearch");
JavaScript
function fnSearch() {
var frm = document.searchFrm;
var str = encryptData(frm, _currentPage);
}
function encryptData(form, _currentPage) {
var retValue = "";
try {
var crypto = document.getElementById("SomeActiveX");
var resultVal = MakeInput(form);
var inputStr = "";
if (resultVal[0] != "" && resultVal[1] != "") {
inputStr = resultVal[0] + "&" + resultVal[1];
}
else if (resultVal[0] == "") {
inputStr = resultVal[1];
}
else if (resultVal[1] == "") {
inputStr = resultVal[0];
}
else {
inputStr = "";
}
retValue = crypto.Encrypt(inputStr);
}
catch (e) {
alert(e);
}
return retValue;
}
function MakeInput(form) {
var result = new Array(2);
try {
var keyProc = document.getElementById("SomeActiveX");
var name = new Array(form.elements.length);
var value = new Array(form.elements.length);
var enc_name = new Array();
var enc_value = new Array();
var enc_idx = 0;
var j = 0;
var len = form.elements.length;
for (i = 0; i < len; i++) {
if ((form.elements[i].type != "button") && (form.elements[i].type != "reset") &&
(form.elements[i].type != "submit")) {
if ((form.elements[i].type == "radio") || (form.elements[i].type == "checkbox")) {
if (form.elements[i].checked == true) {
name[j] = form.elements[i].name;
value[j] = form.elements[i].value;
j++;
}
}
else if ((form.elements[i].type == "text") || (form.elements[i].type == "password")) {
name[j] = form.elements[i].name;
value[j] = form.elements[i].value;
j++;
if (keyProc != null && (form.elements[i].getAttribute("enc") == "on")) {
enc_name[enc_idx] = "_E2E_" + form.elements[i].name;
enc_value[enc_idx] = keyProc.GetEncData("", form.name, form.elements[i].name);
enc_idx++;
}
}
else {
if (form.elements[i].type == "select-one") {
var ind = form.elements[i].selectedIndex;
var singleLen = form.elements[i].length;
if (singleLen > 0) {
if (ind >= 0) {
name[j] = form.elements[i].name;
value[j] = form.elements[i].options[ind].value;
j++;
}
else {
}
}
else {
}
}
else if (form.elements[i].type == "select-multiple") {
var multiLen = form.elements[i].length;
for (k = 0; k < multiLen; k++) {
if (form.elements[i].options[k].selected) {
name[j] = form.elements[i].name;
value[j] = form.elements[i].options[k].value;
j++;
}
}
}
else {
name[j] = form.elements[i].name;
value[j] = form.elements[i].value;
j++;
}
}
}
}
var flag1 = false;
var flag2 = false;
var signed_text = "";
var unsigned_text = "";
for (i = 0; i < j; i++) {
if (name[i].charAt(0) != "_") {
if (flag1) {
signed_text += "&";
}
else {
flag1 = true;
}
signed_text += name[i];
signed_text += "=";
signed_text += escape_url(value[i]);
}
else {
if (flag2) {
unsigned_text += "&";
}
else {
flag2 = true;
}
unsigned_text += name[i];
unsigned_text += "=";
unsigned_text += escape_url(value[i]);
}
}
for (i = 0; i < enc_idx; i++) {
if (flag2) {
unsigned_text += "&";
}
else {
flag2 = true;
}
unsigned_text += enc_name[i];
unsigned_text += "=";
unsigned_text += enc_value[i];
}
if (enc_idx > 0) {
if (flag2) {
unsigned_text += "&";
}
else {
flag2 = true;
}
unsigned_text += "_ENCSEED";
unsigned_text += "=";
unsigned_text += keyProc.GetEncData(serverCert, "", "");
}
result[0] = signed_text;
result[1] = unsigned_text;
}
catch (e) {
throw e;
}
return result;
}
And the error message was captured.
Sorry, the error message is written in Korean.
But it says, "Stack overflow(at line 38)".
But I think the number is not pointing at the line number, coz I've changed lines but the error message is always 38.
It doesn't think you are in a continuous loop. It fails when memory runs out in stack. The same code might work on a computer with higher memory. Each time a function is called a stack entry is kept in stack to move to calee once the function is done, so in your case the stack space runs out after 60 calls. The line 38 is not line number in your code.
Why dont you try same code on a high end PC