Results 1 to 20 of 20

Thread: Results of a code and output location

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2013
    Posts
    15

    Results of a code and output location

    Hi everyone

    I am beginner in VB.NET. I want to know how can (if I can in VB.NET) to write something in a screen, but in specific location on the screen. For example, how can I write “Hello World!” in let’s say 15 row and 22 column in screen.
    In TB (DOS) where I used to write code that’s been very simple. You just had to write
    Locate 15, 22: Print “Hello World”.
    Is it possible here something like that?

    (In general is it possible the definition of specific location output?)

    Please give an example.


    Thanks a lot

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,619

    Re: Results of a code and output location

    Is this for a console application or for a windows form application?
    If it's the former it's:
    Code:
        Sub Main()
            Write_at_Point(22, 15, "Hello World")
            Console.ReadLine()
        End Sub
    
        Private Sub Write_at_Point(ByVal x As Integer, ByVal y As Integer, ByVal msg As String)
            Console.CursorLeft = x
            Console.CursorTop = y
            Console.WriteLine(msg)
        End Sub
    If it's the latter than you can do something like this:

    Code:
        Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            Dim g As Graphics = e.Graphics
    
            g.DrawString("Hello World", Me.Font, Brushes.Black, New Point(15, 22))
        End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2013
    Posts
    15

    Re: Results of a code and output location

    Thank a lot dday9
    It is windows form application.
    I try the code.
    Have to put something in the Form (TextBox, etc.) and click on it?


    The Private Sub Form!_Paint(………)….Me.Paint will appear or have to write it?
    Th.

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,619

    Re: Results of a code and output location

    With the paint, the only thing that you'd need to do is invoke the paint. To do that would be simple, just add your textbox and call Me.Refresh in it's Text Changed event. Then when you call DrawString, you'd just replace the "Hello World" with the TextBox's text:

    Code:
        Private Sub Form1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
            Dim g As Graphics = e.Graphics
    
            g.DrawString(TextBox1.Text, Me.Font, Brushes.Black, New Point(22, 15))
        End Sub
    
        Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
            Me.Refresh()
        End Sub

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2013
    Posts
    15

    Re: Results of a code and output location

    Thank you dday9. I'll try the code and comeback.

    Th.

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2013
    Posts
    15

    Re: Results of a code and output location

    Sorry dday9 i couldn't make it.

    Can you please describe the process step by step, until to enter the code .

    Thank you and sorry.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,126

    Re: Results of a code and output location

    What did you try, and what did not work? DDay could certainly write it for you, but who would gain from that? Neither him nor you. Most likely, you got pretty close. Show us what you did and we'll help you finish it.
    My usual boring signature: Nothing

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,619

    Re: Results of a code and output location

    I think that I may know the problem, do you know how to generate events for controls?

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 2013
    Posts
    15

    Re: Results of a code and output location

    Hi dday9
    Finally i did it. (To say the truth i didn't understand how)

    I did these steps:

    First open a form and 2 click. Then i write the first part of the code you gave me, but the title was different ....Private sub Form1_Load(By Val....)....Load
    Then add a TextBox and 2 click again to the TExtBox and write the second part. Also here the title was different.
    Then i decide to do something else.

    I open a new Form and i didnt add anything at Form, just copy
    the code you gave me and run.
    The program worked perfect without controls or anything.

    The problem is that i didnt understant a few things like:
    The code inputs the "Hellow World!" like graphics? If it instead "Hellow World!" we had number and we would like to use it as number,
    the code will work or must use a convert function?

    What does the Me.Refresh()?

    The 15,22 represent pixels from the left up corner of the screen or something else?

    Can we run code without adding controls in a Form?

    and some more questions but i dont want to bother you anymore.

    Th.

  10. #10
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,619

    Re: Results of a code and output location

    Trust me you aren't bothering me because you're actually putting forth an effort unlike a lot of people. What happened whenever you double clicked the form is it loaded the form's load event. You can tell it's the load event because at the very end of the first line it should have something along the lines of:
    Code:
    Handles Form1.Load
    Every control(I believe) has a default event to where you can simply double-click the control and it will generate the event. A few I know just off the top of my head are:

    Control - Event
    Form - Load
    TextBox - TextChanged
    BackGroundWorker - DoWork
    Timer - Tick

    So what do you do when you need a different event, like in my example I use the Form's paint event. Well you need to generate the event and there are several ways to do it. The first is to do it in the designer. First single-click on your control, in this example I'm going to use the Form. Next you pull up it's properties, if you don't have the properties window up you can do so by either hitting F4 or by going to View -> Other Windows -> Properties. The properties window should look like this:
    Name:  properties.png
Views: 75
Size:  22.7 KB
    In the properties window there is the events tab. To get to it, click on the yellow/orange lightning bolt. This will bring up every single event for the control, some like a Form have a good amount of events where as some like the timer have only one event. To generate the event, simply double-click on it. So go ahead and double click on the Paint event to generate that.

    The second way to generate a controls event is to do it in the code. First bring up your code view, to do this, just hit F7 or go to view -> code. From there you should see two comboboxes up at the top of the code. One for the controls and one for their events, take a look at the picture:
    Name:  code view.png
Views: 73
Size:  6.6 KB

    Now it's the easy part, just chose the control on the left hand combobox and then chose the event in the right hand combobox.

    That's a quick lesson on generating events.
    The code inputs the "Hellow World!" like graphics? If it instead "Hellow World!" we had number and we would like to use it as number,
    the code will work or must use a convert function?
    I'm not quite following the question, this is how I interpreted it: If instead you wanted to display a number instead of a string? If so, the only conversion that you would need to do is convert the integer/double/decimal/etc. to a string, which is super easy. Simply call .ToString or wrap the number in parenthesis:
    Code:
            Dim g As Graphics = e.Graphics
            Dim int As Integer = 100
            g.DrawString(int.ToString, Me.Font, Brushes.Black, New Point(22, 15))
    
    'or
    
            Dim g As Graphics = e.Graphics
            g.DrawString("100", Me.Font, Brushes.Black, New Point(22, 15))
    What does the Me.Refresh()?
    With Me.Refresh it forces the Form to repaint itself. The reason why we need to call refresh is so that every time somebody types in the textbox it will reflect properly on the Form. If we didn't have Me.Refresh in there the text would appear the next time that the Form is repainted(which could be never).

    The 15,22 represent pixels from the left up corner of the screen or something else?
    0,0 would represent the upper-left hand corner of the form. So 15, 22 would represent 15 pixels to the right and 22 pixels down from 0, 0.

    Can we run code without adding controls in a Form?
    Sure, the only control that you would need is your Form. In fact in the first example I gave you it was done without any controls at all.

    and some more questions but i dont want to bother you anymore.
    Like I said, so long as you're putting forth the effort you are not bothering me at all.

  11. #11

    Thread Starter
    New Member
    Join Date
    Oct 2013
    Posts
    15

    Re: Results of a code and output location

    Thank you very much dday9 for the very instructive answer.

    One more question about numbers. Must I always convert them to strings if I want to output them in the screen? (In old languages that wasn’t necessary).

    Th.

  12. #12
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,619

    Re: Results of a code and output location

    It's not necessary if you keep Option Strict Off I believe, the compiler will implicitly convert the integer or other numerical datatype to a string. However, you want to keep strict typing in your programming. I have an article on Option Strict here, I'd highly advise that you check it out.

  13. #13
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,126

    Re: Results of a code and output location

    The only thing that can be displayed on the screen is a string. Technically, you can leave Option Strict OFF and the compiler will implicitly convert the number to a string, but implicit conversions are slower than explicit conversions and the same amount of work is being done in the end. So, you can make it look like you are not converting it to a string, but somebody IS doing the conversion, whether you do it explicitly or leave it to the compiler to do it implicitly. You doing the conversion explicitly is more efficient and safer, though in this case you won't see any problems either way.
    My usual boring signature: Nothing

  14. #14

    Thread Starter
    New Member
    Join Date
    Oct 2013
    Posts
    15

    Re: Results of a code and output location

    Thank you both.

    I will read the article dday9.
    If (almost) always needed to convert numbers to string for output, that means we must have a textbox for every number?
    For example let’s say that I want to write a program, which make some calculations and extract some results in about 100 different math. variables and I want to type these 100 variables in 100 different places in the screen. Must also have and 100 different TextBoxes, one per variable?

    Th.

  15. #15
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,619

    Re: Results of a code and output location

    You certainly could, but I think that the better option would be to have a DataGridView with that many calculations going on. Have one column for the equation and the other column for the answer. This way you would be able to loop through all the rows and only display: Row X, Column 1 where Row X is the iteration that you're on. Rows and Columns have a 0 based index so Column 1 is actually the second column where Column 0 would be the first column.

  16. #16

    Thread Starter
    New Member
    Join Date
    Oct 2013
    Posts
    15

    Re: Results of a code and output location

    Th. again dday9.

    I will study the DataGridView, but does this function place the numbers everywhere I want?
    For example let’s say that I would like to type the numbers 1 to 100 in the screen but not in align order, just in random places of the screen (a game program).
    Does the DataGridView do that, or there is an easier way?

    Th.

  17. #17
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,619

    Re: Results of a code and output location

    No, the DataGridView wouldn't do that. I suggested the DataGridView because I was thinking one column for equations and what have you then another column for the results. If you're wanting to type out 1 - 100 in random places, I would do something like this:

    Code:
    Option Strict On
    Option Explicit On
    Public Class Form1
        Private pt_to_int As New Dictionary(Of Integer, Point)
        Private r As New Random
    
        Private Sub SetDictionary()
            'Loop from 1 to 100
            For i As Integer = 1 To 100
                'Set up a new random point,
                'But not one that's already taken
    
                Dim pt As Point
                Dim not_found As Boolean = False
                Do Until not_found = True
                    pt = New Point(r.Next(0, Me.Width - 25), r.Next(0, Me.Height - 25))
    
                    If pt_to_int.ContainsValue(pt) = False Then
                        not_found = True
                    End If
    
                Loop
    
                pt_to_int.Add(i, pt)
    
            Next
    
            Me.Refresh()
        End Sub
    
        Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            For Each itm As Integer In pt_to_int.Keys
                e.Graphics.DrawString(itm.ToString, Me.Font, Brushes.Black, pt_to_int.Values(itm))
            Next
        End Sub
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Call SetDictionary()
        End Sub
    End Class

  18. #18

    Thread Starter
    New Member
    Join Date
    Oct 2013
    Posts
    15

    Re: Results of a code and output location

    Th. dday9

    I tried the code but didn’t run.
    I load a new WAF , then 2 clicks on the form and copy the code, but didn’t run. Many mistakes with red underline.
    What i am doing wrong?

    Th.

  19. #19
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,619

    Re: Results of a code and output location

    You have to think, there are two events generated: the form's load and the form's paint events. Other than that there is one custom sub that I wrote(SetDictionary). So rather than double clicking on the form, just hit F7 to bring up the code. Then past the code into the form. But I have to say, this is all pretty basic stuff. I'm worried that you aren't understanding the basics. I would highly advise going here to learn the basics.

  20. #20

    Thread Starter
    New Member
    Join Date
    Oct 2013
    Posts
    15

    Re: Results of a code and output location

    As I said I am beginner, so I believe its natural asking for basics. The logic is: asking –learning-practicing. This moment I am in the 2 first stages. I have written 3-4 small programs and learning. About the homeandlearn.co.uk, already I have print the half files and reading. If you tired understand.
    Th.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width