每次按下按钮后,在彼此下面动态创建文本框 [英] Dynamically created textboxes underneath eachother after each button click

查看:65
本文介绍了每次按下按钮后,在彼此下面动态创建文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试写一个课程,在每次点击后在form1上创建一个全新的文本框。



新创建的文本框需要直接出现在下一个文本框下面。



目前你可以看到非常简单。



I'm trying to write a class at the moment that creates a brand new textbox on form1 after each click.

the newly created textbox needs to appear directly under the next textbox.

It's very simple at the moment as you can see.

Public Class Textbox_Create

    Shared Property tb As Control

    Public Shared Sub addtextbox()
        Dim Counttotal As Integer = 1
        Dim count As Integer = 1
        Dim name As String
        Dim tb As New TextBox()
        name = ("TxtBox" & CStr(Counttotal))
        tb.Name = name
        tb.Height = 10
        tb.Width = 5
        Counttotal = count + Counttotal
        Form1.Controls.Add(tb)
    End Sub
End Class





第一个按钮点击工作正常,它会创建第一个文本框。好的。



但是点击第二个按钮(显然)它被放置在原始文本框的顶部,而不是在下面。



它命名文本框,因为我希望能够引用前一个文本框的位置(我猜你会引用原始文本框名称,然后指向它的x,y位置)



我是否以正确的方式解决这个问题?



The first button click works fine, it creates the first textbox. All good.

But on second button click (Obviously) It is being placed right over the top of the original textbox, not underneath.

It names the textbox as I want to be able to refer to the previous textbox's location (I'm guessing you would refer to the original textbox name and then point to it's x,y location)

Am I going about this the right way?

推荐答案

你可以尝试这个。对不起,我误解了你的问题..



you could try this one. Sorry, I misunderstood your question..

Dim tb(100) As TextBox
Dim Counttotal As Integer
Dim txtname As String
Dim Xloc As Integer
Dim Yloc, ctr As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Counttotal += 1
        tb(Counttotal) = New TextBox
        txtname = ("TxtBox" & CStr(Counttotal))
        With tb(Counttotal)
            .Location = New Point(Xloc, Yloc + ctr)
            .Text = txtname
            .Height = 10
            .Width = 50
        End With
        Me.Controls.Add(tb(Counttotal))
        ctr += 20
End Sub


只需拖动要显示文本框的FlowLayoutPanel。



请尝试以下代码。



Just drag a FlowLayoutPanel where the text box to be displayed.

Try the following code.

Private ClickCounter As Integer = 0
Private Overloads Sub CreateTextBox(ByVal container As Control)
    'Create new textbox instance
    Dim m_NewTextBox As New TextBox
    'Set Properties
    m_NewTextBox.Name = String.Format("TextBox{0}", ClickCounter)
    m_NewTextBox.Text = String.Format("TextBox{0}", ClickCounter)
    'Add to container(flowlayout pannel container)
    container.Controls.Add(m_NewTextBox)
    'Increment ClickCounter
    ClickCounter = ClickCounter + 1
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    FlowLayoutPanel1.AutoScroll = True
    CreateTextBox(Me.FlowLayoutPanel1)
End Sub


如果不是在运行时,您可以右键单击文本框并单击发送回。



但是如果在运行时,我相信有这样的代码:

if not at runtime, you can just right click on the textbox and click send to back.

but if at runtime, I believe there's this kind of code:
textbox1.sendToBack()





我没试过那个,但每当我按下'。'后,我就看到了控制器的名字。 :D



也许你可以试试。我认为我不会受伤..



I haven't tried that one, but I've seen it whenever I press '.' after the control's name. :D

maybe you can try. I won't hurt I think..


这篇关于每次按下按钮后,在彼此下面动态创建文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