如果tb为空,则中止移动记录或保存 [英] Abort moving a record or saving if tb is empty

查看:83
本文介绍了如果tb为空,则中止移动记录或保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会尽量保持清晰。



我的表单上有很多文本框(tb),这些文本框由导入的CSV文件填充。在记录可以通过任何方式保存之前(点击保存按钮,移动到另一个记录,添加新记录,关闭程序)我试图确保填充特定的tb字段。



我有一个脏命令(下面的代码)来记录记录上是否有任何变化,如果是这样,提示用户是否要在保存或取消保存之前移动到下一条记录(如果向前或向后) ,添加新记录或关闭程序)。无论用户使用何种方法移动到另一条记录,添加记录或只是尝试关闭程序而不保存,都会调用脏。



随着修改后面的脏代码会检查并提示我文本框是否缺少数据...但是一旦用户单击确定,它就会继续询问我是否要保存记录(即使我取消保存)然后移动对于所请求的记录或添加,无论我在测试期间选择哪一个。



我希望程序停在那里并保持记录,直到用户纠正错误。比运行脏和移动,添加记录或关闭程序,假设没有其他必需的tb为null / empty。



我的导入代码示例如下:

I will try to be as clear as possible.

I have many textboxes (tb) on my form which are populated by an import of a CSV file. Before the record can be saved by any means (hitting save button, moving to another record, adding a new record, closing the program) I am trying to ensure specific tb fields are populated.

I have a "dirty" command (code below) to note if anything changes on the record and if so prompts the user if they want to save or cancel save before moving to the next record (be if forward or backwards, adding a new record or closing the program). The "dirty" is called regardless of what method a user uses to move to another record, add a record or simply tries to close the program without saving.

With the modified "Dirty" code below it does check and prompt me if a textbox is missing data... but once the user clicks ok, it then continues to ask if I want to save the record (even if I cancel the save) then moves to the requested record or adds, whichever I chose during testing.

I want the program to stop there and stay on the record until the user corrects the "error." than runs the dirty and moves, adds a record or closes the program, assuming no other required tb are null/empty.

An example of my import code is as follows:

tbCarrierLoadNumber.Text = MyList.Items.Item(288).ToString





我的导入工作正常。我以为我可以放置以下内容(替换上面的相同导入行)但是在表单加载之前似乎没有给出错误:





My import works fine. I thought I could place the following (replacing the same import line above) but it seems not to work either giving werrors before the form even loads:

If (String.IsNullOrEmpty(tbCarrierLoadNumber.Text)) Then
                MsgBox("Carrier Load Number is Required." & vbNewLine & "Please Correct.", MsgBoxStyle.Exclamation, "OVERSIGHT ERROR")
else
tbCarrierLoadNumber.Text = MyList.Items.Item(288).ToString
end if





如果我理解正确,它会检查方框之前导入,显然msgbox会在导入之前弹出。我需要在导入之后以及脏事件之前或期间发生这种情况并且不知道如何做到这一点。



因为这显然不起作用我去了回到我的原始导入行并修改我的脏代码如下(请在脏代码结束时查看其他信息):



我尝试了什么:





If I understand properly it is checking the box before the import, so obviously the msgbox will pop up before the import. I need this to happen after the import and before or during the "dirty" event and at a loss how to do this.

Since this apparently is not working I went back to my original import line and modified my "dirty" code as follows (please see additional info at end of dirty code):

What I have tried:

Private Sub Dirty()
    If Not LoadForm Then
        Cursor = Cursors.WaitCursor

        If (String.IsNullOrEmpty(dpDispatchDate.Text)) Then
            MsgBox("Dispatch Date cannot by blank." & vbNewLine & "Please Correct.", MsgBoxStyle.Critical, "OVERSIGHT ERROR")
            Return
        End If
        If (String.IsNullOrEmpty(tbCarrierLoadNumber.Text)) Then
            MsgBox("Carrier Load Number is Required." & vbNewLine & "Please Correct.", MsgBoxStyle.Exclamation, "OVERSIGHT ERROR")
            Return
        End If
        If (String.IsNullOrEmpty(tb01Time.Text)) Then
            MsgBox("01 Pickup Date/Time cannot by blank." & vbNewLine & "Please Correct.", MsgBoxStyle.Critical, "OVERSIGHT ERROR")
            Return
        End If
        If (String.IsNullOrEmpty(tb90Time.Text)) Then
            MsgBox("90 Pickup Date/Time cannot by blank." & vbNewLine & "Please Correct.", MsgBoxStyle.Critical, "OVERSIGHT ERROR")
            Return
        End If
        If (String.IsNullOrEmpty(tbShipCode.Text)) Then
            MsgBox("A Shipper Code is Required - Cannot by blank." & vbNewLine & "PPlease Correct.", MsgBoxStyle.Exclamation, "OVERSIGHT ERROR")
            Return
        End If
        If (String.IsNullOrEmpty(tbRcvrCode.Text)) Then
            MsgBox("A Receiver Code is Required - Cannot by blank." & vbNewLine & "Please Correct.", MsgBoxStyle.Exclamation, "OVERSIGHT ERROR")
            Return
        End If
        If (String.IsNullOrEmpty(tbLinehaulToTruck.Text)) Then
            MsgBox("Linehaul to Truck cannot by blank." & vbNewLine & "Please Correct.", MsgBoxStyle.Critical, "OVERSIGHT ERROR")
            Return
        End If
        If (String.IsNullOrEmpty(tbLinehaulToFax.Text)) Then
            MsgBox("Linehaul to Fax cannot by blank." & vbNewLine & "Please Correct.", MsgBoxStyle.Critical, "OVERSIGHT ERROR")
            Return
        End If
        If (String.IsNullOrEmpty(tbNumPU.Text)) Then
            MsgBox("Number of Pickups cannot by blank." & vbNewLine & "Please Enter Number of Pickups.", MsgBoxStyle.Exclamation, "OVERSIGHT ERROR")
            Return
        End If
        If (String.IsNullOrEmpty(tbNumbDrops.Text)) Then
            MsgBox("Number of Drops cannot by blank." & vbNewLine & "Please Enter Number of Drops.", MsgBoxStyle.Exclamation, "OVERSIGHT ERROR")
            Return
        End If

        Validate()
        LoadInfoDataSetBindingSource.EndEdit()
        If LoadInfoDataSet.HasChanges Then
            MeMsgBoxSaveChanges.ShowDialog()
            If MeMsgBoxSaveChanges.Result = "Yes" Then
                btnSaveNav.PerformClick()
            Else
                LoadInfoDataSet.RejectChanges()
            End If
        End If
        Cursor = Cursors.Default
    End If
