RightToLeft RichTextBox with pre-loaded text - vb.net

Ok i found a strange type of bug in MS default Richtextbox in vb.net 2008. If we add some line of text in Richtextbox programmaticlly. there is a gape from right side. see the image below
here is my code
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f As New Form
Dim rtb As New RichTextBox
f.Width = 500
f.Height = 500
rtb.RightToLeft = Windows.Forms.RightToLeft.Yes
For i = 1 To 20
rtb.AppendText("بسم اللہ الرحمن الرحیم" & vbNewLine)
Next
rtb.Dock = DockStyle.Fill
f.Controls.Add(rtb)
f.Show()
End Sub

I can't explain it, but try changing the order of your code so that the RichTextBox control is added to the form before you append the text. This worked for me:
Private Sub btn1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn1.Click
Dim f As New Form
f.Width = 500
f.Height = 500
Dim rtb As New RichTextBox
rtb.Name = "rtb"
rtb.Dock = DockStyle.Fill
rtb.RightToLeft = RightToLeft.Yes
f.Controls.Add(rtb)
For i = 1 To 25
rtb.AppendText("بسم اللہ الرحمن الرحیم" & vbNewLine)
Next
f.Show()
f.BeginInvoke(New Action(Of RichTextBox)(AddressOf RunFix), rtb)
End Sub
Sub RunFix(ByVal rtfControl As RichTextBox)
rtfControl.Select(0, 0)
rtfControl.ScrollToCaret()
End Sub
I added a hack BeginInvoke method that performs a ScrollToCaret() call which seems to fix the problem.

If you also set
rtb.Width = 500
rtb.Height = 500
Then it works as desired.
I agree it is strange behaviour. It does the same with VS 2012 RC.

Related

vb.net How to overwrite the text in a combobox after the dropdownclosed event

I created this piece of code to illustrate the idea, it uses a combobox and a textbox
Private Sub ComboBox2_DropDown(sender As Object, e As EventArgs) Handles ComboBox2.DropDown
ComboBox2.Items.Clear()
ComboBox2.Items.Add("0001 | Apple")
ComboBox2.Items.Add("0002 | Pear")
ComboBox2.Items.Add("0003 | Banana")
ComboBox2.Items.Add("0004 | Pineapple")
ComboBox2.Items.Add("0005 | Cherry")
End Sub
Private Sub ComboBox2_DropDownClosed(sender As Object, e As EventArgs) Handles ComboBox2.DropDownClosed
Dim selecteditem As String = ComboBox2.Items(ComboBox2.SelectedIndex)
ComboBox2.Text = Strings.Left(selecteditem,4)
TextBox2.Text = Strings.Left(selecteditem,4)
End Sub
When I select an item from the combobox what happens is that the combobox keeps showing the whole string while the textbox only shows the first 4 characters.
How can I overwrite the combobox text after I close the combobox?
* edit *
I tried a combo of the solutions but ran into a problem because the data was bound to a datasource so it's not possible to change the item.
This is the new code:
Private Sub ComboBox2_DropDown(sender As Object, e As EventArgs) Handles ComboBox2.DropDown
SQL.ExecQuery($"select ID, Name, RTRIM(ID + ' | ' + Name) as SingleColumn from GCCTEST.dbo.tblFruit")
ComboBox2.DataSource = SQL.DBDT
ComboBox2.DisplayMember = "SingleColumn"
End Sub
Private Sub ComboBox2_DropDownClosed(sender As Object, e As EventArgs) Handles ComboBox2.DropDownClosed
ComboBox2.DisplayMember = "ID"
ComboBox2.SelectedIndex = 0
End Sub
Now I only need to have the 0 be the index I chose...
The following should work.
If not necessary, don't populate the combobox on every drop-down, instead call the FillComboBox-method when loading the Form.
Private Sub FillComboBox()
SQL.ExecQuery($"select ID, Name, RTRIM(ID + ' | ' + Name) as SingleColumn from GCCTEST.dbo.tblFruit")
ComboBox2.DataSource = SQL.DBDT
ComboBox2.DisplayMember = "ID"
ComboBox2.ValueMember = "ID"
End Sub
Private Sub ComboBox2_DropDown(sender As Object, e As EventArgs) Handles ComboBox2.DropDown
Me.ComboBox2.DisplayMember = "SingleColumn"
End Sub
Private Sub ComboBox2_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox2.SelectionChangeCommitted
Dim selV As Object = Me.ComboBox2.SelectedValue
Me.TextBox2.Text = CStr(selV)
Me.ComboBox2.DisplayMember = "ID"
'Set the current value again, otherwise the combobox will always display the first item
Me.ComboBox2.SelectedValue = selV
End Sub
I used a few properties and .net String.SubString method instead of the old vb6 Strings.Left.
Private Sub ComboBox1_DropDownClosed(sender As Object, e As EventArgs) Handles ComboBox1.DropDownClosed
Dim SelectedString As String = ComboBox1.SelectedItem.ToString
Dim ChangedString As String = SelectedString.Substring(0, 4)
Dim index As Integer = ComboBox1.SelectedIndex
ComboBox1.Items(index) = ChangedString
End Sub
You can fill your combo box one by one to avoid binding problems as follows...
Private Sub ComboBox1_DropDown(sender As Object, e As EventArgs) Handles ComboBox1.DropDown
Using cn As New SqlConnection("Your connection string")
Using cmd As New SqlCommand("Select ID, Name From tblFruit;", cn)
cn.Open()
Using dr As SqlDataReader = cmd.ExecuteReader
ComboBox1.BeginUpdate()
While dr.Read
ComboBox1.Items.Add(dr(0).ToString & " | " & dr(1).ToString)
End While
ComboBox1.EndUpdate()
End Using
End Using
End Using
You can solve this graphical problem putting a Label in your Form and moving it over your ComboBox.
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.Label1.AutoSize = False
Me.Label1.BackColor = Me.ComboBox1.BackColor
Me.Label1.Location = New Point(Me.ComboBox1.Location.X + 1, Me.ComboBox1.Location.Y + 1)
Me.Label1.Size = New Size(Me.ComboBox1.Width - 18, Me.ComboBox1.Height - 2)
Me.ComboBox1.Items.Add("0001 | Apple")
Me.ComboBox1.Items.Add("0002 | Pear")
Me.ComboBox1.Items.Add("0003 | Banana")
Me.ComboBox1.Items.Add("0004 | Pineapple")
Me.ComboBox1.Items.Add("0005 | Cherry")
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Me.Label1.Text = Trim(Me.ComboBox1.SelectedItem.Split("|")(0))
End Sub
Please note that:
you can populate your ComboBox a single time during Form load event
you can use SelectedIndexChanged instead of DropDown and DropDownClosed event
if this is not a graphical problem please give a look at DisplayMember and ValueMember properties

