|
Ummm... the answer is rather obvious. Upgrade your version of Visual Studio.
|
|
|
|
|
|
Depending on the user input I create a certain number of textboxes using a loop. For example if user inputs "10" then 10 textboxes are created. TxTBox1, TxTBox2 etc etc.
How then do I create identical event handlers for these textboxes for example to validate input for them. They will all be exactly the same.
|
|
|
|
|
Try:
void AllTheseTextBoxes_TextChanged(object sender, EventArgs e)
{
TextBox tb = sender as TextBox;
if (tb != null)
{
string input = tb.Text;
...
}
}
private void MyButton_Click(object sender, EventArgs ew)
{
for (int i = 0; i < 10; i++)
{
TextBox tb = new TextBox();
tb.Text = "Textbox " + i;
tb.TextChanged += AllTheseTextBoxes_TextChanged;
...
}
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
OriginalGriff has shown you how to make all run-time created Controls use the same EventHandler. Let me add:
1. when you create a set of Controls at run-time: you probably should keep track of them ... keep a reference to the instances for future use: I got tired of writing Control specific code for this, and created a generic method:
public Dictionary<string, T> CreateControls<T>(Control container, int ncontrols, Func<T, int, string> nameGenerator, Func<T, int, Point> locGenerator)
where T : Control
{
Dictionary<string, T> dict = new Dictionary<string, T>();
string name;
for (int i = 0; i < ncontrols; i++)
{
T newT = Activator.CreateInstance<T>();
container.Controls.Add(newT);
name = nameGenerator(newT, i);
newT.Name = name;
dict.Add(name, newT);
if (locGenerator != null)
{
newT.Location = locGenerator(newT, i);
}
}
return dict;
} Sample usage:
int hstart, vstart, hoff, voff;
hstart = 100;
vstart = 100;
hoff = 0;
voff = 60;
var tbxDict = CreateControls<TextBox>(this, 5, (tbx, i) => $"newTbx_{i}", (tbx, i) => new Point(hstart + (i * hoff), vstart + (i * voff)));
var check = tbxDict["newTbx_0"].Name; When this is executed, five new TextBoxes are added to the Form this code runs in, a Dictionary is created and returned for easy future access to the created TextBoxes, and the new TextBoxes are placed on the Form in a column starting at 100,100. offset from each other vertically by 60 points.
The two functions (Delegates) that create the name and position of each new TextBox are passed as parameters to the 'CreateControls method.
Now, validation: that's the next chapter
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
VB6 is long dead, but it had one function I really found useful.
It was called ProperCase() , or something similar.
What it did was apply upper casing to the first letter of each word and make the rest of the word lower case. It was great for names.
Does C# have anything that performs that function?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
|
|
Thanks Eddy!
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
You're welcome
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
HI,
I am trying to figure out how to convert my nested object in to not nested JSON...
I am writing this in .net MVC
This is my object (its a list)
emp { Country: "USA",
Grade: "2B",
Data: {
Name:abc,
Value:155
},
{
Name:abcd,
Value:200
},
{
Name:abcd,
Value:300
}
},
I would like to be able to convert it to JSON, while Changing the "Data" as new object where name is a name of object and value is its value....so something like this
emp { Country: "USA",
Grade: "2B",
abc: 155,
abcd: 200,
abcde: 300
},
I am still learning, and not sure how to go about it, didn't work much with json before. Would appreciate information if its possible and some tips how to solve it
|
|
|
|
|
You haven't shown the code you're using to generate the JSON.
At a guess, you'll need to make your emp property a Dictionary<string, object> :
emp = new Dictionary<string, object>
{
["Country"] = "USA",
["Grade"] = "2B",
["abc"] = 155,
["abcd"] = 200,
["abcde"] = 300,
},
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
So, I would actively discourage this approach. If your domain model looks like that, there is no reason the JSON representation of your domain model shouldn't as well. Such a structure will also result in downstream problems when you make changes at the source level.
If you absolutely need to do this, though, you can implement a new JsonConverter for your object. Have a look at:
Custom JsonConverter
Custom JsonConverter<T>
"Never attribute to malice that which can be explained by stupidity."
- Hanlon's Razor
|
|
|
|
|
protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
this.BindGrid();
}
protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
int Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
string site = (row.FindControl("txtSite") as TextBox).Text;
string fake = (row.FindControl("txtFake") as TextBox).Text;
string query = "UPDATE Sites SET Sites=@Site, Fake=@Fake WHERE id=@id";
string constr = "Data Source=LAPTOP-01LEC0RG;Persist Security Info=False;Integrated Security=SSPI;Initial Catalog=BlackList;User Id=sa;Password=blacklist;Connect Timeout=30;";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@id", Id);
cmd.Parameters.AddWithValue("@Site", site);
cmd.Parameters.AddWithValue("@fake", fake);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
This is my code. my update option does not function when i click edit.
|
|
|
|
|
What does happen? Have you traced the code with the debugger, and why do you not check the return value from the ExecuteNonQuery ?
|
|
|
|
|
Don't connect to your database as sa . That is an unrestricted account which could be used to destroy your server. Instead, use a specific account which has only the permissions required by your application.
Also, avoid hard-coding the connection string. Store it in a configuration file:
Connection Strings and Configuration Files | Microsoft Docs[^]
And if that's your real sa password that you've just posted to a public forum, then you should change it ASAP!
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hello.
Sorry in advance for the long post.
We currently have a c# console application that pulls medical claims from our client via a web service. Our application doesn't handle multi-threading at the moment. Unfortunately, we've recently been told by the client that we're going to start receiving millions of claims all at once (as opposed to the typical few thousand per day) & after doing the math, we've determined it would take weeks for this application to process that many claims, so we thought introducing multi-threading into the application would help. However, this raised a question of a potential issue: if it's multi-threaded, how do we ensure no claims get lost in the mix and never pulled?
Our current process is as follows:
1) We pull an initial list of all claim id's from the client. This is a one-time pull and we store the list.
2) We then read off the next claim from that list & call their web service for the details of that claim. We do this one claim at a time, until we've finished pulling the details of every claim from that list.
3) Before pulling each claim, we store the value of the claim id we're trying to process into a "Last Claim ID Processed" field in our db table, in case there's ever a system crash and we need to pick up later with the claim id where we left off with. This way, our application just basically says "ok, I see there was a failure last time we ran. Lets start off with claim id#12345 rather than starting from the very beginning of the list"
4) Finally, we store that claim info in our database and move onto the next claim.
The bulk of our time is at step #2, where we process each claim, so this is the piece we want to split into multi-threading. Where we can split this process into about 10 threads and each one can asynchronously read the next claim id from the claim list that we pulled in step #1. Theoretically, that should make our application process claims 10 times faster than it currently is. But my problem is how would each thread know which other claims are already in the midst of being processed?
So again, my concern is, how do we ensure no claims get lost in the mix? For example, I'm concerned about the following scenario:
1) Thread #1 reads from the claim list & pulls the next claim. Claim id 10001. Before processing, it stores the value of this claim id into our "Last Claim ID Processed" field in our import summary table, to let us know it's the last claim processed in case of a crash (step #3 above)
2) Thread #2 reads from the list & pulls claim id 10002 & repeats the same process
3) Thread #3 reads from the list & pulls claim id 10003 & repeats the same process
4) Thread #1 finishes processing claim id 10001 & starts pulling claim id 10004. And again, stores that claim id value into the import summary table.
5) Thread #3 crashes and never finishes pulling claim id 10003...
In the above scenario, we have a big problem! Claim 10003 crashes but we'll never know because thread #1 has already overwritten the "Last Claim ID processed" field with 10004.
If you've stuck with me this long, I thank you (again, sorry for the long post). But I guess I'm just looking for a little advice on the best way to handle this new multi-threading process we want to integrate. I need to be VERY careful that no claims end up lost in the mix.
Thanks in advance for any advice you can provide!
|
|
|
|
|
Hi,
When things get handled out of order, as in multi-threading, you can't handle all possible mishaps with a single "Last Claim ID Processed". What you need is a list/table of claims where each claim has a "status" field, so you can have any number of threads looking for and dealing with unhandled claims. And you will want at least three possible statuses: new, started, and done. (Obviously done would not be needed if a claim would be removed when done processing).
modified 15-Nov-18 17:19pm.
|
|
|
|
|
Yesterday I asked this same question. Today I want to follow up on it
First, some clarification... I'm working on a Xamarin Forms app. The app will only be able to run INSIDE our VPN.
Could I just check "System.Environment.UserDomainName" to see if returns the right domain name?
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
|
Tried that.. I VPN in on my phone, run the Android app in VS2017, the app starts on my phone, and Environment.UserDomainName always returns "localhost".
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
That's pretty much what the Remarks section in the MSDN page says will happen. When I run it on my PC it returns my PC name.
|
|
|
|
|
What I'm trying to accomplish is to determine if the app is being run on our network. The users will VPN in, then run the app. I need to check that their connected before continuing to let app load.
I've also tried "Dns.GetHostAddresses("www.the_domain.com").FirstOrDefault()". This returns a 192.168.... address.
Any idea how to check if the user is VPN's in from an Android app?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
As far as I am aware, this will always return the address related to the system on which the app is running. It's a while since I did this for real.
|
|
|
|
|
|