Asp. NET interactive bitmap form design (8)

Source: Internet
Author: User
Tags http request
Asp.net| Interaction | Design button Click event handler
Next is the button click event handler for each button. Most simply add a new, drawn object to the drawing list, and then call the Invalidate on PictureBox to redraw with the updated drawing list. The typical button event handler code looks like this:


C#
private void Addpoint_click (object sender, System.EventArgs e) {
Drawinglist.add (New Dpoint (Getrandompoint (), Color.Blue));
Drawing.invalidate ();
}

Visual Basic

. NET Private Sub Addpoint_click (ByVal sender as System.Object, _
ByVal e as System.EventArgs) Handles Addpoint.click
Drawinglist.add (New dpoint (Getrandompoint (), Color.Blue))
Drawing.invalidate ()
End Sub


The change fills to hot pink (the fill color to the pink) button is different-it gets an array of all the objects that can be filled in the list, and then changes their brush colors to pink. This part of the code appears at the end of the "return to the list of available" section earlier. (In addition, you must also invalidate the PictureBox.) )

Finally, the Erase all (delete all) button simply creates a new drawing list and points our drawinglist field to the list. This frees up the old drawing list for the final memory collection. Then make the PictureBox invalid and remove yourself.

PictureBox Paint Event handlers
The last thing we want to pay attention to is to draw an image in the PictureBox. To do this, you need to handle the Paint event generated by PictureBox, and then use the Graphics object passed through this event to draw on it. To draw, just call the Drawlist method of the drawing list--a For Each loop and polymorphic will handle the rest of the work!


C#
private void Drawing_paint (object sender,
System.Windows.Forms.PaintEventArgs e) {
Drawinglist.drawlist (E.graphics);
}


Visual Basic

. NET Private Sub drawing_paint (ByVal sender as Object, _
ByVal e as System.Windows.Forms.PaintEventArgs) _
Handles Drawing.paint
Drawinglist.drawlist (E.graphics)
End Sub


The end of our tour of Windows forms applications-Please consider the code and make changes so you can learn more!

Using a drawing object in an ASP.net application
Although there are some differences between a asp.net Web application and a Windows forms application, the similarity between the two is still surprising to Dr GUI.

The main part of a WEB forms application
The main part of the ASP.net Web forms application corresponds very much to the parts of the Windows forms application.

Page
This corresponds to the main window in a Windows forms application. A page is a container for all buttons and other controls.

Button
Similarly, there is a set of buttons that you can use to perform various actions on a form. Note that unlike previous applications, we set the pagelayout property of the page document to GridLayout instead of FlowLayout. This means that we can locate each button (and other controls) by pixel position.

Note that there are also some text boxes here.

Also note that you cannot copy and paste Windows forms controls to the Web--you must re-create the entire page.

Image control
The image control corresponds to the PictureBox in a Windows forms application. But there are some important differences: the image control does not generate the Paint message, but rather contains the URL that loads the image.

We set this URL to a second Web page, imagegen.aspx. In other words, we have a Web page that is all about generating bits from our drawing list and then sending the images to the client's Web browser.

We'll discuss the code below.

The primary routines for WEB forms applications
There are some important differences between the code for Windows forms applications and Web forms applications-but there are some interesting similarities. Also note that all of the code in the drawing object file can be used in any of the three applications.

Our page is derived from System.Web.UI.Page and contains a set of declarations for all controls, in addition to the following:

Exactly the same content: data declarations and Getrandompoint
This code is almost identical to the code in a Visual Basic. NET Windows Forms Application. If you want, you can look at this code again. There is only one difference between them, which is to declare the field without initializing it. They will be initialized in the Page_Load method (as shown later).

The Getrandompoint method is exactly the same as other applications. It's good to be able to reuse code, isn't it?

Very similar Content: button click event handler
The button click event handler is the same as a Windows forms application, with one exception: In a Web form, the image control does not have to be invalidated because it is redrawn each time the button is clicked. In addition, it will be redrawn automatically--so the only thing to invoke is the Add method of the drawing list.

The following is a typical button event handler--for drawing a point.

Private Sub Addpoint_click (ByVal sender as System.Object, _ ByVal E as System.EventArgs) Handles Addpoint.click Drawinglis T.add (New dpoint (Getrandompoint (), Color.Blue)) End Sub

Other button event handlers are similar to Windows forms, except, of course, with one exception, which is to not invoke any of the methods to invalidate the image.

Very different content: page loading and unloading handlers
The page load and unload handler methods are completely different.

Keep in mind that our page objects are recreated with each HTTP request. Because each request will create a new page, we cannot store the data as a member variable as in Windows Forms, and the main window will be present with the application in Windows Forms.

So we have to store the required information between the request and the page in some state variable. Here are a few options-this will be discussed below.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.