I"m new in VB and i have a problem with Label.
Example:
public Form1()
{
Label.Text = "s";
InitializeComponent();
}
And when i run program , I"m getting:
System.NullReferenceException textBox1 was null.
How can I fix it ?
Same on TextBox.
Related
first of all thanks for taking your time, now
the problem i have is this;
I have different textblocks in my main XAML file, im trying to get their text to use them in a sqlquery from another file(.cs file)
public void FillTable()
{
MainWindow l = new MainWindow();
string sql = "insert into Pacientes (nombre) values ('"+ l.nombre_text.Text +"')";
var command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
l.Close();
}
However, when i check the Table the result is null, when i check the table the
"nombre" column is blank
See sql image
any clue what im doing wrong?
You can't do a "new MainWindow". You need to get a reference to your MainWindow or somehow pass in the text to FillTable().
Without seeing more of your code, an exact solution is not probable, but something along these lines might get you unblocked.
...
// in MainWindow.cs
FillTable(this);
...
public void FillTable(MainWindow window)
{
string sql = "insert into Pacientes (nombre) values ('"+ window.nombre_text.Text +"')";
var command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}
OR
...
FillTable(nombre_text.Text);
...
public void FillTable(string nombre)
{
string sql = "insert into Pacientes (nombre) values ('"+ nombre +"')";
var command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}
What i want to do is the Following
Create an UserControle like this:
public partial class MyUserControle : UserControl
with a Wrappanel inside to add Controls and stuff to
<WrapPanel Margin="0,0,0,41" Name="wrapPanel1" />
this class has 2 Buttons to Cancel or Apply the Settings in the Window.
The window later gets open like this
Window w = new Window();
w.SizeToContent = System.Windows.SizeToContent.WidthAndHeight;
w.Content = myClass.getmyUserControle();
bool? t = w.ShowDialog();
Where myClass is a special Class that creates the UserControle and adds some stuff it wants to be changed in the WrapPanel
Now in myClass say i have a simple Point
System.Drawing.Point myPoint = new Point(10,15);
and getmyUserControle() looks like
public MyUserControle getmyUserControle(){
MyUserControle UC = new MyUserControle();
WPF.TextBox X1 = new WPF.TextBox();
System.Windows.Data.Binding BindingX = new System.Windows.Data.Binding("X");
BindingX.Source = myPoint;
BindingX.Mode = System.Windows.Data.BindingMode.TwoWay;
X1.SetBinding(WPF.TextBox.TextProperty, BindingX);
UC.addhere.Add(X1);
return UC;
}
The Textbox is filled properly but the changes dont change the source. How can i Fix this?
edit:
I can add a
private MyUserControle myUCsave = null;
to the class and set the
myUCsave = UC;
at the end of getmyUserControle() and check at the start
myUCsave != null return myUCsave;
that fixes the "save" of the textbox, but the underlying myPoint doesnt change.
Edit: Maybe there is a more simple Case: i create a simple Form and add this to the MainWindow via constructor
public partial class MainWindow : Window
public MainWindow()
{
InitializeComponent();
TextBox X1 = new TextBox();
TextBox Y1 = new TextBox();
X1.Margin = new Thickness(0, 0, 20, 20);
Y1.Margin = new Thickness(0, 0, 100, 100);
X1.Width = 100;
Y1.Width = 100;
X1.Height = 200;
Y1.Height = 200;
System.Windows.Data.Binding BindingX = new System.Windows.Data.Binding("X");
System.Windows.Data.Binding BindingY = new System.Windows.Data.Binding("X");
BindingX.Mode = System.Windows.Data.BindingMode.TwoWay;
BindingY.Mode = System.Windows.Data.BindingMode.TwoWay;
BindingX.Source = myPoint;
BindingY.Source = myPoint;
X1.SetBinding(TextBox.TextProperty, BindingX);
Y1.SetBinding(TextBox.TextProperty, BindingY);
this.MainGrid.Children.Add(Y1);
this.MainGrid.Children.Add(X1);
}
Point myPoint = new Point(100, 200);
This Should Create Two TextBoxes X1,Y1 that are Linked to the same source (myPoint.X)? But when i change one thing the other textbox doesn't change neither does the source.
I want to have a drop down list with 12 choices.
I found that ComboBox is what I need (if there is a better control kindly tell me).
I dragged and drop a combo box into a panel using VS2012 and then clicked on the left arrow that appears on the combo box. The following wizard shows:
As you can see, I am just able to type the name of the choice but not the value of it.
My question is how to get the value of these choices?
What I have tried
I built an array with the same length as the choices, so when the user selects any choice, I get the position of that choice and get the value from that array.
Is there a better way?
You need to use a datatable and then select the value from that.
eg)
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Description", typeof(string));
dt.Load(reader);
//Setting Values
combobox.ValueMember = "ID";
combobox.DisplayMember = "Description";
combobox.SelectedValue = "ID";
combobox.DataSource = dt;
You can then populate your datatable using:
dt.Rows.Add("1","ComboxDisplay");
Here, the DisplayMember(The dropdown list items) are the Descriptions and the Value is the ID.
You need to include a 'SelectedIndexChanged' Event on your combobox (If using VS then double click the control in Design Mode) to get the new values. Something like:
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
int ID = Combobox.ValueMember;
string Description = ComboBox.DisplayMember.ToString();
}
You can then use the variables in the rest of your code.
You cannot use the wizard to store values and text. To store DisplayText/Value pair the combobox needs to be connected to some data.
public class ComboboxItem
{
public string DisplayText { get; set; }
public int Value { get; set; }
}
There are two properties on the combobox - DisplayMember and ValueMember. We use these to tell the combobox that - show whats in DisplayMember and the actual value is in ValueMember.
private void DataBind()
{
comboBox1.DisplayMember = "DisplayText";
comboBox1.ValueMember = "Value";
ComboboxItem item = new ComboboxItem();
item.DisplayText = "Item1";
item.Value = 1;
comboBox1.Items.Add(item);
}
To get the value -
int selectedValue = (int)comboBox1.SelectedValue;
I have a parameterised constructor in My Application. I want to add controls dynamically to my silverlight Child Control Page. But it gives NullReferenceException.
I can't find out why it returns null.Can any help me with this situation?
public PDFExport(FrameworkElement graphTile1, FrameworkElement graphTile2,FrameworkElement graphTile3)
{
Button btnGraph1 = new Button();
string Name = graphTile1.Name;
btnGraph1.Content = Name;
btnGraph1.Width = Name.Length;
btnGraph1.Height = 25;
btnGraph1.Click += new RoutedEventHandler(btnGraph1_Click);
objStack.Children.Add(btnGraph1);
LayoutRoot.Children.Add(objStack); // Here am getting null Reference Exception
_graphTile1 = graphTile1;
_graphTile2 = graphTile2;
_graphTile3 = graphTile3;
}
Thanks.
I guess objStack is a stackpanel declared in your XAML?
Be aware that the UI component of your xaml are build by the call to InitializeComponent.
Thus objStack will not exist until you call InitializeCOmponent() in your constructor.
Also, you should know that the call to InitializeComponent is asynchronous, so you code should look like something like that:
private readonly FrameworkElement _graphTile1;
private readonly FrameworkElement _graphTile2;
private readonly FrameworkElement _graphTile3;
public PDFExport(FrameworkElement graphTile1, FrameworkElement graphTile2, FrameworkElement graphTile3)
{
_graphTile1 = graphTile1;
_graphTile2 = graphTile2;
_graphTile3 = graphTile3;
}
private void PDFExport_OnLoaded(object sender, RoutedEventArgs e)
{
Button btnGraph1 = new Button();
string Name = _graphTile1.Name;
btnGraph1.Content = Name;
btnGraph1.Width = Name.Length;
btnGraph1.Height = 25;
btnGraph1.Click += new RoutedEventHandler(btnGraph1_Click);
objStack.Children.Add(btnGraph1);
LayoutRoot.Children.Add(objStack);
}
Hope it helps.
As per my research i got that, why it raises an exception: Because there is no
InitializeComponent() in My Constructor and am not calling parent constructor.
That is the reason it raises Exception.
Just Add InitializeComponent() to the code, simple
I have a simple form with a textBox in it which I am trying to make an user friendly Auto-suggest Textbox..
As per the current scenario I am using AutoCompleteStringCollectionclass bywhich I am able to make textbox show suggestions for the word that starts with the particular Text entered in textbox..
But,I want to make my program as like it should show suggestions even when a part of the string coming from database matches the Textbox.Text.
Presently I am able to filter the data coming from DB based on the userInput by using dataView .But still I am not able to show output on my front end..
I have tried all textbox events like 'KeyPress',KeyDown,KeyUp,TextChanged but its not working....
MyCode::
public partial class Form2 : Form
{
AutoCompleteStringCollection autoCompletefromDB = new AutoCompleteStringCollection();
AutoCompleteStringCollection searchResults = new AutoCompleteStringCollection();
MyLinqDataContext dtcontext = new MyLinqDataContext();
// static string searchChar = "";
SqlConnection con = new SqlConnection("Data Source=DATASERVER\\SQL2K8;Initial Catalog=VTMMedicalContent;Persist Security Info=True;User ID=vtm;Password=M3d!c#l");
DataTable dTable = new DataTable();
SqlCommand cmd;
SqlDataAdapter da;
DataView dtView;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select DiagnosisName from [VTMMedicalContent].[dbo].[DiagnosisMaster]";
da = new SqlDataAdapter(cmd);
da.Fill(dTable);
dtView = new DataView(dTable);
}
//And My KeyPress Event Code..
private void txtAutoComplete_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsControl(e.KeyChar))
{
dtView.RowFilter = dtView.Table.Columns[0].ColumnName + " Like '%" + e.KeyChar + "%'";
foreach (DataRowView dtViewRow in dtView)
searchResults.Add(dtViewRow[0].ToString());
txtAutoComplete.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
txtAutoComplete.AutoCompleteSource = AutoCompleteSource.CustomSource;
txtAutoComplete.AutoCompleteCustomSource = searchResults;
}
//MessageBox.Show("The Elements in searchResult are:" + searchResults.Count);
}
I have the tried writing the same codes in KeyDown,KeyUp,TextChanged events but of no use..:(
It only works on Form_Load but that only shows suggestions that matches the starting point of the word..
Create an AutoComplete TextBoxControl
The AutoComplete feature has a couple of quirks that were inherited from its original designed use, the address box of Internet Explorer. This includes emitting the Enter key when you click on an item in the list. Pressing Enter in the address box of IE makes it navigate to the entered URL.
There isn't anything you can do about that, the native interface (IAutoComplete2) has very few options to configure the way it works. It pokes the keystrokes into the text box by faking Windows messages. Which is one way you can tell the difference, the actual key won't be down. Something you can check by pinvoking GetKeyState(), like this:
as an example you can mess with this anyway you like
private void textBox1_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyData == Keys.Enter && GetKeyState(Keys.Enter) < 0) {
Console.WriteLine("Really down");
}
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern short GetKeyState(Keys key);