|
Your code seems to suggest that you are maintaining the state of your app in a variable (clickCount) which is declared inside the button Click handler, which means it won't survive the handling of a click, hence it will always restart having the value of 1.
You should move the declaration outside the method, i.e. make it a class member. That way, for each instance of the Form (assuming you might create more than one of them), the button would remember its state.
You do not need the static keyword, unless you want to have all instances of that Form to share their state (which seems unlikely to me).
IMO it is easier and robuster to put the state inside the button itself; there are several ways to achieve this;
- one is to create your own MyButton class, inheriting from Button, and taking care of its peculiarities (IMO it would be overkill here);
- another is to hide the state in the Tag property, which is a field provided but unused by Windows for all WinForms Controls, so it is available for whatever purpose you decide to use it for. Mind you, it is of type object, so you would need some casting. (I prefer this approach).
- and then you could derive the state from existing variables and/or properties; in your example, you could perhaps just check the txtDisplay.Text and when it ends on "Side1" just replace "Side1" by "Side2". (I typically don't do it this way, as it is less robust; e.g. when you change languages, there is more code that needs changing).
modified 23-Jun-12 10:12am.
|
|
|
|
|
Hi Luc, thanks for replying. I have searched for a way to "put the state inside the button itself" like you have suggested but all I've found were maintaining session state for web apps and not windows form apps. I'm also interested in your suggestion to "hide the state in the Tag property" but I can't find an example of how to do that either. Please give me links to articles on how to do those things I've just mention if you can. Thanks again for your help.
modified 24-Jun-12 5:55am.
|
|
|
|
|
What's so hard about it??
SomeButton.Tag = stateValue;
|
|
|
|
|
(Another garbage sentence was needed to get this post accepted amidst bugs and cache issues, sorry for that)
And the harder half of the job is getting the state back, which requires a cast as I said:
int myState=(int)myControl.Tag;
And no, I don't have a reference about this subject.
Inheriting from a class, or creating a more specialized one, is a fundamental concept in Object-Oriented Programming. There are zillions of articles on the subject, why not try and read Custom Button - Issues with Focus Border, Text Color, etc.[^] (I didn't, it was the first I found using CP search).
|
|
|
|
|
Sorry for the delay. If you want to keep number of clicks inside the button, all you have to do is subclass Button class and add a field with the number of clicks. Then, override the OnClick method to increase the number of clicks by one every time the button is clicked. Don't forget to call the parent OnClick so that the normal button click behavior is executed.
You have an example below.
using System;
using System.Drawing;
using System.Windows.Forms;
class CountClickButton : Button {
int clickCount;
public CountClickButton() {
clickCount = 0;
}
protected override void OnClick(EventArgs e) {
clickCount = clickCount + 1;
base.OnClick(e);
}
public int ClickCount {
get {
return clickCount;
}
}
}
class CountClickForm : Form {
CountClickButton b;
public CountClickForm() {
b = new CountClickButton();
b.Text = "Click me!";
b.Size = b.PreferredSize;
ClientSize = new Size(700, 400);
b.Left = (ClientSize.Width - b.Width) / 2;
b.Top = (ClientSize.Height - b.Height) / 2;
b.Click += new EventHandler(b_clicked);
Controls.Add(b);
Text = "I count clicks!";
}
void b_clicked(object sender, EventArgs e) {
string text = String.Format("Button has been clicked {0} times.", b.ClickCount);
if (b.ClickCount < 1) {
text = "Sorry, I was drunk when programming this. Cheers!!";
}
if (b.ClickCount == 1) {
text = "Button has been clicked once.";
}
if (b.ClickCount == 2) {
text = "Button has been clicked twice.";
}
Text = text;
}
[STAThreadAttribute]
static void Main() {
Application.Run(new CountClickForm());
}
}
|
|
|
|
|
usually in vb.net i do like this,
Dim id As Guid = DirectCast(DirectCast(Me.ABCBindingSource.Current, DataRowView)("IdABC"), Guid)
but in C# is like this,
Guid id = ((DataRowView)this.ABCBindingSource.Current)["IdABC"];
tried a lot of code to convert it to Guid and all fail.
even this way didn't works.
Guid id = Convert.ChangeType(((DataRowView)this.categoryBindingSource.Current)["IdCategory"],Guid);
Edit :
omg randomly write and i got like this,
Guid id = (Guid)((DataRowView)this.categoryBindingSource.Current)["IdCategory"];
real hard to move from vb.net to c#..
modified 23-Jun-12 2:08am.
|
|
|
|
|
Does that mean you found the solution - because the (Guid) option is the one I would have gone for first...
If you solve it yourself, please edit the question and add "[Solved]" to the subject line - that way it's obvious!
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
okay, thank you..
|
|
|
|
|
You're welcome!
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
I have a Listbox that show items from a database. I have a button that I want to use to show details of an item selected. Right now I click the button and a new window opens with all the records in a DataGridview, but it defaults to the top of the list. I want it to go directly to that record when I click the button instead of having to scroll to find it. Could someone point me in the direction I need to go to find the method I need to use. Not asking for a ride, just directions.
My favorite bits make quarters.
|
|
|
|
|
|
Sorry, the Q&A where I was, was not easily discerned from the C# forum. I reposted the second one because the first one I felt did not have enough info in it. Navigation in some places here can be kind of confusing. It was easier when I first signed up here.
My favorite bits make quarters.
|
|
|
|
|
Hi All,
I`m working on server client application and I would like to send a byte array with a message and bytes.
For example:
in the byteArray i`ll have place for string "Name" , "Message"(will be "Image") , and then the bytes of the image.
When the server will get the bytes I will he the ability to know who send it(His name) and what did he sent in this case image.
Then I will take the last part of the byte[] and will convert the bytes to Image and display it on client winform.
Any one can help here ?
Thanks,
Doron
|
|
|
|
|
If you know the exact length of each text field, this is pretty easy to do.
using System.Text;
string name = Encoding.ASCII.GetString(byteArray, 0, nameFieldLength)
string message = Encoding.ASCII.GetString(byteArray, nameFieldLength, messageFieldLength)
Then getting the image itself depends on whether you're working in WinForms or WPF, but either one lets you load a bitmap image from a byte array.
If you're going to use variable-length fields, then you'll need some kind of delimiter to tell you which bytes to grab, and it gets slightly more complex.
|
|
|
|
|
I recommend the XmlSerializer[^] class.
Create a class to hold your string and byte array such as:
public class StringAndByteArray
{
public string Name = string.Empty;
public byte[] ByteArray = null;
}
Then serialize the class to a network stream to send it across.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Since I am new to linq, I am asking this question. I would like to know how to setup a linq statement for a C# 2010 application that communicates with a sql server2008 r2 database. I have the following two questions:
1. I would like to know how to select 6 specific columns from one database table. They key to the table is based on an identity seed that is increment by one.
2. i would like to know how to pick the largest (maximum) and smallest (minimum) value for a create date in one table called main.
|
|
|
|
|
These (and other common) Lina examples can be found here[^]. I found this page to be quick problem solver when I first saw Linq.
/ravi
|
|
|
|
|
In a C# 2010 application I would like to be able to use linq instead of sql to update from to 1 to 6 specific columns in a table that is stored in sql server 2008 r2 database. I am asking that question since I know linq is the preferred method over using sql.
I do know what the key to the table is.
Thus can use tell me how to acdcomplish this goal?
|
|
|
|
|
sc steinhayse wrote: linq is the preferred method over using sql.
Only if you're no good at writing SQL -- otherwise writing SQL will always be superior.
sc steinhayse wrote: tell me how to acdcomplish this goal
I have no idea; I don't use Linq.
|
|
|
|
|
If you use Linq to SQL query, you can use:
var myQuery =
from persons in myDataContext.LastPersons
where persons.LastName.StartsWith("Co")
select persons;
foreach (clsPerson currentPerson in myQuery)
{
currentPerson.LastUpdate = DateTime.Now;
}
try
{
myDataContext.SubmitChanges();
MessageBox.Show("Operation successfully");
}
catch (Exeption ex)
{
MessageBox.Show(ex.Message);
}
search DataContext reference
|
|
|
|
|
|
Usually the key column will be updated automatically, To use Linq or other tool, you should have primary and foreign key properly defined for the Table.
Linq will be the simplest and easy way, you will be updating as you update the collection like how you update collection in C#, it will updates DB Automatically
|
|
|
|
|
Hi,
i have 3 tables in my sql database:
The tables are:
1.Items table
fields=Id,Barcodes,ItemDescription,QtyAvailable,UserI
D,BranchID,CostPrice,SalesPrice
=====================================================
2.Sales table
fields=Id,SalesDate,QtySales,ItemID,CustomerID
=====================================================
3.customer table
fields=ID,Fname,Lname,Address,Phone#
=====================================================
how do i link all these tables together in asp.net
webform such that with 1 combo box, i can select
customer name and every details i want in the form
and the second combo box will select Item ID and all
corresponding info of the product(like Quantity
Available, Sales price, etc) and 2 blank columns for
recording quantity sold and date sold?
I have created customer form using LINQ to sql, now i
want intergrate sales/items tables into it such that
when i select a particular customer, i should be able
to select items(2 or more items) he/she buys and
manually enter quantity sold while the price is
inserted automatically from item table and current
date is inserted in the form automatically. Please
how do i achieve this?
|
|
|
|
|
Johnmc071 wrote: asp.net
You might want to ask in the ASP.NET forum.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
|
|
|
|
|
Hi all,
As said in the message subject, I'm looking for a rules-based expert system proven enough to be integrated in a commercial product. I have used JBoss Drools (Java) in the past and at first I have thought to use Dools.NET, but there [^] I found a message from Mark Proctor assuring that the project has been abandoned a few years ago, and the truth is that it scares me a little.
Consequently, an option is to connect JBoss Drools (Java) with the C# project somehow, but previously I prefer to ask if someone have faced such situation. ¿Is there any other proven expert system engine available over C#?
Thanks in advance.
modified 22-Jun-12 7:44am.
|
|
|
|