How to acces checked status of dynamicaly created Radio Buttons in VB

I create a variable amount of radio buttons in a Tab Page by reading the lines out of an array using:
Public Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim rbgen As RadioButton
Dim tab1 = 0
For y As Integer = 0 To Array.GetUpperBound(0) Step 1
If Array(y, 0) = "ABC" Then
rbgen = New RadioButton
rbgen.Name = "RButton" & Convert.ToString(y)
rbgen.Left = 10
rbgen.Top = ((tab1) * 30)
rbgen.Text = Array(y, 2)
rbgen.Size = New System.Drawing.Size(260, 40)
TabPage1.Controls.Add(rbgen)
tab1 = tab1 + 1
End If
Next
End Sub
When i click a "start" Button i need to run different code depending on the checked RadioButton. But how do i access the information of which radio Button is checked?
Thank you very much for your help!
You could try to add a Handler after you add the radio button:
AddHandler rbgen.CheckedChanged, AddressOf RadioBox_CheckedChanged
and then you need to add this sub
Private Sub RadioBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim RadioBox As RadioButton = TryCast(sender, RadioButton)
If RadioBox IsNot Nothing Then
MessageBox.Show(RadioBox.CheckState)
End If
End Sub
and if you want to check the status
For Each RadioBox In TabPage1.Controls.OfType(Of RadioButton)()
if Ctype(TabPage1.controls("RadioButton" & i), radiobutton).checked = true then
'your Code Here
end if
next
I could not test it yet but I hope I already helped you a little bit :)

How do I get a value from a dynamic control?

