|
Hi, i have a problem with setting a file path to a public variable.
So, what my code must do :
- open file
- set full path to a file so i can read it in another function
- in other function open ini file for reading
but i cant get a file path to it
Here is a code ;
using System;
using System.Windows.Forms;
using IniParser;
using IniParser.Model;
using System.Collections.Generic;
using System.IO;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public string myFilePath;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
searchBtn.Enabled = false;
writeIniBtn.Enabled = false;
saveBtn.Enabled = false;
}
private void openIniBtn_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.InitialDirectory = "c:\\";
openFile.Filter = "INI files (*.ini)|*.ini";
openFile.Title = "Select a INI file for edit";
if (openFile.ShowDialog() == DialogResult.OK)
{
string myFilePath = openFile.FileName;
searchBtn.Enabled = true;
writeIniBtn.Enabled = true;
saveBtn.Enabled = true;
openIniBtn.Enabled = false;
}
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count != 0)
{
ListViewItem item = listView1.SelectedItems[0];
string output = item.Text.Split('[', ']')[1];
string outputt = item.Text.Split('[', ']')[2];
itemIdTxt.Text = output.ToString();
nameInput.Text = outputt.ToString();
}
}
private void searchBtn_Click(object sender, EventArgs e)
{
MessageBox.Show("my file path : " + myFilePath);
|
|
|
|
|
That's because you've hidden the field with a local variable:
public string myFilePath;
private void openIniBtn_Click(object sender, EventArgs e)
{
...
string myFilePath = openFile.FileName;
...
}
Remove the local variable, and use the field instead:
private void openIniBtn_Click(object sender, EventArgs e)
{
...
myFilePath = openFile.FileName;
...
}
If you want to be absolutely safe, you can even prefix the field name with this. to ensure that it uses the field and not a local variable:
private void openIniBtn_Click(object sender, EventArgs e)
{
...
this.myFilePath = openFile.FileName;
...
}
Scopes | Basic concepts | C# Language Reference | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks man this solved my problem.
|
|
|
|
|
Is there a known API to Google Images[^] so that it is possible to use it automatically with local files?
|
|
|
|
|
Try the Google developers website.
|
|
|
|
|
I hope someone can help me. I develop my first C# WPF Program and I need an input control for IP Address. To my big surprise there isn't a control for this which I think is pretty strange. In C++ I have something like that.
Okay so far I have read I could use a masked textbox from the extended wpf toolkit. But I have 2 problems with that:
First of all when I use a mask like 000.000.000.000 and someone wants to enter an address like 192.168.1.1 I would get 192.168.001.001 But I don't want to see leading zeros
the second problem is that someone could enter an address like 500.300.400.800. How can I handle this?
|
|
|
|
|
|
Thank you for the link but that's what I did the entire day yesterday but I found no page that answered my 2 questions. Either the links weren't available anymore or they just said that you should use masked textboxes. But non of them has explained how to handle my two problems with the masked text boxes
I also found a dll that offered an IP Address control but I couldn't change the font size for example for the control at all so I can't use it either
|
|
|
|
|
Try posting your question in the WPF forum, you may get a better answer.
|
|
|
|
|
Don't you see that an IPv4 address consists of 4 bytes?
Now write a value converter between string and IpAddress which handles that formatting.
That can be based on the following snippet showing both conversions:
IPAddress address = IPAddress.Parse("192.168.1.1");
byte[] bytes = address.GetAddressBytes();
string formatted = $"{bytes[0]:D3}.{bytes[1]:D3}.{bytes[2]:D3}.{bytes[3]:D3}";
|
|
|
|
|
Why not just use a normal TextBox control, combined with IPAddress.TryParse[^] to parse the input?
The problem with the old IP address control is that it's limited to IPv4 addresses. If your application ever needs to deal with IPv6 addresses, that control won't work.
IPv6 - Wikipedia[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Member 13196574 wrote: To my big surprise there isn't a control for this which I think is pretty strange
Really? That's a not-so-commonly used control so I'm not surprised it doesn't exist.
On the other hand, up until about seven years ago, WPF didn't have a data grid. Now THAT was a surprise!
|
|
|
|
|
I have created SQL store procedure.
CREATE PROCEDURE spGetMiti (
@engdate varchar(10)
) with encryption AS
BEGIN
DECLARE @RC int
DECLARE @RomanDate DATETIME
DECLARE @PrnLine nvarchar (4000)
EXEC @RC = [HRDB].[dbo].[s_GetNepaliDate] @engdate, @RomanDate OUTPUT
SELECT @PrnLine = isnull(CONVERT(nvarchar, @RomanDate, 111), '')
SELECT @PrnLine [Tarikh]
END
it displays in correct result after executing store procedure
EXEC spGetMiti '1978/02/03'
Tarikh > 2035/10/21
IN c#, class MtCommom
private string _engdate = string.Empty;
public string Engdate
{
get { return _engdate; }
set {
if (value.Length < 10)
{
throw new Exception("Correct date is required");
} _engdate = value;
}
}
public DataTable GetMiti()
{
SqlParameter[] param = new SqlParameter[]
{
new SqlParameter("@engdate", _engdate)
};
return DAO.GetTable("spGetMiti", null);
}
in Button Press
MtCommon mtcom = new MtCommon();
mtxtDOB.Text = string.Empty;
mtcom.Engdate = mtxtEngDate.Text;
DataTable dt = mtcom.GetMiti();
mtxtDOB.Text = dt.Rows[0]["Tarikh"].ToString();
Error Displays
"Procedure or function 'spGetMiti' expects parameter '@engdate', which was not supplied."
|
|
|
|
|
public DataTable GetMiti()
{
SqlParameter[] param = new SqlParameter[]
{
new SqlParameter("@engdate", _engdate)
};
return DAO.GetTable("spGetMiti", null);
}
In the above code you create an array of SqlParameter values, and add one labelled @engdate . However, when you call the stored procedure, I do not see that the parameter array is being passed to it.
|
|
|
|
|
CREATE PROCEDURE spGetMiti (
@engdate varchar(10)
) with encryption AS
BEGIN
DECLARE @RC int
DECLARE @RomanDate DATETIME
DECLARE @PrnLine nvarchar (4000)
EXEC @RC = [HRDB].[dbo].[s_GetNepaliDate] @engdate, @RomanDate OUTPUT
SELECT @PrnLine = isnull(CONVERT(nvarchar, @RomanDate, 111), '')
SELECT @PrnLine [Tarikh]
END
I think, there is a parameter "@engdate". Isn't it parameter?
|
|
|
|
|
Yes, but you never send the parameter when you call the SP.
|
|
|
|
|
Dear Sir,
Thank you for your response and pointing on parameter. I focused on parameter and
I found out the error in my code
public DataTable GetMiti()
{
SqlParameter[] param = new SqlParameter[]
{
new SqlParameter("@engdate", _engdate)
};
return DAO.GetTable("spGetMiti", null);
}
return DAO.GetTable("spGetMiti", null);
I corrected it
return DAO.GetTable("spGetMiti", param);
|
|
|
|
|
Which is what I explained in my first reply.
|
|
|
|
|
My requirement is straight forward. i have Http://abc need to make it Https://abc . I have added the folowing code in web.config. i.e. added new rule in IIS. I have folowwed the URL rewriting module in IIS.
<rewrite>
<rules>
<rule name="http to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^off$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
</rule>
</rules>
</rewrite>
It's not working .
|
|
|
|
|
And what does that have to do with C#?
Ask here: Ask a Question[^]
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
This is the code form web.config. Just formatted for proper code view. If you know the answer then Helpout.
|
|
|
|
|
And (I ask again) what does that have to do with C#?
This is a dedicated forum: C# only. QA is more general, it takes queries on many, many subjects.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
i want to raise an exception when the user enters an alt keywords like({,Ñ,º,ô) in xml with c# code. the exception has to be raised from loadxml()
|
|
|
|
|
Don't post the same question in multiple places: it duplicates work and that annoys people.
You already have an answer in QA, so leave it there.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I'm having a dickens of a time trying to instantiate the VCCodeModel interface. This is part of the Visual Studio object model. I eventually want to extract type information from a C++ project directly from Visual Studio.
First I locate the Visual C++ project in the solution, and then I get a reference to the Project object.
The problem is when I try to cast the Project.CodeModel interface to a VCCodeModel interface, which is what they say to do in the documentation[^].
The error that I receive is:
'Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.VisualStudio.VCCodeModel.VCCodeModel'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{A590E96B-CC8C-48AF-9E8F-7C3FE7865586}' failed due to the following error: Interface not registered (Exception from HRESULT: 0x80040155).' This is interesting because the GUID in the error message is not the GUID given by the documentation for the VCCodeModel interface! The correct GUID is {E6691CDE-9A41-4891-8D8C-C1E93958E6A0}
Does anyone know what's going on here? Why can't I retrieve the VCCodeModel interface as they do in the docs, and why does it try to cast it to the wrong IID ?
My code is below. I'm using Visual Studio 2017.
private void buttonExtract_Click(object sender, EventArgs e)
{
EnvDTE80.DTE2 dte2 =
(DTE2) System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.15.0");
int Count = dte2.Solution.Projects.Count;
Project P = null;
Project FW32 = null;
for (int a = 1; a <= Count; a++)
{
P = dte2.Solution.Projects.Item(a);
if (P.FullName.Contains("ConsoleApplication"))
{
break;
}
}
VCCodeModel vcCodeModel = (VCCodeModel) P.CodeModel;
}
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|