End Sub





我也尝试用exit sub替换上面的return而不改变行为。



I also tried replacing the above "return" with "exit sub" with no change in behavior.

推荐答案

尝试改变你的逻辑以下内容:



- 从CSV开始加载的按钮 - 这应该加载文件并用第一行填充文本框。



- 每个(我认为)下一个记录的按钮,前一个记录。



- 保存记录和按钮的按钮保存文件(和退出)和另一个取消(和退出)



按下一个或上一个应该只显示ne xt /以前的记录。如果你在加载每一行时突出显示用户的任何问题数据,那将是非常好的。

如果你真的想要,你可以检查是否在Save Record更新基础数据之前完成了任何更新。

保存文件按钮应该在保存之前验证整个数据集 - 尽管用户可能想要保存一半...然后去吃午餐:-)。您将需要考虑如何标记文件已完全完成。



一些细节

- 不要加载文件表单加载。

- 检查文本框时,检查所有文本框并在单个邮件中为用户提供完整的错误列表 - 或者将违规文本框的颜色设置为不同的颜色,或两者都设置。

- 将您的功能分解为更小的例程 - 验证文本框例程可以返回错误控件列表,例如

- 不要按照解决方案1中的建议
Try changing your logic to have the following:

- A button that starts the load from the CSV - this should load the file and populate your textboxes with the first row.

- A button each (I think) for next record, previous record.

- A button for Save Record and a button for Save File (and exit) and another for Cancel (and Exit)

Pressing next or previous should just display the next/previous record. It would be really nice if you highlighted any problem data for the user as you load each row.
If you really want to you can check to see if any updates were done before Save Record updates the underlying data.
Save File button should validate the entire dataset perhaps before saving - although a user might want to save half way through ... and go to lunch :-). You will need to consider how to flag that the file is completely finished with.

Some specifics
- Don't load the file in Form Load.
- When checking the textboxes check all of them and give the user the complete list of errors in a single message - or set the colour of the offending textboxes to a different colour, or both.
- Break down your functionality into smaller routines - the validate textboxes routine could return a list of faulty controls for example
- Don't follow the advice in Solution 1


如果我的问题清楚,我不能100%确定。但对我来说,似乎你需要在保存数据之前传递许多验证。有了它,看看你的上一个代码 Dirty 我认为你需要的是你需要使用
Well I'm not 100% sure if I get your problem clear. But to me it seems as if you have many verification that need to be passed before the data is saved. With that and by the look at your last code Dirty What I think you need is that you need to use the
If...Else...End If

语句并在每个陷阱错误之后或之下尝试查看错误是否已解决,如果没有,则中断该过程以使其不会继续执行其他代码最终会执行插入/更新语句,这会给你带来问题。



你错过了 Else 在你的代码中。



用简单的英语打破这个。



1.使用否则

2.在每个错误下方进行验证,检查用户是否更正了错误。如果不是,你必须决定在取消整个过程之前保持循环运行多长时间,或暂时将其保存在其他地方作为未完成的任务

3.如果你遇到了错误,请不要只显示错误消息,显示错误后尝试处理错误或将用户指向错误发生的位置。



另外我认为你可以解决的另一个问题是你只是在每个错误被捕后返回,你可以做的是为每个错误设置返回False,这样你就会知道出现了错误并且你将知道不保存那些数据。您最后需要检查返回的是真还是假,但请记住,您可以创建自己的变量,然后首先检查,因为返回将被下一次验证覆盖,但只需使用此

statement and after or below each trapped error try to see if the error has been resolved or not if not then break the process so that it won't continue executing other codes which will in the end execute the insert/update statement which will give you problems.

You are missing the Else within your code.

To break this in simple English.

1. Make use of Else
2. Below each error make a verification that will check if the user has corrected the error. If not you will have to decide how long you will keep your loop running before cancelling the whole process or temporally saving it elsewhere as unfinished task
3. If you have trapped an error don't just display the error message, after displaying the error try to work on the error or point the user to where the error occurred.

Also another solution I think you can us is since you just return after each error trapped, what you can do is for each error set your return to False so that you will know that there has been an error and you will know not to save that data. You will need at the end to check the return if its True or False, but keep in mind that you may create your own variables that you will first check because Return will be overwriten by the next verification but by simply using this

If (Your statement) Then
....
Else
....
End If

会帮助你。


这篇关于如果tb为空,则中止移动记录或保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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