Create a control array and event processing in the C # WinForm program,
The control array is an excellent design solution provided by VB. It can easily and quickly process the response and time processing of a large number of similar controls, however, it is a pity that this excellent feature in C # has not been inherited. This article will discuss how to implement it in the C # WinForm program.
First, check the interface.
In the above interface, I want to implement a 42-element Button array to implement a Calendar function. Some of the buttons are shown above. These buttons are placed in a grid layout.
After you manually create all buttons, define the member variable control array in the Form class code: private Button [] btns;
Then initialize In the constructor: btns = new Button [42];
Btns [0] = this. button1; btns [1] = this. button2 ;...... btns [40] = this. button41; btns [41] = this. button42;
We can't help wondering if dynamic creation is better. in fact, the main reason why I use manual creation is that it is convenient to manually place and adjust the button's position and size. If they are dynamically created, it is troublesome to place them on the interface, especially in complicated la S.
Then add the event for the controls in the preceding array: for (int I = 0; I <btns. length; I ++) {btns [I]. click + = new System. eventHandler (this. btns_Click);} if you want to add different events according to the subscript, just make some branches. this method of C # is similar to AddActionListener in wing. We can use C # To implement MVC.
Finally, define the btns_Click event processing function: // The event processing private void btns_Click (object sender, System. eventArgs e) {String [] arrs = (Button) sender ). text. split ('\ n ');
String day = arrs [0];
If (day. Length <2) {day = "0" + day ;}
String date = currMonthLbl. Text + "-" + day;
MainForm. RefreshDatePanel (date );}
From the above, we can see that the control corresponding to the event can be obtained by forcibly converting the sender with the Button, and then the uniqueness can be determined based on its Text attribute, and then it can be processed separately.
To sum up, it is easy to create a control array in C #. First, create an array of the control type in the class, and then initialize it, you can choose whether to dynamically create an initialization task or link to an existing control based on the actual situation, add an event to the array element, and finally implement the event, in event implementation, you can get the corresponding control by converting the sender.
In C language <yes?
First, move left. To move left is to move all the bits of a number to the left. In C, the <operator is used. For example:
Int I = 1;
I = I <2; // shifts the value in I two places to the left.
That is to say, the binary system of 1 is 000... 0001 (here, the number of 0 in front of 1 is related to the number of digits in int, 32-bit machine, 31 0 in gcc), shifted to 000 after 2 digits left... 0100, that is, 4 in decimal system. Therefore, if the Left shift of 1 is equivalent to multiplying by 2, then the left shift of n is multiplied by the Npower of 2 (the number of symbols is not fully applicable, this is because the left shift may cause symbol changes. The reasons are described below)
One problem that needs to be noted is the symbol bit at the leftmost end of the int type and the shift out. we know that int Is the signed integer number, and the first bit on the leftmost side is the signed bit, that is, 0 is positive and 1 is negative. Then, overflow occurs during shift. For example:
Int I = 0x40000000; // 40000000 of hexadecimal notation, 01000000 of hexadecimal notation... 0000
I = I <1;
Then, after I shifts 1 to the left, it will become 0x80000000, that is, 100000 of the binary system... 0000, the sign bit is set to 1, and the other bits are all 0, which is changed to the minimum value that can be expressed by the int type. The 32-bit int value is-2147483648, overflow. what will happen if I is moved 1 to the left? In C language, the highest bit is discarded. After 1 is discarded, the value of I is changed to 0.
A special case in the left shift is that when the number of digits in the left shift exceeds the maximum number of digits in the value type, the compiler will use the number of digits in the left shift to de-model the maximum number of digits and then shift by the remainder, for example:
Int I = 1, j = 0x80000000; // set int to 32 characters
I = I <33; // 33% 32 = 1 shifted to 1, and I changed to 2
J = j <33; // 33% 32 = 1 shifts 1 bit left, j changes to 0, and the highest bit is discarded.
When compiling this program with gcc, the compiler will provide a warning, indicating the number of places to move left> = type length. in fact, I, j moves the remainder of 1 bit, that is, 33% after 32. this rule is implemented in gcc. It is unclear whether other compilers are the same.
In short, the Left shift is: discard the highest bit, 0 fill the second bit
Now that the right shift is clear, the right shift is better understood.
The concept of right shifting is opposite to that of left shifting. It is to move several digits to the right. The operator is>.
The bitwise of the right-shift symbol is different from that of the left-shift symbol. For signed integers, for example, int type, the right-shift symbol remains unchanged. For example:
Int I = 0x80000000;
I = I> 1; // The I value will not change to 0x40000000, but 0xc0000000
That is to say, after the sign bit moves to the right, if it is positive, it fills in 0, and if it is negative, it fills in 1, that is, the arithmetic shift in assembly language to the right. similarly, when the number of digits to be moved exceeds the length of the type, the remainder is obtained and then the remainder is moved.
Negative number 10100110> 5 (assuming the word length is 8 characters), the result is 11111101.
In short, in C, the Left shift is the logical/arithmetic left shift (the two are identical), and the right shift is the arithmetic right shift, which will keep the symbol bit unchanged. in practical applications, you can use the left/right shift to perform fast multiplication/Division operations, which is much more efficient than loop operations.
For example, in C language, the Left shift <represents multiplied by 2, right shift> represents dividing by 2, which is caused by how the computer works! But if it is 7, the binary number is 0111, And the right shift is 3.5, but after the right shift, the binary number is 0011, which is 3. Different. How can I explain it ??
A: The two operands of the shift operator must be integer. The Value Type of the entire shift expression is also integer, and the rest of the full text>
& In C Language
& Can be used as the bitwise AND or address fetch Operator
The following describes two usage methods:
1. bitwise and operation bitwise AND operator "&" are binary operators. Its function is the binary phase corresponding to the two numbers involved in the operation. The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0. The number of involved operations is supplemented.
For example, 9 & 5 can be written as follows: 00001001 (Binary complement of 9) & 00000101 (Binary complement of 5) 00000001 (Binary complement of 1) Visible 9 & 5 = 1.
Bitwise AND operations are usually used to clear some bits or retain some bits. For example, if a clears the high eight bits of 0 and retains the low eight bits, it can be used as a & 255 operation (255 of the binary number is 0000000011111111 ).
2. Get the address
& As The unary operator, the result is the address of the right operation object.
For example, & x returns the address of x.
The address itself is an abstract concept used to indicate the logical location of an object in the memory.