I've got a form with a picturecontrol (default to black bg) and I have a flowlayoutpanel underneath. On the form's load it cycles through a folder of images and creates a thumbnail (picturecontrol) inside the flowlayoutpanel. What I want to do is dynamically add a click event to let the user change the main picturecontrol image with one of the thumbnails.
Private Sub TabImageLoad()
Dim apppath As String = Application.StartupPath()
Dim strFileSize As String = ""
Dim di As New IO.DirectoryInfo(apppath + "\images")
Dim aryFi As IO.FileInfo() = di.GetFiles("*.*")
Dim fi As IO.FileInfo
For Each fi In aryFi
If fi.Extension = ".jpg" Or fi.Extension = ".jpeg" Or fi.Extension = ".gif" Or fi.Extension = ".bmp" Then
Dim temp As New PictureBox
temp.Image = Image.FromFile(di.ToString + "\" + fi.ToString)
temp.Width = 100
temp.Height = 75
temp.Name = fi.ToString
temp.Visible = True
temp.SizeMode = PictureBoxSizeMode.StretchImage
AddHandler temp.Click, AddressOf Me.temp_click
FlowLayoutPanel1.Controls.Add(temp)
End If
Next
End Sub
Private Sub temp_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
PictureBox1.Image = temp.Image
End Sub
This is my code for the sub that gets the images (note the addhandler attempt) and the sub that links to the addhandler. As you've probably guessed the addhandler doesn't work because "temp" is not declared in the temp_click sub.
Any suggestions?
The sender argument is always the control that triggered the event, in this case a PictureBox:
Private Sub temp_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim pb As PictureBox = DirectCast(sender, PictureBox)
PictureBox1.Image = pb.Image
End Sub
I suggest you to use:
Private Sub temp_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim pbDynamic as PictureBox = trycast(sender,Picturebox)
Then validate with
if pbDynamic IsNot Nothing Then
PictureBox1.Image = pbDynamic.image
end if
This way you avoid runtime errors and null pointer exceptions

Real Time Updating winforms visual studio 2010 .net

I am having some problems getting the data to reload after its updated to the database i am loading my gridview from form load BindGrid() event I have included my code here.
My Delcarations are as follows I have tried everything here but cant force it to refresh the grid
Dim dbContext As New R3Delivery
Dim threeContext As New skechersDeliveryEntities1
Dim bs As New BindingSource
Private Sub frmConfirmDeliverys_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
BindGrid()
dgDeliverys.Columns(0).ReadOnly = True
dgDeliverys.Columns(1).ReadOnly = True
dgDeliverys.Columns(2).ReadOnly = True
End Sub
Public Sub BindGrid()
Dim cfglocation As Int16
cfglocation = cfb.StoreLocation
bs.DataSource = (From u In threeContext.R3Delivery Where u.isprocessed = True AndAlso u.location = 1
Select u)
bs.ResetBindings(True)
dgDeliverys.DataSource = bs
End Sub
My save button is as follows
Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
threeContext.SaveChanges()
BindGrid()
End Sub
I thought I should show my declaration of my form as well the above code is in my edit form the below is my calling from
Dim frmConfirmDeliverys As New frmConfirmDeliverys
frmConfirmDeliverys.ShowInTaskbar = False
frmConfirmDeliverys.ShowDialog()
have you tried doing this?
dgDeliverys.Datasource=Nothing
dgDeliverys.DataSource = bs
or
dgDeliverys.Refresh
Also like jmcilhinney said, check if the data you are expecting is returned by the query.

How can I reduce this code to a single procedure

I am relatively new to the whole .NET thing, coming from a VB classic background.
On my form I have a tabcontrol, with 4 tabs. Most of the code is handled using a shared handler, but for the others I have to write a handler for each.
How can I optimize these routines into a single procedure?
Private Sub cboCalc0_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCalc0.SelectedIndexChanged
'Page 0
If (Not (IsNothing(trvSignals0.SelectedNode)) And txtSignalName0.Enabled = True) AndAlso trvSignals0.SelectedNode.Level = 3 Then
tempChannelProp(0, trvSignals0.SelectedNode.Tag).CalcVariant = cboCalc0.SelectedIndex
End If
End Sub
Private Sub cboCalc1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCalc1.SelectedIndexChanged
'Page 1
If (Not (IsNothing(trvSignals1.SelectedNode)) And txtSignalName0.Enabled = True) AndAlso trvSignals1.SelectedNode.Level = 3 Then
tempChannelProp(1, trvSignals1.SelectedNode.Tag).CalcVariant = cboCalc1.SelectedIndex
End If
End Sub
Private Sub cboCalc2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCalc2.SelectedIndexChanged
'Page 2
If (Not (IsNothing(trvSignals2.SelectedNode)) And txtSignalName2.Enabled = True) AndAlso trvSignals2.SelectedNode.Level = 3 Then
tempChannelProp(2, trvSignals2.SelectedNode.Tag).CalcVariant = cboCalc2.SelectedIndex
End If
End Sub
Private Sub cboCalc3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCalc3.SelectedIndexChanged
'Page 3
If (Not (IsNothing(trvSignals3.SelectedNode)) And txtSignalName3.Enabled = True) AndAlso trvSignals3.SelectedNode.Level = 3 Then
tempChannelProp(3, trvSignals3.SelectedNode.Tag).CalcVariant = cboCalc3.SelectedIndex
End If
End Sub
I have handled the other bits as follows, and it works great, but I just cannot figure out how to do it with code like that above.
Private Sub trvSignals_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles trvSignals0.AfterCheck, trvSignals1.AfterCheck, trvSignals2.AfterCheck, trvSignals3.AfterCheck
'Handles Page 0,1,2,3
sender.SelectedNode = e.Node
If e.Node.Level = 3 Then
tempChannelProp(sender.tag, e.Node.Tag).Active = e.Node.Checked
End If
End Sub
I use the tag property of the control of each page to hold either 0,1,2 or 3 as appropriate.
Thanks
Graham
Create control arrays that you can reference by index, and put the index in each controls' Tag.
Class MyForm
Private ReadOnly cboCalcArray As ComboBox()
Private ReadOnly trvSignalsArray As TreeView()
Private ReadOnly txtSignalNameArray As TextBox()
Public Sub New()
InitializeComponent()
cboCalcArray = new ComboBox() {cboCalc0, cboCalc1, cboCalc2, cboCalc3}
trvSignalsArray = new TreeView() {trvSignals0, trvSignals1, trvSignals2, trvSignals3}
txtSignalNameArray = new TextBox() {txtSignalName0, txtSignalName1, txtSignalName2, txtSignalName3}
End Sub
Private Sub cboCalcX_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) _
Handles cboCalc0.SelectedIndexChanged,
cboCalc1.SelectedIndexChanged,
cboCalc2.SelectedIndexChanged,
cboCalc3.SelectedIndexChanged
Dim index As Integer = sender.Tag
If (Not (IsNothing(trvSignalsArray(index).SelectedNode)) And txtSignalNameArray(index).Enabled = True) AndAlso trvSignals2.SelectedNode.Level = 3 Then
tempChannelProp(index, trvSignalsArray(index).SelectedNode.Tag).CalcVariant = cboCalcArray(index).SelectedIndex
End If
End Sub
End Class
You can leverage a couple of features:
1) The VB Handles statement can hook multiple control events to the same method.
2) You can get the combobox from the sender, then extract the index.
3) From the index, you can find the other associated controls.
Private Sub cboCalc_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCalc0.SelectedIndexChanged, cboCalc1.SelectedIndexChanged, cboCalc2.SelectedIndexChanged, cboCalc3.SelectedIndexChanged
Dim oCombo As ComboBox
oCombo = DirectCast(sender, ComboBox)
Dim wIndex As Integer
Dim oTree As TreeView
Dim oTextBox As TextBox
wIndex = CInt(oCombo.Name.Substring(oCombo.Name.Length - 1))
oTree = DirectCast(Me.Controls.Find("trvSignals" & wIndex.ToString, True)(0), TreeView)
oTextBox = DirectCast(Me.Controls.Find("txtSignalName" & wIndex.ToString, True)(0), TextBox)
If oTree.SelectedNode IsNot Nothing AndAlso oTextBox.Enabled AndAlso oTree.SelectedNode.Level = 3 Then
tempChannelProp(wIndex, oTree.SelectedNode.Tag).CalcVariant = oCombo.SelectedIndex
End If
End Sub
There is likely a more elegant solution to your problem somewhere. I find that, if I am defining the same controls and methods for multiplpe tabs on a tab control, it might be time to examine my design . . . Not always, though. :-)
This might be a little less clumsy, although using Select Case to choose between otherwise identical configurations of UI elements gives me the heebie-jeebies (It DOES get all your code into one handler, however):
Private Sub PerformCalcs(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles _
cboCalc0.SelectedIndexChanged,
cboCalc1.SelectedIndexChanged,
cboCalc2.SelectedIndexChanged,
cboCalc3.SelectedIndexChanged
Dim cboCalc As ComboBox = DirectCast(sender, ComboBox)
' The Parent of the combo box is the TabPage, and the Parent of the TabPage is the TabControl:
Dim t As TabControl = cboCalc.Parent.Parent
Dim SelectedNode As TreeNode
Dim TxtSignalName As TextBox
' The TabControl knows the index value of the currently selected tab:
Select Case t.SelectedIndex
Case 0 'Page 0
SelectedNode = trvSignals0.SelectedNode
TxtSignalName = txtSignalName0
Case 1 ' Page 1
SelectedNode = trvSignals1.SelectedNode
TxtSignalName = txtSignalName1
Case 2 ' Page 2
SelectedNode = trvSignals2.SelectedNode
TxtSignalName = txtSignalName2
Case 3 ' Page 3
SelectedNode = trvSignals3.SelectedNode
TxtSignalName = txtSignalName3
Case Else ' Ooops! Something horrible happened!
Throw New System.Exception("You have passed an invalid Control as a parameter")
End Select
If (Not (IsNothing(SelectedNode)) And TxtSignalName.Enabled = True) AndAlso trvSignals3.SelectedNode.Level = 3 Then
tempChannelProp(3, SelectedNode.Tag).CalcVariant = cboCalc.SelectedIndex
End If
End Sub